-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_test.go
61 lines (52 loc) · 1.4 KB
/
script_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package snap_test
import (
"flag"
"os"
"path/filepath"
"testing"
"github.com/rogpeppe/go-internal/gotooltest"
"github.com/rogpeppe/go-internal/testscript"
"golang.org/x/mod/modfile"
)
var updateGolden = flag.Bool("update-golden", false, "update golden files in test scripts")
func TestScript(t *testing.T) {
p := testscript.Params{
Dir: "testdata",
UpdateScripts: *updateGolden,
}
gotooltest.Setup(&p)
// make a go.mod for the test that points at the current directory:
modDir, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %s", err)
}
modPath := filepath.Join(modDir, "go.mod")
modBytes, err := os.ReadFile(modPath)
if err != nil {
t.Fatalf("os.ReadFile: %s", err)
}
mod, err := modfile.Parse(modPath, modBytes, nil)
if err != nil {
t.Fatalf("modfile.Parse: %s", err)
}
mod.AddModuleStmt("github.com/jellevandenhooff/snap/example")
mod.AddRequire("github.com/jellevandenhooff/snap", "v0.0.0")
mod.AddReplace("github.com/jellevandenhooff/snap", "", modDir, "")
modBytes, err = mod.Format()
if err != nil {
t.Fatalf("mod.Format: %s", err)
}
origSetup := p.Setup
p.Setup = func(e *testscript.Env) error {
if err := origSetup(e); err != nil {
return err
}
// put the fake go.mod in the work directory
if err := os.WriteFile(filepath.Join(e.WorkDir, "go.mod"), modBytes, 0644); err != nil {
t.Fatal(err)
}
return nil
}
// run tests
testscript.Run(t, p)
}