var-tracking.c (vt_add_function_parameter): Adjust for VEC changes.
[official-gcc.git] / libgo / go / os / path_test.go
blobc1e3fb35436b96199956e329330d78c5f9856cfa
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 . "os"
9 "path/filepath"
10 "runtime"
11 "syscall"
12 "testing"
15 func TestMkdirAll(t *testing.T) {
16 tmpDir := TempDir()
17 path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
18 err := MkdirAll(path, 0777)
19 if err != nil {
20 t.Fatalf("MkdirAll %q: %s", path, err)
22 defer RemoveAll(tmpDir + "/_TestMkdirAll_")
24 // Already exists, should succeed.
25 err = MkdirAll(path, 0777)
26 if err != nil {
27 t.Fatalf("MkdirAll %q (second time): %s", path, err)
30 // Make file.
31 fpath := path + "/file"
32 f, err := Create(fpath)
33 if err != nil {
34 t.Fatalf("create %q: %s", fpath, err)
36 defer f.Close()
38 // Can't make directory named after file.
39 err = MkdirAll(fpath, 0777)
40 if err == nil {
41 t.Fatalf("MkdirAll %q: no error", fpath)
43 perr, ok := err.(*PathError)
44 if !ok {
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)
54 if err == nil {
55 t.Fatalf("MkdirAll %q: no error", ffpath)
57 perr, ok = err.(*PathError)
58 if !ok {
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)
68 if err != nil {
69 t.Fatalf("MkdirAll %q: %s", path, err)
74 func TestRemoveAll(t *testing.T) {
75 tmpDir := TempDir()
76 // Work directory.
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)
86 if err != nil {
87 t.Fatalf("create %q: %s", fpath, err)
89 fd.Close()
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)
102 if err != nil {
103 t.Fatalf("create %q: %s", fpath, err)
105 fd.Close()
106 fd, err = Create(dpath + "/file")
107 if err != nil {
108 t.Fatalf("create %q: %s", fpath, err)
110 fd.Close()
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.
119 testit := true
120 if runtime.GOOS == "windows" {
121 // Chmod is not supported under windows.
122 testit = false
123 } else {
124 // Test fails as root.
125 testit = Getuid() != 0
127 if testit {
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"} {
134 fd, err = Create(s)
135 if err != nil {
136 t.Fatalf("create %q: %s", s, err)
138 fd.Close()
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:
149 // * running as root
150 // * running on a file system without permissions (FAT)
151 RemoveAll(path)
152 Chmod(dpath, 0777)
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")
171 return
174 tmpDir := TempDir()
175 dir := tmpDir + "/dir"
176 err := Mkdir(dir, 0755)
177 if err != nil {
178 t.Fatalf("Mkdir %s: %s", dir, err)
180 defer RemoveAll(dir)
182 link := tmpDir + "/link"
183 err = Symlink("dir", link)
184 if err != nil {
185 t.Fatalf("Symlink %s: %s", link, err)
187 defer RemoveAll(link)
189 path := link + "/foo"
190 err = MkdirAll(path, 0755)
191 if err != nil {
192 t.Errorf("MkdirAll %q: %s", path, err)
196 func TestMkdirAllAtSlash(t *testing.T) {
197 if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
198 return
200 RemoveAll("/_go_os_test")
201 err := MkdirAll("/_go_os_test/dir", 0777)
202 if err != nil {
203 pathErr, ok := err.(*PathError)
204 // common for users not to be able to write to /
205 if ok && pathErr.Err == syscall.EACCES {
206 return
208 t.Fatalf(`MkdirAll "/_go_os_test/dir": %v`, err)
210 RemoveAll("/_go_os_test")