-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
175 lines (158 loc) · 4.06 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/rogpeppe/go-internal/goproxytest"
"github.com/rogpeppe/go-internal/testscript"
"github.com/gunk/gunk/generate"
)
var write = flag.Bool("w", false, "overwrite testdata output files")
func TestMain(m *testing.M) {
if os.Getenv("TESTSCRIPT_COMMAND") == "" {
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",
"github.com/golang/protobuf/protoc-gen-go",
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway",
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger",
"./docgen/",
"./scopegen/",
"github.com/Kunde21/pulpMd",
"./testdata/protoc-gen-strict",
)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
panic(err)
}
// Start the Go proxy server running for all tests.
srv, err := goproxytest.NewServer("testdata/mod", "")
if err != nil {
log.Fatalf("cannot start proxy: %v", err)
}
proxyURL = srv.URL
}
os.Exit(testscript.RunMain(m, map[string]func() int{
"gunk": main1,
}))
}
var proxyURL string
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(`\.pb.*\.go$`)
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
}
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()
home, err := filepath.Abs(filepath.Join(".cache", "home"))
if err != nil {
t.Fatal(err)
}
cachePath, err := userCachePath()
if err != nil {
t.Fatal(err)
}
testscript.Run(t, testscript.Params{
Dir: filepath.Join("testdata", "scripts"),
Setup: func(e *testscript.Env) error {
e.Vars = append(e.Vars, "GOPROXY="+proxyURL)
e.Vars = append(e.Vars, "GONOSUMDB=*")
e.Vars = append(e.Vars, "HOME="+home)
e.Vars = append(e.Vars, "CACHEPATH="+cachePath)
return nil
},
})
}
// userCacheDir returns relative path of user cached data directory from home directory.
// We need to use relative path, because the absolute user cache directory will be different
// for each OS and in some tests we use a isolated $HOME to avoid caching.
func userCachePath() (string, error) {
realUserCacheDir, err := os.UserCacheDir()
if err != nil {
return "", err
}
realHomeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return strings.TrimPrefix(realUserCacheDir, realHomeDir), nil
}