1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 func TestRemoveAll(t
*testing
.T
) {
19 if err
:= RemoveAll(""); err
!= nil {
20 t
.Errorf("RemoveAll(\"\"): %v; want nil", err
)
23 file
:= filepath
.Join(tmpDir
, "file")
24 path
:= filepath
.Join(tmpDir
, "_TestRemoveAll_")
25 fpath
:= filepath
.Join(path
, "file")
26 dpath
:= filepath
.Join(path
, "dir")
28 // Make a regular file and remove
29 fd
, err
:= Create(file
)
31 t
.Fatalf("create %q: %s", file
, err
)
34 if err
= RemoveAll(file
); err
!= nil {
35 t
.Fatalf("RemoveAll %q (first): %s", file
, err
)
37 if _
, err
= Lstat(file
); err
== nil {
38 t
.Fatalf("Lstat %q succeeded after RemoveAll (first)", file
)
41 // Make directory with 1 file and remove.
42 if err
:= MkdirAll(path
, 0777); err
!= nil {
43 t
.Fatalf("MkdirAll %q: %s", path
, err
)
45 fd
, err
= Create(fpath
)
47 t
.Fatalf("create %q: %s", fpath
, err
)
50 if err
= RemoveAll(path
); err
!= nil {
51 t
.Fatalf("RemoveAll %q (second): %s", path
, err
)
53 if _
, err
= Lstat(path
); err
== nil {
54 t
.Fatalf("Lstat %q succeeded after RemoveAll (second)", path
)
57 // Make directory with file and subdirectory and remove.
58 if err
= MkdirAll(dpath
, 0777); err
!= nil {
59 t
.Fatalf("MkdirAll %q: %s", dpath
, err
)
61 fd
, err
= Create(fpath
)
63 t
.Fatalf("create %q: %s", fpath
, err
)
66 fd
, err
= Create(dpath
+ "/file")
68 t
.Fatalf("create %q: %s", fpath
, err
)
71 if err
= RemoveAll(path
); err
!= nil {
72 t
.Fatalf("RemoveAll %q (third): %s", path
, err
)
74 if _
, err
:= Lstat(path
); err
== nil {
75 t
.Fatalf("Lstat %q succeeded after RemoveAll (third)", path
)
78 // Chmod is not supported under Windows and test fails as root.
79 if runtime
.GOOS
!= "windows" && Getuid() != 0 {
80 // Make directory with file and subdirectory and trigger error.
81 if err
= MkdirAll(dpath
, 0777); err
!= nil {
82 t
.Fatalf("MkdirAll %q: %s", dpath
, err
)
85 for _
, s
:= range []string{fpath
, dpath
+ "/file1", path
+ "/zzz"} {
88 t
.Fatalf("create %q: %s", s
, err
)
92 if err
= Chmod(dpath
, 0); err
!= nil {
93 t
.Fatalf("Chmod %q 0: %s", dpath
, err
)
96 // No error checking here: either RemoveAll
97 // will or won't be able to remove dpath;
98 // either way we want to see if it removes fpath
99 // and path/zzz. Reasons why RemoveAll might
100 // succeed in removing dpath as well include:
102 // * running on a file system without permissions (FAT)
106 for _
, s
:= range []string{fpath
, path
+ "/zzz"} {
107 if _
, err
= Lstat(s
); err
== nil {
108 t
.Fatalf("Lstat %q succeeded after partial RemoveAll", s
)
112 if err
= RemoveAll(path
); err
!= nil {
113 t
.Fatalf("RemoveAll %q after partial RemoveAll: %s", path
, err
)
115 if _
, err
= Lstat(path
); err
== nil {
116 t
.Fatalf("Lstat %q succeeded after RemoveAll (final)", path
)
120 // Test RemoveAll on a large directory.
121 func TestRemoveAllLarge(t
*testing
.T
) {
123 t
.Skip("skipping in short mode")
126 tmpDir
:= t
.TempDir()
127 path
:= filepath
.Join(tmpDir
, "_TestRemoveAllLarge_")
129 // Make directory with 1000 files and remove.
130 if err
:= MkdirAll(path
, 0777); err
!= nil {
131 t
.Fatalf("MkdirAll %q: %s", path
, err
)
133 for i
:= 0; i
< 1000; i
++ {
134 fpath
:= fmt
.Sprintf("%s/file%d", path
, i
)
135 fd
, err
:= Create(fpath
)
137 t
.Fatalf("create %q: %s", fpath
, err
)
141 if err
:= RemoveAll(path
); err
!= nil {
142 t
.Fatalf("RemoveAll %q: %s", path
, err
)
144 if _
, err
:= Lstat(path
); err
== nil {
145 t
.Fatalf("Lstat %q succeeded after RemoveAll", path
)
149 func TestRemoveAllLongPath(t
*testing
.T
) {
150 switch runtime
.GOOS
{
151 case "aix", "darwin", "ios", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "illumos", "solaris":
154 t
.Skip("skipping for not implemented platforms")
157 prevDir
, err
:= Getwd()
159 t
.Fatalf("Could not get wd: %s", err
)
162 startPath
, err
:= os
.MkdirTemp("", "TestRemoveAllLongPath-")
164 t
.Fatalf("Could not create TempDir: %s", err
)
166 defer RemoveAll(startPath
)
168 err
= Chdir(startPath
)
170 t
.Fatalf("Could not chdir %s: %s", startPath
, err
)
173 // Removing paths with over 4096 chars commonly fails
174 for i
:= 0; i
< 41; i
++ {
175 name
:= strings
.Repeat("a", 100)
177 err
= Mkdir(name
, 0755)
179 t
.Fatalf("Could not mkdir %s: %s", name
, err
)
184 t
.Fatalf("Could not chdir %s: %s", name
, err
)
190 t
.Fatalf("Could not chdir %s: %s", prevDir
, err
)
193 err
= RemoveAll(startPath
)
195 t
.Errorf("RemoveAll could not remove long file path %s: %s", startPath
, err
)
199 func TestRemoveAllDot(t
*testing
.T
) {
200 prevDir
, err
:= Getwd()
202 t
.Fatalf("Could not get wd: %s", err
)
204 tempDir
, err
:= os
.MkdirTemp("", "TestRemoveAllDot-")
206 t
.Fatalf("Could not create TempDir: %s", err
)
208 defer RemoveAll(tempDir
)
212 t
.Fatalf("Could not chdir to tempdir: %s", err
)
217 t
.Errorf("RemoveAll succeed to remove .")
222 t
.Fatalf("Could not chdir %s: %s", prevDir
, err
)
226 func TestRemoveAllDotDot(t
*testing
.T
) {
229 tempDir
:= t
.TempDir()
230 subdir
:= filepath
.Join(tempDir
, "x")
231 subsubdir
:= filepath
.Join(subdir
, "y")
232 if err
:= MkdirAll(subsubdir
, 0777); err
!= nil {
235 if err
:= RemoveAll(filepath
.Join(subsubdir
, "..")); err
!= nil {
238 for _
, dir
:= range []string{subsubdir
, subdir
} {
239 if _
, err
:= Stat(dir
); err
== nil {
240 t
.Errorf("%s: exists after RemoveAll", dir
)
246 func TestRemoveReadOnlyDir(t
*testing
.T
) {
249 tempDir
:= t
.TempDir()
250 subdir
:= filepath
.Join(tempDir
, "x")
251 if err
:= Mkdir(subdir
, 0); err
!= nil {
255 // If an error occurs make it more likely that removing the
256 // temporary directory will succeed.
257 defer Chmod(subdir
, 0777)
259 if err
:= RemoveAll(subdir
); err
!= nil {
263 if _
, err
:= Stat(subdir
); err
== nil {
264 t
.Error("subdirectory was not removed")
269 func TestRemoveAllButReadOnlyAndPathError(t
*testing
.T
) {
270 switch runtime
.GOOS
{
271 case "js", "windows":
272 t
.Skipf("skipping test on %s", runtime
.GOOS
)
276 t
.Skip("skipping test when running as root")
281 tempDir
:= t
.TempDir()
293 readonly
:= []string{
296 inReadonly
:= func(d
string) bool {
297 for _
, ro
:= range readonly
{
301 dd
, _
:= filepath
.Split(d
)
302 if filepath
.Clean(dd
) == ro
{
309 for _
, dir
:= range dirs
{
310 if err
:= Mkdir(filepath
.Join(tempDir
, dir
), 0777); err
!= nil {
314 for _
, dir
:= range readonly
{
315 d
:= filepath
.Join(tempDir
, dir
)
316 if err
:= Chmod(d
, 0555); err
!= nil {
320 // Defer changing the mode back so that the deferred
321 // RemoveAll(tempDir) can succeed.
325 err
:= RemoveAll(tempDir
)
327 t
.Fatal("RemoveAll succeeded unexpectedly")
330 // The error should be of type *PathError.
331 // see issue 30491 for details.
332 if pathErr
, ok
:= err
.(*PathError
); ok
{
333 want
:= filepath
.Join(tempDir
, "b", "y")
334 if pathErr
.Path
!= want
{
335 t
.Errorf("RemoveAll(%q): err.Path=%q, want %q", tempDir
, pathErr
.Path
, want
)
338 t
.Errorf("RemoveAll(%q): error has type %T, want *fs.PathError", tempDir
, err
)
341 for _
, dir
:= range dirs
{
342 _
, err
:= Stat(filepath
.Join(tempDir
, dir
))
345 t
.Errorf("file %q was deleted but should still exist", dir
)
349 t
.Errorf("file %q still exists but should have been deleted", dir
)
355 func TestRemoveUnreadableDir(t
*testing
.T
) {
356 switch runtime
.GOOS
{
358 t
.Skipf("skipping test on %s", runtime
.GOOS
)
362 t
.Skip("skipping test when running as root")
367 tempDir
:= t
.TempDir()
368 target
:= filepath
.Join(tempDir
, "d0", "d1", "d2")
369 if err
:= MkdirAll(target
, 0755); err
!= nil {
372 if err
:= Chmod(target
, 0300); err
!= nil {
375 if err
:= RemoveAll(filepath
.Join(tempDir
, "d0")); err
!= nil {
381 func TestRemoveAllWithMoreErrorThanReqSize(t
*testing
.T
) {
383 t
.Skip("skipping in short mode")
386 tmpDir
:= t
.TempDir()
387 path
:= filepath
.Join(tmpDir
, "_TestRemoveAllWithMoreErrorThanReqSize_")
389 // Make directory with 1025 read-only files.
390 if err
:= MkdirAll(path
, 0777); err
!= nil {
391 t
.Fatalf("MkdirAll %q: %s", path
, err
)
393 for i
:= 0; i
< 1025; i
++ {
394 fpath
:= filepath
.Join(path
, fmt
.Sprintf("file%d", i
))
395 fd
, err
:= Create(fpath
)
397 t
.Fatalf("create %q: %s", fpath
, err
)
402 // Make the parent directory read-only. On some platforms, this is what
403 // prevents os.Remove from removing the files within that directory.
404 if err
:= Chmod(path
, 0555); err
!= nil {
407 defer Chmod(path
, 0755)
409 // This call should not hang, even on a platform that disallows file deletion
410 // from read-only directories.
411 err
:= RemoveAll(path
)
414 // On many platforms, root can remove files from read-only directories.
418 if runtime
.GOOS
== "windows" {
419 // Marking a directory as read-only in Windows does not prevent the RemoveAll
420 // from creating or removing files within it.
423 t
.Fatal("RemoveAll(<read-only directory>) = nil; want error")
426 dir
, err
:= Open(path
)
432 names
, _
:= dir
.Readdirnames(1025)
433 if len(names
) < 1025 {
434 t
.Fatalf("RemoveAll(<read-only directory>) unexpectedly removed %d read-only files from that directory", 1025-len(names
))