OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [os/] [path_test.go] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package os_test
6
 
7
import (
8
        . "os"
9
        "path/filepath"
10
        "runtime"
11
        "testing"
12
)
13
 
14
func TestMkdirAll(t *testing.T) {
15
        // Create new dir, in _test so it will get
16
        // cleaned up by make if not by us.
17
        path := "_test/_TestMkdirAll_/dir/./dir2"
18
        err := MkdirAll(path, 0777)
19
        if err != nil {
20
                t.Fatalf("MkdirAll %q: %s", path, err)
21
        }
22
        defer RemoveAll("_test/_TestMkdirAll_")
23
 
24
        // Already exists, should succeed.
25
        err = MkdirAll(path, 0777)
26
        if err != nil {
27
                t.Fatalf("MkdirAll %q (second time): %s", path, err)
28
        }
29
 
30
        // Make file.
31
        fpath := path + "/file"
32
        f, err := Create(fpath)
33
        if err != nil {
34
                t.Fatalf("create %q: %s", fpath, err)
35
        }
36
        defer f.Close()
37
 
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)
42
        }
43
        perr, ok := err.(*PathError)
44
        if !ok {
45
                t.Fatalf("MkdirAll %q returned %T, not *PathError", fpath, err)
46
        }
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))
49
        }
50
 
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)
56
        }
57
        perr, ok = err.(*PathError)
58
        if !ok {
59
                t.Fatalf("MkdirAll %q returned %T, not *PathError", ffpath, err)
60
        }
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))
63
        }
64
 
65
        if runtime.GOOS == "windows" {
66
                path := `_test\_TestMkdirAll_\dir\.\dir2\`
67
                err := MkdirAll(path, 0777)
68
                if err != nil {
69
                        t.Fatalf("MkdirAll %q: %s", path, err)
70
                }
71
        }
72
}
73
 
74
func TestRemoveAll(t *testing.T) {
75
        // Work directory.
76
        path := "_test/_TestRemoveAll_"
77
        fpath := path + "/file"
78
        dpath := path + "/dir"
79
 
80
        // Make directory with 1 file and remove.
81
        if err := MkdirAll(path, 0777); err != nil {
82
                t.Fatalf("MkdirAll %q: %s", path, err)
83
        }
84
        fd, err := Create(fpath)
85
        if err != nil {
86
                t.Fatalf("create %q: %s", fpath, err)
87
        }
88
        fd.Close()
89
        if err = RemoveAll(path); err != nil {
90
                t.Fatalf("RemoveAll %q (first): %s", path, err)
91
        }
92
        if _, err := Lstat(path); err == nil {
93
                t.Fatalf("Lstat %q succeeded after RemoveAll (first)", path)
94
        }
95
 
96
        // Make directory with file and subdirectory and remove.
97
        if err = MkdirAll(dpath, 0777); err != nil {
98
                t.Fatalf("MkdirAll %q: %s", dpath, err)
99
        }
100
        fd, err = Create(fpath)
101
        if err != nil {
102
                t.Fatalf("create %q: %s", fpath, err)
103
        }
104
        fd.Close()
105
        fd, err = Create(dpath + "/file")
106
        if err != nil {
107
                t.Fatalf("create %q: %s", fpath, err)
108
        }
109
        fd.Close()
110
        if err = RemoveAll(path); err != nil {
111
                t.Fatalf("RemoveAll %q (second): %s", path, err)
112
        }
113
        if _, err := Lstat(path); err == nil {
114
                t.Fatalf("Lstat %q succeeded after RemoveAll (second)", path)
115
        }
116
 
117
        // Determine if we should run the following test.
118
        testit := true
119
        if runtime.GOOS == "windows" {
120
                // Chmod is not supported under windows.
121
                testit = false
122
        } else {
123
                // Test fails as root.
124
                testit = Getuid() != 0
125
        }
126
        if testit {
127
                // Make directory with file and subdirectory and trigger error.
128
                if err = MkdirAll(dpath, 0777); err != nil {
129
                        t.Fatalf("MkdirAll %q: %s", dpath, err)
130
                }
131
 
132
                for _, s := range []string{fpath, dpath + "/file1", path + "/zzz"} {
133
                        fd, err = Create(s)
134
                        if err != nil {
135
                                t.Fatalf("create %q: %s", s, err)
136
                        }
137
                        fd.Close()
138
                }
139
                if err = Chmod(dpath, 0); err != nil {
140
                        t.Fatalf("Chmod %q 0: %s", dpath, err)
141
                }
142
 
143
                // No error checking here: either RemoveAll
144
                // will or won't be able to remove dpath;
145
                // either way we want to see if it removes fpath
146
                // and path/zzz.  Reasons why RemoveAll might
147
                // succeed in removing dpath as well include:
148
                //      * running as root
149
                //      * running on a file system without permissions (FAT)
150
                RemoveAll(path)
151
                Chmod(dpath, 0777)
152
 
153
                for _, s := range []string{fpath, path + "/zzz"} {
154
                        if _, err := Lstat(s); err == nil {
155
                                t.Fatalf("Lstat %q succeeded after partial RemoveAll", s)
156
                        }
157
                }
158
        }
159
        if err = RemoveAll(path); err != nil {
160
                t.Fatalf("RemoveAll %q after partial RemoveAll: %s", path, err)
161
        }
162
        if _, err := Lstat(path); err == nil {
163
                t.Fatalf("Lstat %q succeeded after RemoveAll (final)", path)
164
        }
165
}
166
 
167
func TestMkdirAllWithSymlink(t *testing.T) {
168
        if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
169
                t.Log("Skipping test: symlinks don't exist under Windows/Plan 9")
170
                return
171
        }
172
 
173
        err := Mkdir("_test/dir", 0755)
174
        if err != nil {
175
                t.Fatal(`Mkdir "_test/dir":`, err)
176
        }
177
        defer RemoveAll("_test/dir")
178
 
179
        err = Symlink("dir", "_test/link")
180
        if err != nil {
181
                t.Fatal(`Symlink "dir", "_test/link":`, err)
182
        }
183
        defer RemoveAll("_test/link")
184
 
185
        path := "_test/link/foo"
186
        err = MkdirAll(path, 0755)
187
        if err != nil {
188
                t.Errorf("MkdirAll %q: %s", path, err)
189
        }
190
}
191
 
192
func TestMkdirAllAtSlash(t *testing.T) {
193
        if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
194
                return
195
        }
196
        RemoveAll("/_go_os_test")
197
        err := MkdirAll("/_go_os_test/dir", 0777)
198
        if err != nil {
199
                pathErr, ok := err.(*PathError)
200
                // common for users not to be able to write to /
201
                if ok && pathErr.Err == EACCES {
202
                        return
203
                }
204
                t.Fatalf(`MkdirAll "/_go_os_test/dir": %v`, err)
205
        }
206
        RemoveAll("/_go_os_test")
207
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.