Daily bump.
[official-gcc.git] / libgo / go / os / path_test.go
blob27abf59826f729469869df9a700fee8c3f13ce82
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.
5 package os_test
7 import (
8 "io/ioutil"
9 . "os"
10 "path/filepath"
11 "runtime"
12 "syscall"
13 "testing"
16 func TestMkdirAll(t *testing.T) {
17 tmpDir := TempDir()
18 path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
19 err := MkdirAll(path, 0777)
20 if err != nil {
21 t.Fatalf("MkdirAll %q: %s", path, err)
23 defer RemoveAll(tmpDir + "/_TestMkdirAll_")
25 // Already exists, should succeed.
26 err = MkdirAll(path, 0777)
27 if err != nil {
28 t.Fatalf("MkdirAll %q (second time): %s", path, err)
31 // Make file.
32 fpath := path + "/file"
33 f, err := Create(fpath)
34 if err != nil {
35 t.Fatalf("create %q: %s", fpath, err)
37 defer f.Close()
39 // Can't make directory named after file.
40 err = MkdirAll(fpath, 0777)
41 if err == nil {
42 t.Fatalf("MkdirAll %q: no error", fpath)
44 perr, ok := err.(*PathError)
45 if !ok {
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)
55 if err == nil {
56 t.Fatalf("MkdirAll %q: no error", ffpath)
58 perr, ok = err.(*PathError)
59 if !ok {
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)
69 if err != nil {
70 t.Fatalf("MkdirAll %q: %s", path, err)
75 func TestRemoveAll(t *testing.T) {
76 tmpDir := TempDir()
77 // Work directory.
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)
87 if err != nil {
88 t.Fatalf("create %q: %s", fpath, err)
90 fd.Close()
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)
103 if err != nil {
104 t.Fatalf("create %q: %s", fpath, err)
106 fd.Close()
107 fd, err = Create(dpath + "/file")
108 if err != nil {
109 t.Fatalf("create %q: %s", fpath, err)
111 fd.Close()
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.
120 testit := true
121 if runtime.GOOS == "windows" {
122 // Chmod is not supported under windows.
123 testit = false
124 } else {
125 // Test fails as root.
126 testit = Getuid() != 0
128 if testit {
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"} {
135 fd, err = Create(s)
136 if err != nil {
137 t.Fatalf("create %q: %s", s, err)
139 fd.Close()
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:
150 // * running as root
151 // * running on a file system without permissions (FAT)
152 RemoveAll(path)
153 Chmod(dpath, 0777)
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-")
175 if err != nil {
176 t.Fatal(err)
178 defer RemoveAll(tmpDir)
180 dir := tmpDir + "/dir"
181 err = Mkdir(dir, 0755)
182 if err != nil {
183 t.Fatalf("Mkdir %s: %s", dir, err)
186 link := tmpDir + "/link"
187 err = Symlink("dir", link)
188 if err != nil {
189 t.Fatalf("Symlink %s: %s", link, err)
192 path := link + "/foo"
193 err = MkdirAll(path, 0755)
194 if err != nil {
195 t.Errorf("MkdirAll %q: %s", path, err)
199 func TestMkdirAllAtSlash(t *testing.T) {
200 if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
201 return
203 RemoveAll("/_go_os_test")
204 err := MkdirAll("/_go_os_test/dir", 0777)
205 if err != nil {
206 pathErr, ok := err.(*PathError)
207 // common for users not to be able to write to /
208 if ok && pathErr.Err == syscall.EACCES {
209 return
211 t.Fatalf(`MkdirAll "/_go_os_test/dir": %v`, err)
213 RemoveAll("/_go_os_test")