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 var isReadonlyError
= func(error
) bool { return false }
18 func TestMkdirAll(t
*testing
.T
) {
20 path
:= tmpDir
+ "/_TestMkdirAll_/dir/./dir2"
21 err
:= MkdirAll(path
, 0777)
23 t
.Fatalf("MkdirAll %q: %s", path
, err
)
25 defer RemoveAll(tmpDir
+ "/_TestMkdirAll_")
27 // Already exists, should succeed.
28 err
= MkdirAll(path
, 0777)
30 t
.Fatalf("MkdirAll %q (second time): %s", path
, err
)
34 fpath
:= path
+ "/file"
35 f
, err
:= Create(fpath
)
37 t
.Fatalf("create %q: %s", fpath
, err
)
41 // Can't make directory named after file.
42 err
= MkdirAll(fpath
, 0777)
44 t
.Fatalf("MkdirAll %q: no error", fpath
)
46 perr
, ok
:= err
.(*PathError
)
48 t
.Fatalf("MkdirAll %q returned %T, not *PathError", fpath
, err
)
50 if filepath
.Clean(perr
.Path
) != filepath
.Clean(fpath
) {
51 t
.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath
, filepath
.Clean(perr
.Path
), filepath
.Clean(fpath
))
54 // Can't make subdirectory of file.
55 ffpath
:= fpath
+ "/subdir"
56 err
= MkdirAll(ffpath
, 0777)
58 t
.Fatalf("MkdirAll %q: no error", ffpath
)
60 perr
, ok
= err
.(*PathError
)
62 t
.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath
, err
)
64 if filepath
.Clean(perr
.Path
) != filepath
.Clean(fpath
) {
65 t
.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath
, filepath
.Clean(perr
.Path
), filepath
.Clean(fpath
))
68 if runtime
.GOOS
== "windows" {
69 path
:= tmpDir
+ `\_TestMkdirAll_\dir\.\dir2\`
70 err
:= MkdirAll(path
, 0777)
72 t
.Fatalf("MkdirAll %q: %s", path
, err
)
77 func TestMkdirAllWithSymlink(t
*testing
.T
) {
78 testenv
.MustHaveSymlink(t
)
81 dir
:= tmpDir
+ "/dir"
82 if err
:= Mkdir(dir
, 0755); err
!= nil {
83 t
.Fatalf("Mkdir %s: %s", dir
, err
)
86 link
:= tmpDir
+ "/link"
87 if err
:= Symlink("dir", link
); err
!= nil {
88 t
.Fatalf("Symlink %s: %s", link
, err
)
92 if err
:= MkdirAll(path
, 0755); err
!= nil {
93 t
.Errorf("MkdirAll %q: %s", path
, err
)
97 func TestMkdirAllAtSlash(t
*testing
.T
) {
99 case "android", "ios", "plan9", "windows":
100 t
.Skipf("skipping on %s", runtime
.GOOS
)
102 RemoveAll("/_go_os_test")
103 const dir
= "/_go_os_test/dir"
104 err
:= MkdirAll(dir
, 0777)
106 pathErr
, ok
:= err
.(*PathError
)
107 // common for users not to be able to write to /
108 if ok
&& (pathErr
.Err
== syscall
.EACCES ||
isReadonlyError(pathErr
.Err
)) {
109 t
.Skipf("could not create %v: %v", dir
, err
)
111 t
.Fatalf(`MkdirAll "/_go_os_test/dir": %v, %s`, err
, pathErr
.Err
)
113 RemoveAll("/_go_os_test")