commit 121b1130d450e81812e3c347cc034131a7e59693
parent 5fbe04b30fccf09ce62755b47410c8349670d85d
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 5 Jan 2024 15:46:45 +0900
change client.TestReadDir to use fs.WalkDir
Diffstat:
M | client/file_test.go | | | 105 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 64 insertions(+), 41 deletions(-)
diff --git a/client/file_test.go b/client/file_test.go
@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"io/fs"
- "path"
"reflect"
"strings"
"testing"
@@ -145,8 +144,8 @@ func TestFileRead(t *testing.T) {
}
}
-// TestReadDir tests whether ReadDir returns the same dir entries as testfs.Fsys
-// has.
+// TestReadDir tests whether ReadDir returns the same dir entries as testfs
+// by comparing their *lib9p.Stat.
func TestReadDir(t *testing.T) {
cfs, cancel, err := mount(testfs)
if err != nil {
@@ -154,46 +153,70 @@ func TestReadDir(t *testing.T) {
}
defer cancel()
defer cfs.Unmount()
- testReadDir(t, cfs, testfs, ".")
-}
-
-func testReadDir(t *testing.T, cfs *FS, tfs *testFS, cwd string) {
- cf, err := cfs.OpenFile(cwd, lib9p.OREAD)
- if err != nil {
- if strings.Contains(err.Error(), "permission") {
- return
+ var walk = func(path string, d fs.DirEntry, err error) error {
+ t.Log(path)
+ if err != nil {
+ return err
}
- t.Fatalf("open %s: %v", cwd, err)
- }
- defer cf.Close()
- st, err := cf.Stat()
- if err != nil {
- t.Fatalf("stat: %v", err)
- }
- if !st.IsDir() {
- return
- }
- de, err := cf.(*File).ReadDir(-1)
- if err != nil && err != io.EOF {
- t.Fatalf("readdir: %v", err)
- }
- tf, err := tfs.OpenFile(cwd, lib9p.OREAD)
- if err != nil {
- t.Fatalf("open: %v", err)
- }
- if len(de) != len(tf.(*testFile).children) {
- t.Fatalf("number of directory entries does not match.: %d != %d",
- len(de), len(tf.(*testFile).children))
- }
-L:
- for _, f := range de {
- for _, c := range tf.(*testFile).children {
- if c.stat.Name == f.Name() {
- childPath := path.Join(cwd, f.Name())
- testReadDir(t, cfs, tfs, childPath)
- continue L
+ if !d.IsDir() {
+ return nil
+ }
+ cf, err := cfs.OpenFile(path, lib9p.OREAD)
+ if err != nil {
+ if !strings.Contains(err.Error(), "permission") {
+ t.Error(err)
+ }
+ return nil
+ }
+ cde, err := cf.(lib9p.ReadDirFile).ReadDir(-1)
+ if err != nil {
+ if err != io.EOF {
+ t.Error(err)
+ return nil
}
}
- t.Errorf("file name not in the testfs: %s", f.Name())
+ tf, err := testfs.OpenFile(path, lib9p.OREAD)
+ if err != nil {
+ if !strings.Contains(err.Error(), "permission") {
+ t.Error(err)
+ }
+ return nil
+ }
+ tde, err := tf.(lib9p.ReadDirFile).ReadDir(-1)
+ if err != nil {
+ if err != io.EOF {
+ t.Error(err)
+ return nil
+ }
+ }
+ if len(cde) != len(tde) {
+ t.Errorf("%s: length of direntry not match", path)
+ return nil
+ }
+ demap := make(map[string]*lib9p.Stat)
+ for _, cde0 := range cde {
+ info, err := cde0.Info()
+ if err != nil {
+ t.Error(err)
+ return nil
+ }
+ demap[cde0.Name()] = info.Sys().(*lib9p.Stat)
+ }
+ for _, tde0 := range tde {
+ info, err := tde0.Info()
+ if err != nil {
+ t.Error(err)
+ return nil
+ }
+ if !reflect.DeepEqual(demap[tde0.Name()], info.Sys().(*lib9p.Stat)) {
+ t.Errorf("%s: stat doesn't match:\n\twant: %v\n\tgot: %v",
+ path, info.Sys().(*lib9p.Stat), demap[tde0.Name()])
+ return nil
+ }
+ }
+ return nil
+ }
+ if err := fs.WalkDir(lib9p.ExportFS{FS: testfs}, ".", walk); err != nil {
+ t.Error(err)
}
}