c++: remove Concepts TS code
[official-gcc.git] / libgo / go / os / removeall_test.go
blob08dfdd8ec0644703cf505b973b21a58c5396cbec
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.
5 package os_test
7 import (
8 "fmt"
9 "os"
10 . "os"
11 "path/filepath"
12 "runtime"
13 "strings"
14 "testing"
17 func TestRemoveAll(t *testing.T) {
18 tmpDir := t.TempDir()
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)
30 if err != nil {
31 t.Fatalf("create %q: %s", file, err)
33 fd.Close()
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)
46 if err != nil {
47 t.Fatalf("create %q: %s", fpath, err)
49 fd.Close()
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)
62 if err != nil {
63 t.Fatalf("create %q: %s", fpath, err)
65 fd.Close()
66 fd, err = Create(dpath + "/file")
67 if err != nil {
68 t.Fatalf("create %q: %s", fpath, err)
70 fd.Close()
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"} {
86 fd, err = Create(s)
87 if err != nil {
88 t.Fatalf("create %q: %s", s, err)
90 fd.Close()
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:
101 // * running as root
102 // * running on a file system without permissions (FAT)
103 RemoveAll(path)
104 Chmod(dpath, 0777)
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) {
122 if testing.Short() {
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)
136 if err != nil {
137 t.Fatalf("create %q: %s", fpath, err)
139 fd.Close()
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":
152 break
153 default:
154 t.Skip("skipping for not implemented platforms")
157 prevDir, err := Getwd()
158 if err != nil {
159 t.Fatalf("Could not get wd: %s", err)
162 startPath, err := os.MkdirTemp("", "TestRemoveAllLongPath-")
163 if err != nil {
164 t.Fatalf("Could not create TempDir: %s", err)
166 defer RemoveAll(startPath)
168 err = Chdir(startPath)
169 if err != nil {
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)
178 if err != nil {
179 t.Fatalf("Could not mkdir %s: %s", name, err)
182 err = Chdir(name)
183 if err != nil {
184 t.Fatalf("Could not chdir %s: %s", name, err)
188 err = Chdir(prevDir)
189 if err != nil {
190 t.Fatalf("Could not chdir %s: %s", prevDir, err)
193 err = RemoveAll(startPath)
194 if err != nil {
195 t.Errorf("RemoveAll could not remove long file path %s: %s", startPath, err)
199 func TestRemoveAllDot(t *testing.T) {
200 prevDir, err := Getwd()
201 if err != nil {
202 t.Fatalf("Could not get wd: %s", err)
204 tempDir, err := os.MkdirTemp("", "TestRemoveAllDot-")
205 if err != nil {
206 t.Fatalf("Could not create TempDir: %s", err)
208 defer RemoveAll(tempDir)
210 err = Chdir(tempDir)
211 if err != nil {
212 t.Fatalf("Could not chdir to tempdir: %s", err)
215 err = RemoveAll(".")
216 if err == nil {
217 t.Errorf("RemoveAll succeed to remove .")
220 err = Chdir(prevDir)
221 if err != nil {
222 t.Fatalf("Could not chdir %s: %s", prevDir, err)
226 func TestRemoveAllDotDot(t *testing.T) {
227 t.Parallel()
229 tempDir := t.TempDir()
230 subdir := filepath.Join(tempDir, "x")
231 subsubdir := filepath.Join(subdir, "y")
232 if err := MkdirAll(subsubdir, 0777); err != nil {
233 t.Fatal(err)
235 if err := RemoveAll(filepath.Join(subsubdir, "..")); err != nil {
236 t.Error(err)
238 for _, dir := range []string{subsubdir, subdir} {
239 if _, err := Stat(dir); err == nil {
240 t.Errorf("%s: exists after RemoveAll", dir)
245 // Issue #29178.
246 func TestRemoveReadOnlyDir(t *testing.T) {
247 t.Parallel()
249 tempDir := t.TempDir()
250 subdir := filepath.Join(tempDir, "x")
251 if err := Mkdir(subdir, 0); err != nil {
252 t.Fatal(err)
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 {
260 t.Fatal(err)
263 if _, err := Stat(subdir); err == nil {
264 t.Error("subdirectory was not removed")
268 // Issue #29983.
269 func TestRemoveAllButReadOnlyAndPathError(t *testing.T) {
270 switch runtime.GOOS {
271 case "js", "windows":
272 t.Skipf("skipping test on %s", runtime.GOOS)
275 if Getuid() == 0 {
276 t.Skip("skipping test when running as root")
279 t.Parallel()
281 tempDir := t.TempDir()
282 dirs := []string{
283 "a",
284 "a/x",
285 "a/x/1",
286 "b",
287 "b/y",
288 "b/y/2",
289 "c",
290 "c/z",
291 "c/z/3",
293 readonly := []string{
294 "b",
296 inReadonly := func(d string) bool {
297 for _, ro := range readonly {
298 if d == ro {
299 return true
301 dd, _ := filepath.Split(d)
302 if filepath.Clean(dd) == ro {
303 return true
306 return false
309 for _, dir := range dirs {
310 if err := Mkdir(filepath.Join(tempDir, dir), 0777); err != nil {
311 t.Fatal(err)
314 for _, dir := range readonly {
315 d := filepath.Join(tempDir, dir)
316 if err := Chmod(d, 0555); err != nil {
317 t.Fatal(err)
320 // Defer changing the mode back so that the deferred
321 // RemoveAll(tempDir) can succeed.
322 defer Chmod(d, 0777)
325 err := RemoveAll(tempDir)
326 if err == nil {
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)
337 } else {
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))
343 if inReadonly(dir) {
344 if err != nil {
345 t.Errorf("file %q was deleted but should still exist", dir)
347 } else {
348 if err == nil {
349 t.Errorf("file %q still exists but should have been deleted", dir)
355 func TestRemoveUnreadableDir(t *testing.T) {
356 switch runtime.GOOS {
357 case "js":
358 t.Skipf("skipping test on %s", runtime.GOOS)
361 if Getuid() == 0 {
362 t.Skip("skipping test when running as root")
365 t.Parallel()
367 tempDir := t.TempDir()
368 target := filepath.Join(tempDir, "d0", "d1", "d2")
369 if err := MkdirAll(target, 0755); err != nil {
370 t.Fatal(err)
372 if err := Chmod(target, 0300); err != nil {
373 t.Fatal(err)
375 if err := RemoveAll(filepath.Join(tempDir, "d0")); err != nil {
376 t.Fatal(err)
380 // Issue 29921
381 func TestRemoveAllWithMoreErrorThanReqSize(t *testing.T) {
382 if testing.Short() {
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)
396 if err != nil {
397 t.Fatalf("create %q: %s", fpath, err)
399 fd.Close()
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 {
405 t.Fatal(err)
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)
413 if Getuid() == 0 {
414 // On many platforms, root can remove files from read-only directories.
415 return
417 if err == nil {
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.
421 return
423 t.Fatal("RemoveAll(<read-only directory>) = nil; want error")
426 dir, err := Open(path)
427 if err != nil {
428 t.Fatal(err)
430 defer dir.Close()
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))