github.com/seh/gb@v0.4.4-0.20160724065125-065d2b2b1ba1/internal/fileutils/path_test.go (about) 1 // Copyright 2009 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. 4 package fileutils 5 6 import ( 7 "os" 8 "runtime" 9 "testing" 10 ) 11 12 func TestRemoveAll(t *testing.T) { 13 tmpDir := os.TempDir() 14 // Work directory. 15 path := tmpDir + "/_TestRemoveAll_" 16 fpath := path + "/file" 17 dpath := path + "/dir" 18 19 // Make directory with 1 file and remove. 20 if err := os.MkdirAll(path, 0777); err != nil { 21 t.Fatalf("MkdirAll %q: %s", path, err) 22 } 23 fd, err := os.Create(fpath) 24 if err != nil { 25 t.Fatalf("create %q: %s", fpath, err) 26 } 27 fd.Close() 28 if err = RemoveAll(path); err != nil { 29 t.Fatalf("RemoveAll %q (first): %s", path, err) 30 } 31 if _, err = os.Lstat(path); err == nil { 32 t.Fatalf("Lstat %q succeeded after RemoveAll (first)", path) 33 } 34 35 // Make directory with file and subdirectory and remove. 36 if err = os.MkdirAll(dpath, 0777); err != nil { 37 t.Fatalf("MkdirAll %q: %s", dpath, err) 38 } 39 fd, err = os.Create(fpath) 40 if err != nil { 41 t.Fatalf("create %q: %s", fpath, err) 42 } 43 fd.Close() 44 fd, err = os.Create(dpath + "/file") 45 if err != nil { 46 t.Fatalf("create %q: %s", fpath, err) 47 } 48 fd.Close() 49 if err = RemoveAll(path); err != nil { 50 t.Fatalf("RemoveAll %q (second): %s", path, err) 51 } 52 if _, err := os.Lstat(path); err == nil { 53 t.Fatalf("Lstat %q succeeded after RemoveAll (second)", path) 54 } 55 56 // Determine if we should run the following test. 57 testit := true 58 if runtime.GOOS == "windows" { 59 // Chmod is not supported under windows. 60 testit = false 61 } else { 62 // Test fails as root. 63 testit = os.Getuid() != 0 64 } 65 if testit { 66 // Make directory with file and subdirectory and trigger error. 67 if err = os.MkdirAll(dpath, 0777); err != nil { 68 t.Fatalf("MkdirAll %q: %s", dpath, err) 69 } 70 71 for _, s := range []string{fpath, dpath + "/file1", path + "/zzz"} { 72 fd, err = os.Create(s) 73 if err != nil { 74 t.Fatalf("create %q: %s", s, err) 75 } 76 fd.Close() 77 } 78 if err = os.Chmod(dpath, 0); err != nil { 79 t.Fatalf("Chmod %q 0: %s", dpath, err) 80 } 81 82 // No error checking here: either RemoveAll 83 // will or won't be able to remove dpath; 84 // either way we want to see if it removes fpath 85 // and path/zzz. Reasons why RemoveAll might 86 // succeed in removing dpath as well include: 87 // * running as root 88 // * running on a file system without permissions (FAT) 89 RemoveAll(path) 90 os.Chmod(dpath, 0777) 91 92 for _, s := range []string{fpath, path + "/zzz"} { 93 if _, err = os.Lstat(s); err == nil { 94 t.Fatalf("Lstat %q succeeded after partial RemoveAll", s) 95 } 96 } 97 } 98 if err = RemoveAll(path); err != nil { 99 t.Fatalf("RemoveAll %q after partial RemoveAll: %s", path, err) 100 } 101 if _, err = os.Lstat(path); err == nil { 102 t.Fatalf("Lstat %q succeeded after RemoveAll (final)", path) 103 } 104 }