forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testproj.go
310 lines (274 loc) · 7.99 KB
/
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
305
306
307
308
309
310
// 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 integration
import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
"github.com/golang/dep/internal/test"
"github.com/pkg/errors"
)
const (
projectRoot string = "src/github.com/golang/notexist"
)
// RunFunc defines the function signature for an integration test command to execute.
type RunFunc func(prog string, newargs []string, outW, errW io.Writer, dir string, env []string) error
// TestProject manages the "virtual" test project directory structure
// and content
type TestProject struct {
t *testing.T
preImports []string
tempdir string
env []string
origWd string
stdout bytes.Buffer
stderr bytes.Buffer
run RunFunc
}
// NewTestProject initializes a new test's project directory.
func NewTestProject(t *testing.T, initPath, wd string, run RunFunc) *TestProject {
new := &TestProject{
t: t,
origWd: wd,
env: os.Environ(),
run: run,
}
new.makeRootTempDir()
new.TempDir(projectRoot, "vendor")
new.CopyTree(initPath)
new.Setenv("GOPATH", new.tempdir)
return new
}
// Cleanup (remove) the test project's directory.
func (p *TestProject) Cleanup() {
os.RemoveAll(p.tempdir)
}
// Path to the test project directory.
func (p *TestProject) Path(args ...string) string {
return filepath.Join(p.tempdir, filepath.Join(args...))
}
// ProjPath builds an import path for the test project.
func (p *TestProject) ProjPath(args ...string) string {
localPath := append([]string{projectRoot}, args...)
return p.Path(localPath...)
}
// TempDir creates a temporary directory for the test project.
func (p *TestProject) 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))
}
}
// TempProjDir builds the path to a package within the test project.
func (p *TestProject) TempProjDir(args ...string) {
localPath := append([]string{projectRoot}, args...)
p.TempDir(localPath...)
}
// VendorPath lists the contents of the test project's vendor directory.
func (p *TestProject) VendorPath(args ...string) string {
localPath := append([]string{projectRoot, "vendor"}, args...)
p.TempDir(localPath...)
return p.Path(localPath...)
}
// RunGo runs a go command, and expects it to succeed.
func (p *TestProject) 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()
}
}
// RunGit runs a git command, and expects it to succeed.
func (p *TestProject) 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 *test.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()
}
}
// GetStdout gets the Stdout output from test run.
func (p *TestProject) GetStdout() string {
return p.stdout.String()
}
// GetStderr gets the Stderr output from test run.
func (p *TestProject) GetStderr() string {
return p.stderr.String()
}
// GetVendorGit populates the initial vendor directory for a test project.
func (p *TestProject) 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)
}
// DoRun executes the integration test command against the test project.
func (p *TestProject) DoRun(args []string) error {
if *test.PrintLogs {
p.t.Logf("running testdep %v", args)
}
prog := filepath.Join(p.origWd, "testdep"+test.ExeSuffix)
newargs := append([]string{args[0], "-v"}, args[1:]...)
p.stdout.Reset()
p.stderr.Reset()
status := p.run(prog, newargs, &p.stdout, &p.stderr, p.ProjPath(""), p.env)
if *test.PrintLogs {
if p.stdout.Len() > 0 {
p.t.Logf("\nstandard output:%s", p.stdout.String())
}
if p.stderr.Len() > 0 {
p.t.Logf("standard error:\n%s", p.stderr.String())
}
}
return status
}
// CopyTree recursively copies a source directory into the test project's directory.
func (p *TestProject) 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)
}
// GetVendorPaths collects final vendor paths at a depth of three levels.
func (p *TestProject) 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
}
// GetImportPaths collect final vendor paths at a depth of three levels.
func (p *TestProject) 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
}
// RecordImportPaths takes a snapshot of the import paths before test is run.
func (p *TestProject) RecordImportPaths() {
p.preImports = p.GetImportPaths()
}
// CompareImportPaths compares import paths before and after test commands.
func (p *TestProject) 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 *TestProject) makeRootTempDir() {
if p.tempdir == "" {
var err error
p.tempdir, err = ioutil.TempDir("", "gotest")
p.Must(err)
// Fix for OSX where the tempdir is a symlink:
if runtime.GOOS == "darwin" {
p.tempdir, err = filepath.EvalSymlinks(p.tempdir)
p.Must(err)
}
}
}
// Setenv sets an environment variable to use when running the test go
// command.
func (p *TestProject) Setenv(name, val string) {
p.env = append(p.env, name+"="+val)
}
// Must gives a fatal error if err is not nil.
func (p *TestProject) Must(err error) {
if err != nil {
p.t.Fatalf("%+v", err)
}
}