@@ -326,7 +326,7 @@ type Node struct {
326
326
mark int
327
327
}
328
328
329
- var tree = & Node {
329
+ var testTree = & Node {
330
330
"testdata" ,
331
331
[]* Node {
332
332
{"a" , nil , 0 },
@@ -359,7 +359,7 @@ func walkTree(n *Node, path string, f func(path string, n *Node)) {
359
359
}
360
360
}
361
361
362
- func makeTree (t * testing.T ) {
362
+ func makeTree (t * testing.T , tree * Node ) {
363
363
walkTree (tree , tree .name , func (path string , n * Node ) {
364
364
if n .entries == nil {
365
365
fd , err := os .Create (path )
@@ -376,7 +376,7 @@ func makeTree(t *testing.T) {
376
376
377
377
func markTree (n * Node ) { walkTree (n , "" , func (path string , n * Node ) { n .mark ++ }) }
378
378
379
- func checkMarks (t * testing.T , report bool ) {
379
+ func checkMarks (t * testing.T , report bool , tree * Node ) {
380
380
walkTree (tree , tree .name , func (path string , n * Node ) {
381
381
if n .mark != 1 && report {
382
382
t .Errorf ("node %s mark = %d; expected 1" , path , n .mark )
@@ -388,7 +388,7 @@ func checkMarks(t *testing.T, report bool) {
388
388
// Assumes that each node name is unique. Good enough for a test.
389
389
// If clear is true, any incoming error is cleared before return. The errors
390
390
// are always accumulated, though.
391
- func mark (info os.FileInfo , err error , errors * []error , clear bool ) error {
391
+ func mark (info os.FileInfo , err error , errors * []error , clear bool , tree * Node ) error {
392
392
name := info .Name ()
393
393
walkTree (tree , tree .name , func (path string , n * Node ) {
394
394
if n .name == name {
@@ -433,21 +433,38 @@ func TestWalk(t *testing.T) {
433
433
defer restore ()
434
434
}
435
435
}
436
- makeTree (t )
436
+
437
+ tmpDir , err := ioutil .TempDir ("" , "TestWalk" )
438
+ if err != nil {
439
+ t .Fatal ("creating temp dir:" , err )
440
+ }
441
+ defer os .RemoveAll (tmpDir )
442
+
443
+ tmpDirs := strings .FieldsFunc (tmpDir , func (s rune ) bool {
444
+ return s == os .PathSeparator
445
+ })
446
+ var tree * Node
447
+ var prevNode * Node = testTree
448
+ for i := len (tmpDirs ) - 1 ; i >= 0 ; i -- {
449
+ tree = & Node {tmpDirs [i ], []* Node {prevNode }, 0 }
450
+ prevNode = tree
451
+ }
452
+
453
+ makeTree (t , tree )
437
454
errors := make ([]error , 0 , 10 )
438
455
clear := true
439
456
markFn := func (path string , info os.FileInfo , err error ) error {
440
- return mark (info , err , & errors , clear )
457
+ return mark (info , err , & errors , clear , tree )
441
458
}
442
459
// Expect no errors.
443
- err : = filepath .Walk (tree .name , markFn )
460
+ err = filepath .Walk (tree .name , markFn )
444
461
if err != nil {
445
462
t .Fatalf ("no error expected, found: %s" , err )
446
463
}
447
464
if len (errors ) != 0 {
448
465
t .Fatalf ("unexpected errors: %s" , errors )
449
466
}
450
- checkMarks (t , true )
467
+ checkMarks (t , true , tree )
451
468
errors = errors [0 :0 ]
452
469
453
470
// Test permission errors. Only possible if we're not root
@@ -473,7 +490,7 @@ func TestWalk(t *testing.T) {
473
490
t .Errorf ("expected 2 errors, got %d: %s" , len (errors ), errors )
474
491
}
475
492
// the inaccessible subtrees were marked manually
476
- checkMarks (t , true )
493
+ checkMarks (t , true , tree )
477
494
errors = errors [0 :0 ]
478
495
479
496
// 4) capture errors, stop after first error.
@@ -492,7 +509,7 @@ func TestWalk(t *testing.T) {
492
509
t .Errorf ("expected 1 error, got %d: %s" , len (errors ), errors )
493
510
}
494
511
// the inaccessible subtrees were marked manually
495
- checkMarks (t , false )
512
+ checkMarks (t , false , tree )
496
513
errors = errors [0 :0 ]
497
514
498
515
// restore permissions
0 commit comments