5
5
package test
6
6
7
7
import (
8
+ "bytes"
8
9
"io"
10
+ "io/ioutil"
9
11
"os"
12
+ "os/exec"
10
13
"path/filepath"
14
+ "runtime"
11
15
"sort"
12
16
"strings"
13
17
"testing"
18
+
19
+ "github.com/pkg/errors"
14
20
)
15
21
16
22
const (
@@ -23,41 +29,60 @@ type IntegrationTestProject struct {
23
29
t * testing.T
24
30
h * Helper
25
31
preImports []string
32
+ tempdir string
33
+ env []string
34
+ origWd string
35
+ stdout bytes.Buffer
36
+ stderr bytes.Buffer
26
37
}
27
38
28
- func NewTestProject (t * testing.T , initPath string ) * IntegrationTestProject {
39
+ func NewTestProject (t * testing.T , initPath , wd string ) * IntegrationTestProject {
29
40
new := & IntegrationTestProject {
30
- t : t ,
31
- h : NewHelper (t ),
41
+ t : t ,
42
+ origWd : wd ,
43
+ env : os .Environ (),
32
44
}
33
- new .TempDir ( ProjectRoot )
45
+ new .makeRootTempDir ( )
34
46
new .TempDir (ProjectRoot , "vendor" )
35
47
new .CopyTree (initPath )
36
- new .h .Setenv ("GOPATH" , new .h .Path ("." ))
37
- new .h .Cd (new .Path (ProjectRoot ))
48
+
49
+ // Note that the Travis darwin platform, directories with certain roots such
50
+ // as /var are actually links to a dirtree under /private. Without the patch
51
+ // below the wd, and therefore the GOPATH, is recorded as "/var/..." but the
52
+ // actual process runs in "/private/var/..." and dies due to not being in the
53
+ // GOPATH because the roots don't line up.
54
+ if runtime .GOOS == "darwin" && needsPrivateLeader (new .tempdir ) {
55
+ new .Setenv ("GOPATH" , filepath .Join ("/private" , new .tempdir ))
56
+ } else {
57
+ new .Setenv ("GOPATH" , new .tempdir )
58
+ }
59
+
38
60
return new
39
61
}
40
62
41
63
func (p * IntegrationTestProject ) Cleanup () {
42
- p . h . Cleanup ( )
64
+ os . RemoveAll ( p . tempdir )
43
65
}
44
66
45
67
func (p * IntegrationTestProject ) Path (args ... string ) string {
46
- return p . h . Path ( filepath .Join (args ... ))
68
+ return filepath . Join ( p . tempdir , filepath .Join (args ... ))
47
69
}
48
70
49
- func (p * IntegrationTestProject ) TempDir (args ... string ) {
50
- p .h .TempDir (filepath .Join (args ... ))
71
+ func (p * IntegrationTestProject ) ProjPath (args ... string ) string {
72
+ localPath := append ([]string {ProjectRoot }, args ... )
73
+ return p .Path (localPath ... )
51
74
}
52
75
53
- func (p * IntegrationTestProject ) TempProjDir (args ... string ) {
54
- localPath := append ([]string {ProjectRoot }, args ... )
55
- p .h .TempDir (filepath .Join (localPath ... ))
76
+ func (p * IntegrationTestProject ) TempDir (args ... string ) {
77
+ fullPath := p .Path (args ... )
78
+ if err := os .MkdirAll (fullPath , 0755 ); err != nil && ! os .IsExist (err ) {
79
+ p .t .Fatalf ("%+v" , errors .Errorf ("Unable to create temp directory: %s" , fullPath ))
80
+ }
56
81
}
57
82
58
- func (p * IntegrationTestProject ) ProjPath (args ... string ) string {
83
+ func (p * IntegrationTestProject ) TempProjDir (args ... string ) {
59
84
localPath := append ([]string {ProjectRoot }, args ... )
60
- return p . Path (localPath ... )
85
+ p . TempDir (localPath ... )
61
86
}
62
87
63
88
func (p * IntegrationTestProject ) VendorPath (args ... string ) string {
@@ -67,11 +92,51 @@ func (p *IntegrationTestProject) VendorPath(args ...string) string {
67
92
}
68
93
69
94
func (p * IntegrationTestProject ) RunGo (args ... string ) {
70
- p .h .RunGo (args ... )
95
+ cmd := exec .Command ("go" , args ... )
96
+ p .stdout .Reset ()
97
+ p .stderr .Reset ()
98
+ cmd .Stdout = & p .stdout
99
+ cmd .Stderr = & p .stderr
100
+ cmd .Dir = p .tempdir
101
+ cmd .Env = p .env
102
+ status := cmd .Run ()
103
+ if p .stdout .Len () > 0 {
104
+ p .t .Log ("go standard output:" )
105
+ p .t .Log (p .stdout .String ())
106
+ }
107
+ if p .stderr .Len () > 0 {
108
+ p .t .Log ("go standard error:" )
109
+ p .t .Log (p .stderr .String ())
110
+ }
111
+ if status != nil {
112
+ p .t .Logf ("go %v failed unexpectedly: %v" , args , status )
113
+ p .t .FailNow ()
114
+ }
71
115
}
72
116
73
117
func (p * IntegrationTestProject ) RunGit (dir string , args ... string ) {
74
- p .h .RunGit (dir , args ... )
118
+ cmd := exec .Command ("git" , args ... )
119
+ p .stdout .Reset ()
120
+ p .stderr .Reset ()
121
+ cmd .Stdout = & p .stdout
122
+ cmd .Stderr = & p .stderr
123
+ cmd .Dir = dir
124
+ cmd .Env = p .env
125
+ status := cmd .Run ()
126
+ if * PrintLogs {
127
+ if p .stdout .Len () > 0 {
128
+ p .t .Logf ("git %v standard output:" , args )
129
+ p .t .Log (p .stdout .String ())
130
+ }
131
+ if p .stderr .Len () > 0 {
132
+ p .t .Logf ("git %v standard error:" , args )
133
+ p .t .Log (p .stderr .String ())
134
+ }
135
+ }
136
+ if status != nil {
137
+ p .t .Logf ("git %v failed unexpectedly: %v" , args , status )
138
+ p .t .FailNow ()
139
+ }
75
140
}
76
141
77
142
func (p * IntegrationTestProject ) GetVendorGit (ip string ) {
@@ -82,7 +147,32 @@ func (p *IntegrationTestProject) GetVendorGit(ip string) {
82
147
}
83
148
84
149
func (p * IntegrationTestProject ) DoRun (args []string ) error {
85
- return p .h .DoRun (args )
150
+ if * PrintLogs {
151
+ p .t .Logf ("running testdep %v" , args )
152
+ }
153
+ var prog string
154
+ prog = filepath .Join (p .origWd , "testdep" + ExeSuffix )
155
+ newargs := []string {args [0 ], "-v" }
156
+ newargs = append (newargs , args [1 :]... )
157
+ cmd := exec .Command (prog , newargs ... )
158
+ p .stdout .Reset ()
159
+ p .stderr .Reset ()
160
+ cmd .Stdout = & p .stdout
161
+ cmd .Stderr = & p .stderr
162
+ cmd .Env = p .env
163
+ cmd .Dir = p .ProjPath ("" )
164
+ status := cmd .Run ()
165
+ if * PrintLogs {
166
+ if p .stdout .Len () > 0 {
167
+ p .t .Log ("standard output:" )
168
+ p .t .Log (p .stdout .String ())
169
+ }
170
+ if p .stderr .Len () > 0 {
171
+ p .t .Log ("standard error:" )
172
+ p .t .Log (p .stderr .String ())
173
+ }
174
+ }
175
+ return status
86
176
}
87
177
88
178
func (p * IntegrationTestProject ) CopyTree (src string ) {
@@ -159,6 +249,7 @@ func (p *IntegrationTestProject) GetImportPaths() []string {
159
249
return result
160
250
}
161
251
252
+ // Take a snapshot of the import paths before test is run
162
253
func (p * IntegrationTestProject ) RecordImportPaths () {
163
254
p .preImports = p .GetImportPaths ()
164
255
}
@@ -176,3 +267,38 @@ func (p *IntegrationTestProject) CompareImportPaths() {
176
267
}
177
268
}
178
269
}
270
+
271
+ // makeRootTempdir makes a temporary directory for a run of testgo. If
272
+ // the temporary directory was already created, this does nothing.
273
+ func (p * IntegrationTestProject ) makeRootTempDir () {
274
+ if p .tempdir == "" {
275
+ var err error
276
+ p .tempdir , err = ioutil .TempDir ("" , "gotest" )
277
+ p .Must (err )
278
+ }
279
+ }
280
+
281
+ // Setenv sets an environment variable to use when running the test go
282
+ // command.
283
+ func (p * IntegrationTestProject ) Setenv (name , val string ) {
284
+ p .env = append (p .env , name + "=" + val )
285
+ }
286
+
287
+ // Must gives a fatal error if err is not nil.
288
+ func (p * IntegrationTestProject ) Must (err error ) {
289
+ if err != nil {
290
+ p .t .Fatalf ("%+v" , err )
291
+ }
292
+ }
293
+
294
+ // Checks for filepath beginnings that result in the "/private" leader
295
+ // on Mac platforms
296
+ func needsPrivateLeader (path string ) bool {
297
+ var roots = []string {"/var" , "/tmp" , "/etc" }
298
+ for _ , root := range roots {
299
+ if strings .HasPrefix (path , root ) {
300
+ return true
301
+ }
302
+ }
303
+ return false
304
+ }
0 commit comments