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 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 Write(t *testing.T, fs rwvfs.FileSystem) { label := fmt.Sprintf("%T", fs) fs.Mkdir("/foo") defer fs.Remove("/foo") path := "/foo/bar" 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) } err = r.Close() if err != nil { t.Fatalf("%s: r.Close: %s", label, err) } if !bytes.Equal(output, input) { t.Errorf("%s: got output %q, want %q", label, output, input) } IsFile(t, label, fs, path) IsDir(t, label, fs, "/foo") infos, err := fs.ReadDir("/foo") if err != nil { t.Fatalf("%s: ReadDir: %s", label, err) } if len(infos) != 1 || infos[0].Name() != "bar" { t.Fatalf("%s: ReadDir: got %v, want file 'bar'", label, infos) } 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) } err = r.Close() if err != nil { t.Fatalf("%s: r.Close: %s", label, err) } if !bytes.Equal(output, input) { t.Errorf("%s: got output %q, want %q", label, output, input) } err = fs.Remove(path) if err != nil { t.Errorf("%s: Remove(%q): %s", label, path, err) } PathDoesNotExist(t, label, fs, path) }
func removeFile(t *testing.T, fs rwvfs.FileSystem, path string) { if err := fs.Remove(path); err != nil { t.Fatalf("removeFile(%q): %s", path, err) } }
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) } } }