cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
cmd/pint/main_test.go
58lines · modecode
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rogpeppe/go-internal/testscript" |
| 9 | "github.com/rs/zerolog" |
| 10 | "github.com/rs/zerolog/log" |
| 11 | ) |
| 12 | |
| 13 | // mock command that fails tests if error is returned |
| 14 | func mockMainShouldSucceed() int { |
| 15 | app := newApp() |
| 16 | err := app.Run(os.Args) |
| 17 | if err != nil { |
| 18 | log.WithLevel(zerolog.FatalLevel).Err(err).Msg("Fatal error") |
| 19 | return 1 |
| 20 | } |
| 21 | return 0 |
| 22 | } |
| 23 | |
| 24 | // mock command that fails tests if no error is returned |
| 25 | func mockMainShouldFail() int { |
| 26 | app := newApp() |
| 27 | err := app.Run(os.Args) |
| 28 | if err != nil { |
| 29 | log.WithLevel(zerolog.FatalLevel).Err(err).Msg("Fatal error") |
| 30 | return 0 |
| 31 | } |
| 32 | fmt.Fprintf(os.Stderr, "expected an error but none was returned\n") |
| 33 | return 1 |
| 34 | } |
| 35 | |
| 36 | func TestMain(m *testing.M) { |
| 37 | os.Exit(testscript.RunMain(m, map[string]func() int{ |
| 38 | "pint.ok": mockMainShouldSucceed, |
| 39 | "pint.error": mockMainShouldFail, |
| 40 | })) |
| 41 | } |
| 42 | |
| 43 | func TestScripts(t *testing.T) { |
| 44 | testscript.Run(t, testscript.Params{ |
| 45 | Dir: "tests", |
| 46 | UpdateScripts: os.Getenv("UPDATE_SNAPSHOTS") == "1", |
| 47 | Setup: func(env *testscript.Env) error { |
| 48 | // inject an env variable with the current working directory |
| 49 | // so we can use it to copy files into testscript workdir |
| 50 | cwd, err := os.Getwd() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | env.Vars = append(env.Vars, fmt.Sprintf("TESTCWD=%s", cwd)) |
| 55 | return nil |
| 56 | }, |
| 57 | }) |
| 58 | } |
| 59 | |