@@ -184,6 +184,8 @@ func Test(t *testing.T) {
184
184
// Build the targets in each directory.
185
185
var wg sync.WaitGroup
186
186
wg .Add (len (dirs ))
187
+ var errs []error
188
+ var mu sync.Mutex
187
189
for _ , dir := range dirs {
188
190
go func (dir string ) {
189
191
defer wg .Done ()
@@ -196,11 +198,19 @@ func Test(t *testing.T) {
196
198
)
197
199
cmd .Dir = dir
198
200
if err := cmd .Run (); err != nil {
199
- t .Fatalf ("in %s, error running %s: %v" , dir , strings .Join (cmd .Args , " " ), err )
201
+ mu .Lock ()
202
+ errs = append (errs , fmt .Errorf ("in %s, error running %s: %v" , dir , strings .Join (cmd .Args , " " ), err ))
203
+ mu .Unlock ()
200
204
}
201
205
}(dir )
202
206
}
203
207
wg .Wait ()
208
+ if len (errs ) > 0 {
209
+ for _ , err := range errs {
210
+ t .Error (err )
211
+ }
212
+ t .Fatal ("errors building" )
213
+ }
204
214
205
215
t .Run ("Check Hashes" , func (t * testing.T ) {
206
216
// Hash files in each bazel-bin directory.
@@ -232,7 +242,7 @@ func Test(t *testing.T) {
232
242
233
243
// Compare dir0 and dir2. They should be different.
234
244
if err := compareHashes (dirHashes [0 ], dirHashes [2 ]); err == nil {
235
- t .Fatalf ("dir0 and dir2 are the same)" , len ( dirHashes [ 0 ]) )
245
+ t .Fatal ("dir0 and dir2 are the same" )
236
246
}
237
247
})
238
248
@@ -309,24 +319,29 @@ func copyTree(dstRoot, srcRoot string) error {
309
319
310
320
if info .IsDir () {
311
321
return os .Mkdir (dstPath , 0777 )
322
+ } else {
323
+ return copyFile (dstPath , srcPath )
312
324
}
313
- r , err := os .Open (srcPath )
314
- if err != nil {
315
- return nil
316
- }
317
- defer r .Close ()
318
- w , err := os .Create (dstPath )
319
- if err != nil {
320
- return err
321
- }
322
- defer w .Close ()
323
- if _ , err := io .Copy (w , r ); err != nil {
324
- return err
325
- }
326
- return w .Close ()
327
325
})
328
326
}
329
327
328
+ func copyFile (dstPath , srcPath string ) error {
329
+ r , err := os .Open (srcPath )
330
+ if err != nil {
331
+ return nil
332
+ }
333
+ defer r .Close ()
334
+ w , err := os .Create (dstPath )
335
+ if err != nil {
336
+ return err
337
+ }
338
+ defer w .Close ()
339
+ if _ , err := io .Copy (w , r ); err != nil {
340
+ return err
341
+ }
342
+ return w .Close ()
343
+ }
344
+
330
345
func compareHashes (lhs , rhs []fileHash ) error {
331
346
buf := & bytes.Buffer {}
332
347
for li , ri := 0 , 0 ; li < len (lhs ) || ri < len (rhs ); {
@@ -342,6 +357,12 @@ func compareHashes(lhs, rhs []fileHash) error {
342
357
}
343
358
if lhs [li ].hash != rhs [ri ].hash {
344
359
fmt .Fprintf (buf , "%s is different: %s %s\n " , lhs [li ].rel , lhs [li ].hash , rhs [ri ].hash )
360
+ if err := copyToTestOutputs (lhs [li ]); err != nil {
361
+ fmt .Fprintf (buf , "failed to copy file: %v\n " , err )
362
+ }
363
+ if err := copyToTestOutputs (rhs [li ]); err != nil {
364
+ fmt .Fprintf (buf , "failed to copy file: %v\n " , err )
365
+ }
345
366
}
346
367
li ++
347
368
ri ++
@@ -353,7 +374,7 @@ func compareHashes(lhs, rhs []fileHash) error {
353
374
}
354
375
355
376
type fileHash struct {
356
- rel , hash string
377
+ abs , rel , hash string
357
378
}
358
379
359
380
func hashFiles (dir string , exclude func (root , path string ) bool ) ([]fileHash , error ) {
@@ -426,7 +447,7 @@ func hashFiles(dir string, exclude func(root, path string) bool) ([]fileHash, er
426
447
if _ , err := io .Copy (h , r ); err != nil {
427
448
return err
428
449
}
429
- hashes = append (hashes , fileHash {rel : rel , hash : hex .EncodeToString (h .Sum (sum [:0 ]))})
450
+ hashes = append (hashes , fileHash {abs : path , rel : rel , hash : hex .EncodeToString (h .Sum (sum [:0 ]))})
430
451
431
452
return nil
432
453
})
@@ -435,3 +456,9 @@ func hashFiles(dir string, exclude func(root, path string) bool) ([]fileHash, er
435
456
}
436
457
return hashes , nil
437
458
}
459
+
460
+ // copyToTestOutputs copies a hashed file to the test outputs directory so that
461
+ // it can be inspected manually under bazel-testlogs, e.g. using diffoscope.
462
+ func copyToTestOutputs (hash fileHash ) error {
463
+ return copyFile (filepath .Join (os .Getenv ("TEST_UNDECLARED_OUTPUTS_DIR" ), hash .hash ), hash .abs )
464
+ }
0 commit comments