-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
156 lines (143 loc) · 3.46 KB
/
main_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"testing"
"github.com/gunk/gunk/generate"
"github.com/rogpeppe/go-internal/gotooltest"
"github.com/rogpeppe/go-internal/testscript"
)
var write = flag.Bool("w", false, "overwrite testdata output files")
func TestMain(m *testing.M) {
if os.Getenv("TESTSCRIPT_ON") == "" {
flag.Parse()
// Don't put the binaries in a temporary directory to delete, as that
// means we have to re-link them every single time. That's quite
// expensive, at around half a second per 'go test' invocation.
binDir, err := filepath.Abs(".cache")
if err != nil {
panic(err)
}
os.Setenv("GOBIN", binDir)
os.Setenv("PATH", binDir+string(filepath.ListSeparator)+os.Getenv("PATH"))
cmd := exec.Command("go", "install", "-ldflags=-w -s",
"./testdata/protoc-gen-strict",
)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Printf("%+v\n", os.Environ())
panic(err)
}
}
os.Exit(testscript.RunMain(m, map[string]func() int{
"gunk": run,
}))
}
func TestGenerate(t *testing.T) {
t.Parallel()
pkgs := []string{
".", "./imported",
}
dir := filepath.Join("testdata", "generate")
wantFiles, err := generatedFiles(dir)
if err != nil {
t.Fatal(err)
}
for path := range wantFiles {
// make sure we're writing the files
os.Remove(path)
}
if err := generate.Run(dir, pkgs...); err != nil {
t.Fatal(err)
}
if *write {
// don't check that the output files match
return
}
gotFiles, err := generatedFiles(dir)
if err != nil {
t.Fatal(err)
}
for path, got := range gotFiles {
want := wantFiles[path]
if got != want {
t.Errorf("%s was modified", path)
}
delete(wantFiles, path)
}
for path := range wantFiles {
t.Errorf("%s was not generated", path)
}
if testing.Short() {
// the build shouldn't have broken if no files have changed
return
}
cmd := exec.Command("go", append([]string{"build"}, pkgs...)...)
cmd.Dir = dir
if _, err := cmd.Output(); err != nil {
if e, ok := err.(*exec.ExitError); ok {
t.Fatalf("%s", e.Stderr)
}
t.Fatalf("%v", err)
}
}
var rxGeneratedFile = regexp.MustCompile(`\.go|\.json|\.java|\.js$`)
func generatedFiles(dir string) (map[string]string, error) {
files := make(map[string]string)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !rxGeneratedFile.MatchString(info.Name()) {
return nil
}
if info.Name() == "tools.go" {
return nil
}
bs, err := ioutil.ReadFile(path)
if err != nil {
return err
}
files[path] = string(bs)
return nil
})
return files, err
}
func TestScripts(t *testing.T) {
t.Parallel()
cacheDir, err := os.UserCacheDir()
if err != nil {
t.Fatal(err)
}
goCache := filepath.Join(os.TempDir(), "gunk-test-go-cache")
p := testscript.Params{
Dir: filepath.Join("testdata", "scripts"),
Setup: func(e *testscript.Env) error {
cmd := exec.Command("cp",
"testdata/prepare-go-sum/go.mod",
"testdata/prepare-go-sum/go.sum",
e.WorkDir)
cmd.Stderr = os.Stderr
if _, err := cmd.Output(); err != nil {
return fmt.Errorf("failed to copy go.sum: %w", err)
}
e.Vars = append(e.Vars, "GONOSUMDB=*")
e.Vars = append(e.Vars, "GUNK_CACHE_DIR="+cacheDir)
e.Vars = append(e.Vars, "TESTSCRIPT_ON=on")
e.Vars = append(e.Vars, "HOME="+goCache)
return nil
},
}
if err := gotooltest.Setup(&p); err != nil {
t.Fatal(err)
}
testscript.Run(t, p)
}