commit 5fbe04b30fccf09ce62755b47410c8349670d85d
parent cac539e010e709f24bcd43c790387dfd34e59488
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 5 Jan 2024 15:29:59 +0900
add client TestOpenFile
Diffstat:
3 files changed, 64 insertions(+), 10 deletions(-)
diff --git a/client/file_test.go b/client/file_test.go
@@ -40,7 +40,7 @@ func TestFileStat(t *testing.T) {
defer cfs.Unmount()
var testFileStat = func(path string, d fs.DirEntry, err error) error {
t.Log("walk:", path)
- fi, err := fs.Stat(lib9p.ExportFS{testfs}, path)
+ fi, err := fs.Stat(lib9p.ExportFS{FS: testfs}, path)
if err != nil {
t.Error(err)
}
@@ -53,7 +53,7 @@ func TestFileStat(t *testing.T) {
}
return nil
}
- if err := fs.WalkDir(lib9p.ExportFS{cfs}, ".", testFileStat); err != nil {
+ if err := fs.WalkDir(lib9p.ExportFS{FS: testfs}, ".", testFileStat); err != nil {
t.Error(err)
}
}
@@ -92,7 +92,7 @@ func TestClose(t *testing.T) {
<-cc
return nil
}
- if err := fs.WalkDir(lib9p.ExportFS{cfs}, ".", walk); err != nil {
+ if err := fs.WalkDir(lib9p.ExportFS{FS: testfs}, ".", walk); err != nil {
t.Error(err)
}
}
@@ -140,7 +140,7 @@ func TestFileRead(t *testing.T) {
}
return nil
}
- if err := fs.WalkDir(lib9p.ExportFS{cfs}, ".", walk); err != nil {
+ if err := fs.WalkDir(lib9p.ExportFS{FS: testfs}, ".", walk); err != nil {
t.Error(err)
}
}
@@ -158,7 +158,7 @@ func TestReadDir(t *testing.T) {
}
func testReadDir(t *testing.T, cfs *FS, tfs *testFS, cwd string) {
- cf, err := cfs.Open(cwd)
+ cf, err := cfs.OpenFile(cwd, lib9p.OREAD)
if err != nil {
if strings.Contains(err.Error(), "permission") {
return
diff --git a/client/fs.go b/client/fs.go
@@ -16,11 +16,6 @@ type FS struct {
tPool *tagPool
}
-// Open opens the file named name in fsys.
-func (fsys *FS) Open(name string) (lib9p.File, error) {
- return fsys.OpenFile(name, lib9p.OREAD)
-}
-
// OpenFile opens the file named name in fsys with omode.
// If the file does not exist, it create it with perm.
func (fsys *FS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, error) {
diff --git a/client/fs2_test.go b/client/fs2_test.go
@@ -0,0 +1,59 @@
+package client
+
+import (
+ "io/fs"
+ "reflect"
+ "strings"
+ "testing"
+
+ "git.mtkn.jp/lib9p"
+)
+
+// TestOpenFile checks if *FS.OpenFile opens every file in testfs without error
+// other than permission error and
+// every opened file is the same as that of testfs by comparing their lib9p.Stat.
+func TestOpenFile(t *testing.T) {
+ cfs, cancel, err := mount(testfs)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer cancel()
+ walk := func(path string, d fs.DirEntry, err error) error {
+ cf, err := cfs.OpenFile(path, lib9p.OREAD)
+ if err != nil {
+ if !strings.Contains(err.Error(), "permission") {
+ t.Error(err)
+ }
+ return nil
+ }
+ cfi, err := cf.Stat()
+ if err != nil {
+ t.Error(err)
+ return err
+ }
+ cst := cfi.Sys().(*lib9p.Stat)
+ tf, err := testfs.OpenFile(path, lib9p.OREAD)
+ if err != nil {
+ if !strings.Contains(err.Error(), "permission") {
+ t.Error(err)
+ }
+ return nil
+ }
+ tfi, err := tf.Stat()
+ if err != nil {
+ t.Error(err)
+ return err
+ }
+ tst := tfi.Sys().(*lib9p.Stat)
+ if !reflect.DeepEqual(cst, tst) {
+ t.Errorf("%s: stat does not match:\n\twant: %v\n\tgot: %v",
+ path, tst, cst)
+ return nil
+ }
+ return nil
+ }
+ if err := fs.WalkDir(lib9p.ExportFS{FS: testfs}, ".", walk); err != nil {
+ t.Error(err)
+ }
+}
+