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.
16 func TestMkdirAll(t
*testing
.T
) {
18 path
:= tmpDir
+ "/_TestMkdirAll_/dir/./dir2"
19 err
:= MkdirAll(path
, 0777)
21 t
.Fatalf("MkdirAll %q: %s", path
, err
)
23 defer RemoveAll(tmpDir
+ "/_TestMkdirAll_")
25 // Already exists, should succeed.
26 err
= MkdirAll(path
, 0777)
28 t
.Fatalf("MkdirAll %q (second time): %s", path
, err
)
32 fpath
:= path
+ "/file"
33 f
, err
:= Create(fpath
)
35 t
.Fatalf("create %q: %s", fpath
, err
)
39 // Can't make directory named after file.
40 err
= MkdirAll(fpath
, 0777)
42 t
.Fatalf("MkdirAll %q: no error", fpath
)
44 perr
, ok
:= err
.(*PathError
)
46 t
.Fatalf("MkdirAll %q returned %T, not *PathError", fpath
, err
)
48 if filepath
.Clean(perr
.Path
) != filepath
.Clean(fpath
) {
49 t
.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath
, filepath
.Clean(perr
.Path
), filepath
.Clean(fpath
))
52 // Can't make subdirectory of file.
53 ffpath
:= fpath
+ "/subdir"
54 err
= MkdirAll(ffpath
, 0777)
56 t
.Fatalf("MkdirAll %q: no error", ffpath
)
58 perr
, ok
= err
.(*PathError
)
60 t
.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath
, err
)
62 if filepath
.Clean(perr
.Path
) != filepath
.Clean(fpath
) {
63 t
.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath
, filepath
.Clean(perr
.Path
), filepath
.Clean(fpath
))
66 if runtime
.GOOS
== "windows" {
67 path
:= tmpDir
+ `\_TestMkdirAll_\dir\.\dir2\`
68 err
:= MkdirAll(path
, 0777)
70 t
.Fatalf("MkdirAll %q: %s", path
, err
)
75 func TestRemoveAll(t
*testing
.T
) {
78 path
:= tmpDir
+ "/_TestRemoveAll_"
79 fpath
:= path
+ "/file"
80 dpath
:= path
+ "/dir"
82 // Make directory with 1 file and remove.
83 if err
:= MkdirAll(path
, 0777); err
!= nil {
84 t
.Fatalf("MkdirAll %q: %s", path
, err
)
86 fd
, err
:= Create(fpath
)
88 t
.Fatalf("create %q: %s", fpath
, err
)
91 if err
= RemoveAll(path
); err
!= nil {
92 t
.Fatalf("RemoveAll %q (first): %s", path
, err
)
94 if _
, err
= Lstat(path
); err
== nil {
95 t
.Fatalf("Lstat %q succeeded after RemoveAll (first)", path
)
98 // Make directory with file and subdirectory and remove.
99 if err
= MkdirAll(dpath
, 0777); err
!= nil {
100 t
.Fatalf("MkdirAll %q: %s", dpath
, err
)
102 fd
, err
= Create(fpath
)
104 t
.Fatalf("create %q: %s", fpath
, err
)
107 fd
, err
= Create(dpath
+ "/file")
109 t
.Fatalf("create %q: %s", fpath
, err
)
112 if err
= RemoveAll(path
); err
!= nil {
113 t
.Fatalf("RemoveAll %q (second): %s", path
, err
)
115 if _
, err
:= Lstat(path
); err
== nil {
116 t
.Fatalf("Lstat %q succeeded after RemoveAll (second)", path
)
119 // Determine if we should run the following test.
121 if runtime
.GOOS
== "windows" {
122 // Chmod is not supported under windows.
125 // Test fails as root.
126 testit
= Getuid() != 0
129 // Make directory with file and subdirectory and trigger error.
130 if err
= MkdirAll(dpath
, 0777); err
!= nil {
131 t
.Fatalf("MkdirAll %q: %s", dpath
, err
)
134 for _
, s
:= range []string{fpath
, dpath
+ "/file1", path
+ "/zzz"} {
137 t
.Fatalf("create %q: %s", s
, err
)
141 if err
= Chmod(dpath
, 0); err
!= nil {
142 t
.Fatalf("Chmod %q 0: %s", dpath
, err
)
145 // No error checking here: either RemoveAll
146 // will or won't be able to remove dpath;
147 // either way we want to see if it removes fpath
148 // and path/zzz. Reasons why RemoveAll might
149 // succeed in removing dpath as well include:
151 // * running on a file system without permissions (FAT)
155 for _
, s
:= range []string{fpath
, path
+ "/zzz"} {
156 if _
, err
= Lstat(s
); err
== nil {
157 t
.Fatalf("Lstat %q succeeded after partial RemoveAll", s
)
161 if err
= RemoveAll(path
); err
!= nil {
162 t
.Fatalf("RemoveAll %q after partial RemoveAll: %s", path
, err
)
164 if _
, err
= Lstat(path
); err
== nil {
165 t
.Fatalf("Lstat %q succeeded after RemoveAll (final)", path
)
169 func TestMkdirAllWithSymlink(t
*testing
.T
) {
170 if runtime
.GOOS
== "windows" || runtime
.GOOS
== "plan9" {
171 t
.Skip("Skipping test: symlinks don't exist under Windows/Plan 9")
174 tmpDir
, err
:= ioutil
.TempDir("", "TestMkdirAllWithSymlink-")
178 defer RemoveAll(tmpDir
)
180 dir
:= tmpDir
+ "/dir"
181 err
= Mkdir(dir
, 0755)
183 t
.Fatalf("Mkdir %s: %s", dir
, err
)
186 link
:= tmpDir
+ "/link"
187 err
= Symlink("dir", link
)
189 t
.Fatalf("Symlink %s: %s", link
, err
)
192 path
:= link
+ "/foo"
193 err
= MkdirAll(path
, 0755)
195 t
.Errorf("MkdirAll %q: %s", path
, err
)
199 func TestMkdirAllAtSlash(t
*testing
.T
) {
200 if runtime
.GOOS
== "windows" || runtime
.GOOS
== "plan9" {
203 RemoveAll("/_go_os_test")
204 err
:= MkdirAll("/_go_os_test/dir", 0777)
206 pathErr
, ok
:= err
.(*PathError
)
207 // common for users not to be able to write to /
208 if ok
&& pathErr
.Err
== syscall
.EACCES
{
211 t
.Fatalf(`MkdirAll "/_go_os_test/dir": %v`, err
)
213 RemoveAll("/_go_os_test")