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.
15 func TestMkdirAll(t
*testing
.T
) {
17 path
:= tmpDir
+ "/_TestMkdirAll_/dir/./dir2"
18 err
:= MkdirAll(path
, 0777)
20 t
.Fatalf("MkdirAll %q: %s", path
, err
)
22 defer RemoveAll(tmpDir
+ "/_TestMkdirAll_")
24 // Already exists, should succeed.
25 err
= MkdirAll(path
, 0777)
27 t
.Fatalf("MkdirAll %q (second time): %s", path
, err
)
31 fpath
:= path
+ "/file"
32 f
, err
:= Create(fpath
)
34 t
.Fatalf("create %q: %s", fpath
, err
)
38 // Can't make directory named after file.
39 err
= MkdirAll(fpath
, 0777)
41 t
.Fatalf("MkdirAll %q: no error", fpath
)
43 perr
, ok
:= err
.(*PathError
)
45 t
.Fatalf("MkdirAll %q returned %T, not *PathError", fpath
, err
)
47 if filepath
.Clean(perr
.Path
) != filepath
.Clean(fpath
) {
48 t
.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath
, filepath
.Clean(perr
.Path
), filepath
.Clean(fpath
))
51 // Can't make subdirectory of file.
52 ffpath
:= fpath
+ "/subdir"
53 err
= MkdirAll(ffpath
, 0777)
55 t
.Fatalf("MkdirAll %q: no error", ffpath
)
57 perr
, ok
= err
.(*PathError
)
59 t
.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath
, err
)
61 if filepath
.Clean(perr
.Path
) != filepath
.Clean(fpath
) {
62 t
.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath
, filepath
.Clean(perr
.Path
), filepath
.Clean(fpath
))
65 if runtime
.GOOS
== "windows" {
66 path
:= tmpDir
+ `\_TestMkdirAll_\dir\.\dir2\`
67 err
:= MkdirAll(path
, 0777)
69 t
.Fatalf("MkdirAll %q: %s", path
, err
)
74 func TestRemoveAll(t
*testing
.T
) {
77 path
:= tmpDir
+ "/_TestRemoveAll_"
78 fpath
:= path
+ "/file"
79 dpath
:= path
+ "/dir"
81 // Make directory with 1 file and remove.
82 if err
:= MkdirAll(path
, 0777); err
!= nil {
83 t
.Fatalf("MkdirAll %q: %s", path
, err
)
85 fd
, err
:= Create(fpath
)
87 t
.Fatalf("create %q: %s", fpath
, err
)
90 if err
= RemoveAll(path
); err
!= nil {
91 t
.Fatalf("RemoveAll %q (first): %s", path
, err
)
93 if _
, err
:= Lstat(path
); err
== nil {
94 t
.Fatalf("Lstat %q succeeded after RemoveAll (first)", path
)
97 // Make directory with file and subdirectory and remove.
98 if err
= MkdirAll(dpath
, 0777); err
!= nil {
99 t
.Fatalf("MkdirAll %q: %s", dpath
, err
)
101 fd
, err
= Create(fpath
)
103 t
.Fatalf("create %q: %s", fpath
, err
)
106 fd
, err
= Create(dpath
+ "/file")
108 t
.Fatalf("create %q: %s", fpath
, err
)
111 if err
= RemoveAll(path
); err
!= nil {
112 t
.Fatalf("RemoveAll %q (second): %s", path
, err
)
114 if _
, err
:= Lstat(path
); err
== nil {
115 t
.Fatalf("Lstat %q succeeded after RemoveAll (second)", path
)
118 // Determine if we should run the following test.
120 if runtime
.GOOS
== "windows" {
121 // Chmod is not supported under windows.
124 // Test fails as root.
125 testit
= Getuid() != 0
128 // Make directory with file and subdirectory and trigger error.
129 if err
= MkdirAll(dpath
, 0777); err
!= nil {
130 t
.Fatalf("MkdirAll %q: %s", dpath
, err
)
133 for _
, s
:= range []string{fpath
, dpath
+ "/file1", path
+ "/zzz"} {
136 t
.Fatalf("create %q: %s", s
, err
)
140 if err
= Chmod(dpath
, 0); err
!= nil {
141 t
.Fatalf("Chmod %q 0: %s", dpath
, err
)
144 // No error checking here: either RemoveAll
145 // will or won't be able to remove dpath;
146 // either way we want to see if it removes fpath
147 // and path/zzz. Reasons why RemoveAll might
148 // succeed in removing dpath as well include:
150 // * running on a file system without permissions (FAT)
154 for _
, s
:= range []string{fpath
, path
+ "/zzz"} {
155 if _
, err
:= Lstat(s
); err
== nil {
156 t
.Fatalf("Lstat %q succeeded after partial RemoveAll", s
)
160 if err
= RemoveAll(path
); err
!= nil {
161 t
.Fatalf("RemoveAll %q after partial RemoveAll: %s", path
, err
)
163 if _
, err
:= Lstat(path
); err
== nil {
164 t
.Fatalf("Lstat %q succeeded after RemoveAll (final)", path
)
168 func TestMkdirAllWithSymlink(t
*testing
.T
) {
169 if runtime
.GOOS
== "windows" || runtime
.GOOS
== "plan9" {
170 t
.Log("Skipping test: symlinks don't exist under Windows/Plan 9")
175 dir
:= tmpDir
+ "/dir"
176 err
:= Mkdir(dir
, 0755)
178 t
.Fatalf("Mkdir %s: %s", dir
, err
)
182 link
:= tmpDir
+ "/link"
183 err
= Symlink("dir", link
)
185 t
.Fatalf("Symlink %s: %s", link
, err
)
187 defer RemoveAll(link
)
189 path
:= link
+ "/foo"
190 err
= MkdirAll(path
, 0755)
192 t
.Errorf("MkdirAll %q: %s", path
, err
)
196 func TestMkdirAllAtSlash(t
*testing
.T
) {
197 if runtime
.GOOS
== "windows" || runtime
.GOOS
== "plan9" {
200 RemoveAll("/_go_os_test")
201 err
:= MkdirAll("/_go_os_test/dir", 0777)
203 pathErr
, ok
:= err
.(*PathError
)
204 // common for users not to be able to write to /
205 if ok
&& pathErr
.Err
== syscall
.EACCES
{
208 t
.Fatalf(`MkdirAll "/_go_os_test/dir": %v`, err
)
210 RemoveAll("/_go_os_test")