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.
17 var isReadonlyError = func(error) bool { return false }
19 func TestMkdirAll(t *testing.T) {
21 path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
22 err := MkdirAll(path, 0777)
24 t.Fatalf("MkdirAll %q: %s", path, err)
26 defer RemoveAll(tmpDir + "/_TestMkdirAll_")
28 // Already exists, should succeed.
29 err = MkdirAll(path, 0777)
31 t.Fatalf("MkdirAll %q (second time): %s", path, err)
35 fpath := path + "/file"
36 f, err := Create(fpath)
38 t.Fatalf("create %q: %s", fpath, err)
42 // Can't make directory named after file.
43 err = MkdirAll(fpath, 0777)
45 t.Fatalf("MkdirAll %q: no error", fpath)
47 perr, ok := err.(*PathError)
49 t.Fatalf("MkdirAll %q returned %T, not *PathError", fpath, err)
51 if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
52 t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", fpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
55 // Can't make subdirectory of file.
56 ffpath := fpath + "/subdir"
57 err = MkdirAll(ffpath, 0777)
59 t.Fatalf("MkdirAll %q: no error", ffpath)
61 perr, ok = err.(*PathError)
63 t.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath, err)
65 if filepath.Clean(perr.Path) != filepath.Clean(fpath) {
66 t.Fatalf("MkdirAll %q returned wrong error path: %q not %q", ffpath, filepath.Clean(perr.Path), filepath.Clean(fpath))
69 if runtime.GOOS == "windows" {
70 path := tmpDir + `\_TestMkdirAll_\dir\.\dir2\`
71 err := MkdirAll(path, 0777)
73 t.Fatalf("MkdirAll %q: %s", path, err)
78 func TestMkdirAllWithSymlink(t *testing.T) {
79 testenv.MustHaveSymlink(t)
81 tmpDir, err := ioutil.TempDir("", "TestMkdirAllWithSymlink-")
85 defer RemoveAll(tmpDir)
87 dir := tmpDir + "/dir"
88 err = Mkdir(dir, 0755)
90 t.Fatalf("Mkdir %s: %s", dir, err)
93 link := tmpDir + "/link"
94 err = Symlink("dir", link)
96 t.Fatalf("Symlink %s: %s", link, err)
100 err = MkdirAll(path, 0755)
102 t.Errorf("MkdirAll %q: %s", path, err)
106 func TestMkdirAllAtSlash(t *testing.T) {
107 switch runtime.GOOS {
108 case "android", "plan9", "windows":
109 t.Skipf("skipping on %s", runtime.GOOS)
111 switch runtime.GOARCH {
113 t.Skipf("skipping on darwin/%s, mkdir returns EPERM", runtime.GOARCH)
116 RemoveAll("/_go_os_test")
117 const dir = "/_go_os_test/dir"
118 err := MkdirAll(dir, 0777)
120 pathErr, ok := err.(*PathError)
121 // common for users not to be able to write to /
122 if ok && (pathErr.Err == syscall.EACCES || isReadonlyError(pathErr.Err)) {
123 t.Skipf("could not create %v: %v", dir, err)
125 t.Fatalf(`MkdirAll "/_go_os_test/dir": %v, %s`, err, pathErr.Err)
127 RemoveAll("/_go_os_test")