Skip to content

Commit 6ea846c

Browse files
committed
path/filepath: use a temp dir in path_test.go
We should avoid writing temp files to GOROOT, since it might be readonly. Fixes #23881 Change-Id: Iaa38ec404b303f0cf27fdfb7daf1ddd60fd5d1c9
1 parent 8ce74b7 commit 6ea846c

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/path/filepath/path_test.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ type Node struct {
326326
mark int
327327
}
328328

329-
var tree = &Node{
329+
var testTree = &Node{
330330
"testdata",
331331
[]*Node{
332332
{"a", nil, 0},
@@ -359,7 +359,7 @@ func walkTree(n *Node, path string, f func(path string, n *Node)) {
359359
}
360360
}
361361

362-
func makeTree(t *testing.T) {
362+
func makeTree(t *testing.T, tree *Node) {
363363
walkTree(tree, tree.name, func(path string, n *Node) {
364364
if n.entries == nil {
365365
fd, err := os.Create(path)
@@ -376,7 +376,7 @@ func makeTree(t *testing.T) {
376376

377377
func markTree(n *Node) { walkTree(n, "", func(path string, n *Node) { n.mark++ }) }
378378

379-
func checkMarks(t *testing.T, report bool) {
379+
func checkMarks(t *testing.T, report bool, tree *Node) {
380380
walkTree(tree, tree.name, func(path string, n *Node) {
381381
if n.mark != 1 && report {
382382
t.Errorf("node %s mark = %d; expected 1", path, n.mark)
@@ -388,7 +388,7 @@ func checkMarks(t *testing.T, report bool) {
388388
// Assumes that each node name is unique. Good enough for a test.
389389
// If clear is true, any incoming error is cleared before return. The errors
390390
// 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 {
392392
name := info.Name()
393393
walkTree(tree, tree.name, func(path string, n *Node) {
394394
if n.name == name {
@@ -433,21 +433,38 @@ func TestWalk(t *testing.T) {
433433
defer restore()
434434
}
435435
}
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)
437454
errors := make([]error, 0, 10)
438455
clear := true
439456
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)
441458
}
442459
// Expect no errors.
443-
err := filepath.Walk(tree.name, markFn)
460+
err = filepath.Walk(tree.name, markFn)
444461
if err != nil {
445462
t.Fatalf("no error expected, found: %s", err)
446463
}
447464
if len(errors) != 0 {
448465
t.Fatalf("unexpected errors: %s", errors)
449466
}
450-
checkMarks(t, true)
467+
checkMarks(t, true, tree)
451468
errors = errors[0:0]
452469

453470
// Test permission errors. Only possible if we're not root
@@ -473,7 +490,7 @@ func TestWalk(t *testing.T) {
473490
t.Errorf("expected 2 errors, got %d: %s", len(errors), errors)
474491
}
475492
// the inaccessible subtrees were marked manually
476-
checkMarks(t, true)
493+
checkMarks(t, true, tree)
477494
errors = errors[0:0]
478495

479496
// 4) capture errors, stop after first error.
@@ -492,7 +509,7 @@ func TestWalk(t *testing.T) {
492509
t.Errorf("expected 1 error, got %d: %s", len(errors), errors)
493510
}
494511
// the inaccessible subtrees were marked manually
495-
checkMarks(t, false)
512+
checkMarks(t, false, tree)
496513
errors = errors[0:0]
497514

498515
// restore permissions

0 commit comments

Comments
 (0)