func isFile(fs rwvfs.FileSystem, file string) bool { fi, err := fs.Stat(file) if err != nil { return false } return fi.Mode().IsRegular() }
func PathDoesNotExist(t *testing.T, label string, fs rwvfs.FileSystem, path string) { fi, err := fs.Stat(path) if err != nil && !os.IsNotExist(err) { t.Errorf("%s: Stat(%q): want os.IsNotExist-satisfying error, got %q", label, path, err) } else if err == nil { t.Errorf("%s: Stat(%q): want file to not exist, got existing file with FileInfo %+v", label, path, fi) } }
func testWrite(t *testing.T, fs rwvfs.FileSystem, path string) { label := fmt.Sprintf("%T", fs) w, err := fs.Create(path) if err != nil { t.Fatalf("%s: WriterOpen: %s", label, err) } input := []byte("qux") _, err = w.Write(input) if err != nil { t.Fatalf("%s: Write: %s", label, err) } err = w.Close() if err != nil { t.Fatalf("%s: w.Close: %s", label, err) } var r io.ReadCloser r, err = fs.Open(path) if err != nil { t.Fatalf("%s: Open: %s", label, err) } var output []byte output, err = ioutil.ReadAll(r) if err != nil { t.Fatalf("%s: ReadAll: %s", label, err) } if !bytes.Equal(output, input) { t.Errorf("%s: got output %q, want %q", label, output, input) } r, err = fs.Open(path) if err != nil { t.Fatalf("%s: Open: %s", label, err) } output, err = ioutil.ReadAll(r) if err != nil { t.Fatalf("%s: ReadAll: %s", label, err) } if !bytes.Equal(output, input) { t.Errorf("%s: got output %q, want %q", label, output, input) } if err := fs.Remove(path); err != nil { t.Errorf("%s: Remove(%q): %s", label, path, err) } time.Sleep(time.Second) fi, err := fs.Stat(path) if err != nil && !os.IsNotExist(err) { t.Errorf("%s: Stat(%q): want os.IsNotExist-satisfying error, got %q", label, path, err) } else if err == nil { t.Errorf("%s: Stat(%q): want file to not exist, got existing file with FileInfo %+v", label, path, fi) } }
func IsFile(t *testing.T, label string, fs rwvfs.FileSystem, path string) { fi, err := fs.Stat(path) if err != nil { t.Fatalf("%s: Stat(%q): %s", label, path, err) } if !fi.Mode().IsRegular() { t.Errorf("%s: got fs.Stat(%q) Mode().IsRegular() == false, want true", label, path) } }
func IsDir(t *testing.T, label string, fs rwvfs.FileSystem, path string) { fi, err := fs.Stat(path) if err != nil { t.Fatalf("%s: Stat(%q): %s", label, path, err) } if fi == nil { t.Fatalf("%s: FileInfo (%q) == nil", label, path) } if !fi.IsDir() { t.Errorf("%s: got fs.Stat(%q) IsDir() == false, want true", label, path) } }
// statIndex calls fs.Stat on the index's backing file or dir. func statIndex(fs rwvfs.FileSystem, name string) (os.FileInfo, error) { return fs.Stat(fmt.Sprintf(indexFilename, name)) }
func Mkdir(t *testing.T, fs rwvfs.FileSystem) { label := fmt.Sprintf("%T", fs) if strings.Contains(label, "subFS") { if err := fs.Mkdir("/"); err != nil && !os.IsExist(err) { t.Fatalf("%s: subFS Mkdir(/): %s", label, err) } } if strings.Contains(label, "mapFS") { if err := fs.Mkdir("/"); err != nil && !os.IsExist(err) { t.Fatalf("%s: mapFS Mkdir(/): %s", label, err) } } fi, err := fs.Stat(".") if err != nil { t.Fatalf("%s: Stat(.): %s", label, err) } if !fi.Mode().IsDir() { t.Fatalf("%s: got Stat(.) FileMode %o, want IsDir", label, fi.Mode()) } fi, err = fs.Stat("/") if err != nil { t.Fatalf("%s: Stat(/): %s", label, err) } if !fi.Mode().IsDir() { t.Fatalf("%s: got Stat(/) FileMode %o, want IsDir", label, fi.Mode()) } if _, err := fs.ReadDir("."); err != nil { t.Fatalf("%s: ReadDir(.): %s", label, err) } if _, err := fs.ReadDir("/"); err != nil { t.Fatalf("%s: ReadDir(/): %s", label, err) } fis, err := fs.ReadDir("/") if err != nil { t.Fatalf("%s: ReadDir(/): %s", label, err) } if len(fis) != 0 { t.Fatalf("%s: ReadDir(/): got %d file infos (%v), want none (is it including .?)", label, len(fis), fis) } err = fs.Mkdir("dir0") if err != nil { t.Fatalf("%s: Mkdir(dir0): %s", label, err) } IsDir(t, label, fs, "dir0") IsDir(t, label, fs, "/dir0") err = fs.Mkdir("/dir1") if err != nil { t.Fatalf("%s: Mkdir(/dir1): %s", label, err) } IsDir(t, label, fs, "dir1") IsDir(t, label, fs, "/dir1") err = fs.Mkdir("/dir1") if !os.IsExist(err) { t.Errorf("%s: Mkdir(/dir1) again: got err %v, want os.IsExist-satisfying error", label, err) } err = fs.Mkdir("/parent-doesnt-exist/dir2") if !os.IsNotExist(err) { t.Errorf("%s: Mkdir(/parent-doesnt-exist/dir2): got error %v, want os.IsNotExist-satisfying error", label, err) } err = fs.Remove("/dir1") if err != nil { t.Errorf("%s: Remove(/dir1): %s", label, err) } PathDoesNotExist(t, label, fs, "/dir1") }
func testStat(t *testing.T, fs rwvfs.FileSystem, path string) { label := fmt.Sprintf("Stat %T", fs) cases := []struct { parent, child string checkDirs []string }{ {pathpkg.Join(path, "p"), pathpkg.Join(path, "p/c"), nil}, {pathpkg.Join(path, "."), pathpkg.Join(path, "c"), nil}, {pathpkg.Join(path, "p1/p2"), pathpkg.Join(path, "p1/p2/p3/c"), []string{"p1", "p1/p2/p3"}}, {pathpkg.Join(path, "p1"), pathpkg.Join(path, "p1/p2/p3/c"), []string{"p1/p2", "p1/p2/p3"}}, } // Clean out bucket. for _, x := range cases { removeFile(t, fs, x.parent) removeFile(t, fs, x.child) } removeFile(t, fs, path) if path != "." { if _, err := fs.Stat(path); !os.IsNotExist(err) { t.Fatalf("%s: Stat(%s): got error %v, want os.IsNotExist-satisfying", label, path, err) } } if _, err := fs.Stat(path + "/z"); !os.IsNotExist(err) { t.Fatalf("%s: Stat(%s): got error %v, want os.IsNotExist-satisfying", label, path+"/z", err) } // Not sure of the best way to treat S3 keys that are // delimiter-prefixes of other keys, since they can either be like // dirs or files. But let's just choose a way and add a test so we // can change the behavior easily later. for _, x := range cases { t.Logf("# parent %q, child %q", x.parent, x.child) createFile(t, fs, x.parent, []byte("x")) createFile(t, fs, x.child, []byte("x")) parentFI, err := fs.Stat(x.parent) if err != nil { t.Fatalf("%s: Stat(%s): %s", label, x.parent, err) } if !parentFI.Mode().IsDir() { t.Fatalf("%s: Stat(%s) got Mode().IsDir() == false, want true", label, x.parent) } childFI, err := fs.Stat(x.child) if err != nil { t.Fatalf("%s: Stat(%s): %s", label, x.child, err) } if !childFI.Mode().IsRegular() { t.Fatalf("%s: Stat(%s) got Mode().IsRegular() == false, want true", label, x.child) } // Should not exist. doesntExist := pathpkg.Join(x.child, "doesntexist") if _, err := fs.Stat(doesntExist); !os.IsNotExist(err) { t.Fatalf("%s: Stat(%s): got error %v, want os.IsNotExist-satisfying", label, doesntExist, err) } for _, dir := range x.checkDirs { dir = pathpkg.Join(path, dir) fi, err := fs.Stat(dir) if err != nil { t.Fatalf("%s: Stat(%s): %s", label, dir, err) } if !fi.Mode().IsDir() { t.Fatalf("%s: Stat(%s): not dir, want dir", label, dir) } } if x.parent != "." { // Check that the parent file can be opened like a file. f, err := fs.Open(x.parent) if err != nil { t.Fatalf("%s: Open(%s): %s", label, x.parent, err) } f.Close() } // Clean up if err := fs.Remove(x.parent); err != nil { t.Errorf("%s: Remove(%q): %s", label, x.parent, err) } if err := fs.Remove(x.child); err != nil { t.Errorf("%s: Remove(%q): %s", label, x.child, err) } } }