Skip to content

Commit 06bdd52

Browse files
author
Bryan C. Mills
committed
os: use an actual RemoveAll failure in TestRemoveAllWithMoreErrorThanReqSize
Previously we injected an error, and the injection points were (empirically) not realistic on some platforms. Instead, we now make the directory read-only, which (on most platforms) suffices to prevent the removal of its files. Fixes #35117 Updates #29921 Change-Id: Ica4e2818566f8c14df3eed7c3b8de5c0abeb6963 Reviewed-on: https://go-review.googlesource.com/c/go/+/203502 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 66f78e9 commit 06bdd52

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

src/os/export_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ package os
99
var Atime = atime
1010
var LstatP = &lstat
1111
var ErrWriteAtInAppendMode = errWriteAtInAppendMode
12-
var RemoveAllTestHook = &removeAllTestHook

src/os/removeall_at.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ func removeAllFrom(parent *File, base string) error {
153153

154154
// Remove the directory itself.
155155
unlinkError := unix.Unlinkat(parentFd, base, unix.AT_REMOVEDIR)
156-
unlinkError = removeAllTestHook(unlinkError)
157156
if unlinkError == nil || IsNotExist(unlinkError) {
158157
return nil
159158
}

src/os/removeall_noat.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ func removeAll(path string) error {
124124

125125
// Remove directory.
126126
err1 := Remove(path)
127-
err1 = removeAllTestHook(err1)
128127
if err1 == nil || IsNotExist(err1) {
129128
return nil
130129
}

src/os/removeall_test.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package os_test
66

77
import (
8-
"errors"
98
"fmt"
109
"io/ioutil"
1110
. "os"
@@ -413,14 +412,6 @@ func TestRemoveAllWithMoreErrorThanReqSize(t *testing.T) {
413412
t.Skip("skipping in short mode")
414413
}
415414

416-
defer func(oldHook func(error) error) {
417-
*RemoveAllTestHook = oldHook
418-
}(*RemoveAllTestHook)
419-
420-
*RemoveAllTestHook = func(err error) error {
421-
return errors.New("error from RemoveAllTestHook")
422-
}
423-
424415
tmpDir, err := ioutil.TempDir("", "TestRemoveAll-")
425416
if err != nil {
426417
t.Fatal(err)
@@ -429,7 +420,7 @@ func TestRemoveAllWithMoreErrorThanReqSize(t *testing.T) {
429420

430421
path := filepath.Join(tmpDir, "_TestRemoveAllWithMoreErrorThanReqSize_")
431422

432-
// Make directory with 1025 files and remove.
423+
// Make directory with 1025 read-only files.
433424
if err := MkdirAll(path, 0777); err != nil {
434425
t.Fatalf("MkdirAll %q: %s", path, err)
435426
}
@@ -442,13 +433,40 @@ func TestRemoveAllWithMoreErrorThanReqSize(t *testing.T) {
442433
fd.Close()
443434
}
444435

445-
// This call should not hang
446-
if err := RemoveAll(path); err == nil {
447-
t.Fatal("Want error from RemoveAllTestHook, got nil")
436+
// Make the parent directory read-only. On some platforms, this is what
437+
// prevents os.Remove from removing the files within that directory.
438+
if err := Chmod(path, 0555); err != nil {
439+
t.Fatal(err)
448440
}
441+
defer Chmod(path, 0755)
449442

450-
// We hook to inject error, but the actual files must be deleted
451-
if _, err := Lstat(path); err == nil {
452-
t.Fatal("directory must be deleted even with removeAllTetHook run")
443+
// This call should not hang, even on a platform that disallows file deletion
444+
// from read-only directories.
445+
err = RemoveAll(path)
446+
447+
if Getuid() == 0 {
448+
// On many platforms, root can remove files from read-only directories.
449+
return
450+
}
451+
if err == nil {
452+
t.Fatal("RemoveAll(<read-only directory>) = nil; want error")
453+
}
454+
455+
dir, err := Open(path)
456+
if err != nil {
457+
t.Fatal(err)
458+
}
459+
defer dir.Close()
460+
461+
if runtime.GOOS == "windows" {
462+
// Marking a directory in Windows does not prevent the os package from
463+
// creating or removing files within it.
464+
// (See https://golang.org/issue/35042.)
465+
return
466+
}
467+
468+
names, _ := dir.Readdirnames(1025)
469+
if len(names) < 1025 {
470+
t.Fatalf("RemoveAll(<read-only directory>) unexpectedly removed %d read-only files from that directory", 1025-len(names))
453471
}
454472
}

0 commit comments

Comments
 (0)