12 func TestNormalizePath(t *testing.T) {
19 {".", FilePathSeparator},
20 {"./", FilePathSeparator},
21 {"..", FilePathSeparator},
22 {"../", FilePathSeparator},
23 {"./..", FilePathSeparator},
24 {"./../", FilePathSeparator},
27 for i, d := range data {
28 cpath := normalizePath(d.input)
29 if d.expected != cpath {
30 t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, cpath)
35 func TestPathErrors(t *testing.T) {
36 path := filepath.Join(".", "some", "path")
37 path2 := filepath.Join(".", "different", "path")
39 perm := os.FileMode(0755)
41 // relevant functions:
42 // func (m *MemMapFs) Chmod(name string, mode os.FileMode) error
43 // func (m *MemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error
44 // func (m *MemMapFs) Create(name string) (File, error)
45 // func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error
46 // func (m *MemMapFs) MkdirAll(path string, perm os.FileMode) error
47 // func (m *MemMapFs) Open(name string) (File, error)
48 // func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, error)
49 // func (m *MemMapFs) Remove(name string) error
50 // func (m *MemMapFs) Rename(oldname, newname string) error
51 // func (m *MemMapFs) Stat(name string) (os.FileInfo, error)
53 err := fs.Chmod(path, perm)
54 checkPathError(t, err, "Chmod")
56 err = fs.Chtimes(path, time.Now(), time.Now())
57 checkPathError(t, err, "Chtimes")
59 // fs.Create doesn't return an error
61 err = fs.Mkdir(path2, perm)
65 err = fs.Mkdir(path2, perm)
66 checkPathError(t, err, "Mkdir")
68 err = fs.MkdirAll(path2, perm)
70 t.Error("MkdirAll:", err)
73 _, err = fs.Open(path)
74 checkPathError(t, err, "Open")
76 _, err = fs.OpenFile(path, os.O_RDWR, perm)
77 checkPathError(t, err, "OpenFile")
80 checkPathError(t, err, "Remove")
82 err = fs.RemoveAll(path)
84 t.Error("RemoveAll:", err)
87 err = fs.Rename(path, path2)
88 checkPathError(t, err, "Rename")
90 _, err = fs.Stat(path)
91 checkPathError(t, err, "Stat")
94 func checkPathError(t *testing.T, err error, op string) {
95 pathErr, ok := err.(*os.PathError)
97 t.Error(op+":", err, "is not a os.PathError")
100 _, ok = pathErr.Err.(*os.PathError)
102 t.Error(op+":", err, "contains another os.PathError")
106 // Ensure Permissions are set on OpenFile/Mkdir/MkdirAll
107 func TestPermSet(t *testing.T) {
108 const fileName = "/myFileTest"
109 const dirPath = "/myDirTest"
110 const dirPathAll = "/my/path/to/dir"
112 const fileMode = os.FileMode(0765)
113 // directories will also have the directory bit set
114 const dirMode = fileMode | os.ModeDir
119 f, err := fs.OpenFile(fileName, os.O_CREATE, fileMode)
121 t.Errorf("OpenFile Create failed: %s", err)
126 s, err := fs.Stat(fileName)
128 t.Errorf("Stat failed: %s", err)
131 if s.Mode().String() != fileMode.String() {
132 t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), fileMode.String())
137 err = fs.Mkdir(dirPath, dirMode)
139 t.Errorf("MkDir Create failed: %s", err)
142 s, err = fs.Stat(dirPath)
144 t.Errorf("Stat failed: %s", err)
148 if s.Mode().String() != dirMode.String() {
149 t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), dirMode.String())
154 err = fs.MkdirAll(dirPathAll, dirMode)
156 t.Errorf("MkDir Create failed: %s", err)
159 s, err = fs.Stat(dirPathAll)
161 t.Errorf("Stat failed: %s", err)
164 if s.Mode().String() != dirMode.String() {
165 t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), dirMode.String())
170 // Fails if multiple file objects use the same file.at counter in MemMapFs
171 func TestMultipleOpenFiles(t *testing.T) {
172 defer removeAllTestFiles(t)
173 const fileName = "afero-demo2.txt"
175 var data = make([][]byte, len(Fss))
177 for i, fs := range Fss {
179 path := filepath.Join(dir, fileName)
180 fh1, err := fs.Create(path)
182 t.Error("fs.Create failed: " + err.Error())
184 _, err = fh1.Write([]byte("test"))
186 t.Error("fh.Write failed: " + err.Error())
188 _, err = fh1.Seek(0, os.SEEK_SET)
193 fh2, err := fs.OpenFile(path, os.O_RDWR, 0777)
195 t.Error("fs.OpenFile failed: " + err.Error())
197 _, err = fh2.Seek(0, os.SEEK_END)
201 _, err = fh2.Write([]byte("data"))
210 _, err = fh1.Write([]byte("data"))
218 // the file now should contain "datadata"
219 data[i], err = ReadFile(fs, path)
225 for i, fs := range Fss {
229 if string(data[0]) != string(data[i]) {
230 t.Errorf("%s and %s don't behave the same\n"+
231 "%s: \"%s\"\n%s: \"%s\"\n",
232 Fss[0].Name(), fs.Name(), Fss[0].Name(), data[0], fs.Name(), data[i])
237 // Test if file.Write() fails when opened as read only
238 func TestReadOnly(t *testing.T) {
239 defer removeAllTestFiles(t)
240 const fileName = "afero-demo.txt"
242 for _, fs := range Fss {
244 path := filepath.Join(dir, fileName)
246 f, err := fs.Create(path)
248 t.Error(fs.Name()+":", "fs.Create failed: "+err.Error())
250 _, err = f.Write([]byte("test"))
252 t.Error(fs.Name()+":", "Write failed: "+err.Error())
256 f, err = fs.Open(path)
258 t.Error("fs.Open failed: " + err.Error())
260 _, err = f.Write([]byte("data"))
262 t.Error(fs.Name()+":", "No write error")
266 f, err = fs.OpenFile(path, os.O_RDONLY, 0644)
268 t.Error("fs.Open failed: " + err.Error())
270 _, err = f.Write([]byte("data"))
272 t.Error(fs.Name()+":", "No write error")
278 func TestWriteCloseTime(t *testing.T) {
279 defer removeAllTestFiles(t)
280 const fileName = "afero-demo.txt"
282 for _, fs := range Fss {
284 path := filepath.Join(dir, fileName)
286 f, err := fs.Create(path)
288 t.Error(fs.Name()+":", "fs.Create failed: "+err.Error())
292 f, err = fs.Create(path)
294 t.Error(fs.Name()+":", "fs.Create failed: "+err.Error())
298 t.Error(fs.Name()+":", "Stat failed: "+err.Error())
300 timeBefore := fi.ModTime()
302 // sorry for the delay, but we have to make sure time advances,
303 // also on non Un*x systems...
304 switch runtime.GOOS {
306 time.Sleep(2 * time.Second)
308 time.Sleep(1 * time.Second)
309 default: // depending on the FS, this may work with < 1 second, on my old ext3 it does not
310 time.Sleep(1 * time.Second)
313 _, err = f.Write([]byte("test"))
315 t.Error(fs.Name()+":", "Write failed: "+err.Error())
318 fi, err = fs.Stat(path)
320 t.Error(fs.Name()+":", "fs.Stat failed: "+err.Error())
322 if fi.ModTime().Equal(timeBefore) {
323 t.Error(fs.Name()+":", "ModTime was not set on Close()")
328 // This test should be run with the race detector on:
329 // go test -race -v -timeout 10s -run TestRacingDeleteAndClose
330 func TestRacingDeleteAndClose(t *testing.T) {
332 pathname := "testfile"
333 f, err := fs.Create(pathname)
338 in := make(chan bool)
351 // This test should be run with the race detector on:
352 // go test -run TestMemFsDataRace -race
353 func TestMemFsDataRace(t *testing.T) {
354 const dir = "test_dir"
357 if err := fs.MkdirAll(dir, 0777); err != nil {
362 done := make(chan struct{})
366 for i := 0; i < n; i++ {
367 fname := filepath.Join(dir, fmt.Sprintf("%d.txt", i))
368 if err := WriteFile(fs, fname, []byte(""), 0777); err != nil {
371 if err := fs.Remove(fname); err != nil {
383 _, err := ReadDir(fs, dir)
391 func TestMemFsDirMode(t *testing.T) {
393 err := fs.Mkdir("/testDir1", 0644)
397 err = fs.MkdirAll("/sub/testDir2", 0644)
401 info, err := fs.Stat("/testDir1")
406 t.Error("should be a directory")
408 if !info.Mode().IsDir() {
409 t.Error("FileMode is not directory")
411 info, err = fs.Stat("/sub/testDir2")
416 t.Error("should be a directory")
418 if !info.Mode().IsDir() {
419 t.Error("FileMode is not directory")