| 1 | // Copyright (c) 2019, Daniel Martà <[email protected]> |
| 2 | // See LICENSE for licensing information |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "encoding/json" |
| 8 | "flag" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "testing" |
| 13 | |
| 14 | qt "github.com/frankban/quicktest" |
| 15 | |
| 16 | "github.com/rogpeppe/go-internal/gotooltest" |
| 17 | "github.com/rogpeppe/go-internal/testscript" |
| 18 | ) |
| 19 | |
| 20 | func TestMain(m *testing.M) { |
| 21 | os.Exit(testscript.RunMain(m, map[string]func() int{ |
| 22 | "gofumpt": main1, |
| 23 | })) |
| 24 | } |
| 25 | |
| 26 | var update = flag.Bool("u", false, "update testscript output files") |
| 27 | |
| 28 | func TestScript(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | |
| 31 | var goEnv struct { |
| 32 | GOCACHE string |
| 33 | GOMODCACHE string |
| 34 | GOMOD string |
| 35 | } |
| 36 | out, err := exec.Command("go", "env", "-json").CombinedOutput() |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if err := json.Unmarshal(out, &goEnv); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | |
| 44 | p := testscript.Params{ |
| 45 | Dir: filepath.Join("testdata", "script"), |
| 46 | UpdateScripts: *update, |
| 47 | RequireExplicitExec: true, |
| 48 | Setup: func(env *testscript.Env) error { |
| 49 | env.Setenv("GOCACHE", goEnv.GOCACHE) |
| 50 | env.Setenv("GOMODCACHE", goEnv.GOMODCACHE) |
| 51 | env.Setenv("GOMOD_DIR", filepath.Dir(goEnv.GOMOD)) |
| 52 | return nil |
| 53 | }, |
| 54 | } |
| 55 | err = gotooltest.Setup(&p) |
| 56 | qt.Assert(t, err, qt.IsNil) |
| 57 | testscript.Run(t, p) |
| 58 | } |