This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
integration_testproj.go
304 lines (274 loc) · 7.63 KB
/
integration_testproj.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package test
import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
"github.com/pkg/errors"
)
const (
ProjectRoot string = "src/github.com/golang/notexist"
)
// IntegrationTestProject manages the "virtual" test project directory structure
// and content
type IntegrationTestProject struct {
t *testing.T
h *Helper
preImports []string
tempdir string
env []string
origWd string
stdout bytes.Buffer
stderr bytes.Buffer
}
func NewTestProject(t *testing.T, initPath, wd string) *IntegrationTestProject {
new := &IntegrationTestProject{
t: t,
origWd: wd,
env: os.Environ(),
}
new.makeRootTempDir()
new.TempDir(ProjectRoot, "vendor")
new.CopyTree(initPath)
// Note that the Travis darwin platform, directories with certain roots such
// as /var are actually links to a dirtree under /private. Without the patch
// below the wd, and therefore the GOPATH, is recorded as "/var/..." but the
// actual process runs in "/private/var/..." and dies due to not being in the
// GOPATH because the roots don't line up.
if runtime.GOOS == "darwin" && needsPrivateLeader(new.tempdir) {
new.Setenv("GOPATH", filepath.Join("/private", new.tempdir))
} else {
new.Setenv("GOPATH", new.tempdir)
}
return new
}
func (p *IntegrationTestProject) Cleanup() {
os.RemoveAll(p.tempdir)
}
func (p *IntegrationTestProject) Path(args ...string) string {
return filepath.Join(p.tempdir, filepath.Join(args...))
}
func (p *IntegrationTestProject) ProjPath(args ...string) string {
localPath := append([]string{ProjectRoot}, args...)
return p.Path(localPath...)
}
func (p *IntegrationTestProject) TempDir(args ...string) {
fullPath := p.Path(args...)
if err := os.MkdirAll(fullPath, 0755); err != nil && !os.IsExist(err) {
p.t.Fatalf("%+v", errors.Errorf("Unable to create temp directory: %s", fullPath))
}
}
func (p *IntegrationTestProject) TempProjDir(args ...string) {
localPath := append([]string{ProjectRoot}, args...)
p.TempDir(localPath...)
}
func (p *IntegrationTestProject) VendorPath(args ...string) string {
localPath := append([]string{ProjectRoot, "vendor"}, args...)
p.TempDir(localPath...)
return p.Path(localPath...)
}
func (p *IntegrationTestProject) RunGo(args ...string) {
cmd := exec.Command("go", args...)
p.stdout.Reset()
p.stderr.Reset()
cmd.Stdout = &p.stdout
cmd.Stderr = &p.stderr
cmd.Dir = p.tempdir
cmd.Env = p.env
status := cmd.Run()
if p.stdout.Len() > 0 {
p.t.Log("go standard output:")
p.t.Log(p.stdout.String())
}
if p.stderr.Len() > 0 {
p.t.Log("go standard error:")
p.t.Log(p.stderr.String())
}
if status != nil {
p.t.Logf("go %v failed unexpectedly: %v", args, status)
p.t.FailNow()
}
}
func (p *IntegrationTestProject) RunGit(dir string, args ...string) {
cmd := exec.Command("git", args...)
p.stdout.Reset()
p.stderr.Reset()
cmd.Stdout = &p.stdout
cmd.Stderr = &p.stderr
cmd.Dir = dir
cmd.Env = p.env
status := cmd.Run()
if *PrintLogs {
if p.stdout.Len() > 0 {
p.t.Logf("git %v standard output:", args)
p.t.Log(p.stdout.String())
}
if p.stderr.Len() > 0 {
p.t.Logf("git %v standard error:", args)
p.t.Log(p.stderr.String())
}
}
if status != nil {
p.t.Logf("git %v failed unexpectedly: %v", args, status)
p.t.FailNow()
}
}
func (p *IntegrationTestProject) GetVendorGit(ip string) {
parse := strings.Split(ip, "/")
gitDir := strings.Join(parse[:len(parse)-1], string(filepath.Separator))
p.TempProjDir("vendor", gitDir)
p.RunGit(p.ProjPath("vendor", gitDir), "clone", "http://"+ip)
}
func (p *IntegrationTestProject) DoRun(args []string) error {
if *PrintLogs {
p.t.Logf("running testdep %v", args)
}
var prog string
prog = filepath.Join(p.origWd, "testdep"+ExeSuffix)
newargs := []string{args[0], "-v"}
newargs = append(newargs, args[1:]...)
cmd := exec.Command(prog, newargs...)
p.stdout.Reset()
p.stderr.Reset()
cmd.Stdout = &p.stdout
cmd.Stderr = &p.stderr
cmd.Env = p.env
cmd.Dir = p.ProjPath("")
status := cmd.Run()
if *PrintLogs {
if p.stdout.Len() > 0 {
p.t.Log("standard output:")
p.t.Log(p.stdout.String())
}
if p.stderr.Len() > 0 {
p.t.Log("standard error:")
p.t.Log(p.stderr.String())
}
}
return status
}
func (p *IntegrationTestProject) CopyTree(src string) {
filepath.Walk(src,
func(path string, info os.FileInfo, err error) error {
if path != src {
localpath := path[len(src)+1:]
if info.IsDir() {
p.TempDir(ProjectRoot, localpath)
} else {
destpath := filepath.Join(p.ProjPath(), localpath)
copyFile(destpath, path)
}
}
return nil
})
}
func copyFile(dest, src string) {
in, err := os.Open(src)
if err != nil {
panic(err)
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(out, in)
}
// Collect final vendor paths at a depth of three levels
func (p *IntegrationTestProject) GetVendorPaths() []string {
vendorPath := p.ProjPath("vendor")
result := make([]string, 0)
filepath.Walk(
vendorPath,
func(path string, info os.FileInfo, err error) error {
if len(path) > len(vendorPath) && info.IsDir() {
parse := strings.Split(path[len(vendorPath)+1:], string(filepath.Separator))
if len(parse) == 3 {
result = append(result, strings.Join(parse, "/"))
return filepath.SkipDir
}
}
return nil
},
)
sort.Strings(result)
return result
}
// Collect final vendor paths at a depth of three levels
func (p *IntegrationTestProject) GetImportPaths() []string {
importPath := p.Path("src")
result := make([]string, 0)
filepath.Walk(
importPath,
func(path string, info os.FileInfo, err error) error {
if len(path) > len(importPath) && info.IsDir() {
parse := strings.Split(path[len(importPath)+1:], string(filepath.Separator))
if len(parse) == 3 {
result = append(result, strings.Join(parse, "/"))
return filepath.SkipDir
}
}
return nil
},
)
sort.Strings(result)
return result
}
// Take a snapshot of the import paths before test is run
func (p *IntegrationTestProject) RecordImportPaths() {
p.preImports = p.GetImportPaths()
}
// Compare import paths before and after commands
func (p *IntegrationTestProject) CompareImportPaths() {
wantImportPaths := p.preImports
gotImportPaths := p.GetImportPaths()
if len(gotImportPaths) != len(wantImportPaths) {
p.t.Fatalf("Import path count changed during command: pre %d post %d", len(wantImportPaths), len(gotImportPaths))
}
for ind := range gotImportPaths {
if gotImportPaths[ind] != wantImportPaths[ind] {
p.t.Errorf("Change in import paths during: pre %s post %s", gotImportPaths, wantImportPaths)
}
}
}
// makeRootTempdir makes a temporary directory for a run of testgo. If
// the temporary directory was already created, this does nothing.
func (p *IntegrationTestProject) makeRootTempDir() {
if p.tempdir == "" {
var err error
p.tempdir, err = ioutil.TempDir("", "gotest")
p.Must(err)
}
}
// Setenv sets an environment variable to use when running the test go
// command.
func (p *IntegrationTestProject) Setenv(name, val string) {
p.env = append(p.env, name+"="+val)
}
// Must gives a fatal error if err is not nil.
func (p *IntegrationTestProject) Must(err error) {
if err != nil {
p.t.Fatalf("%+v", err)
}
}
// Checks for filepath beginnings that result in the "/private" leader
// on Mac platforms
func needsPrivateLeader(path string) bool {
var roots = []string{"/var", "/tmp", "/etc"}
for _, root := range roots {
if strings.HasPrefix(path, root) {
return true
}
}
return false
}