lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit 76560182459fdb808698ce07fb4fb4118a6ae3d9
parent 6df3fcb1a4f4e5b19ceaa5a37dcdbcfc2a98c247
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Tue,  2 Jan 2024 12:52:30 +0900

change init

Diffstat:
Mfs_test.go | 166++++++++++++++++++++++++++++++++-----------------------------------------------
1 file changed, 68 insertions(+), 98 deletions(-)

diff --git a/fs_test.go b/fs_test.go @@ -2,8 +2,8 @@ package lib9p import ( "bytes" - "io/fs" "fmt" + "io/fs" "path" "strings" "testing" @@ -104,7 +104,7 @@ type group struct { func (fs *testFS) OpenFile(path string, omode OpenMode) (File, error) { wnames := strings.Split(path, "/") - f, err := fs.Walk(wnames) + f, err := fs.walk(wnames) if err != nil { return nil, fmt.Errorf("walk: %v", err) } @@ -128,7 +128,7 @@ func (fs *testFS) IsGroupMember(gid, uid string) bool { return g.members[uid] } -func (fs *testFS) Walk(wnames []string) (*testFile, error) { +func (fs *testFS) walk(wnames []string) (*testFile, error) { if len(wnames) == 1 && (wnames[0] == "." || wnames[0] == "") { return fs.root, nil } @@ -146,117 +146,87 @@ L: return cwd, nil } -func (fs *testFS) WalkPath(pathname string) (*testFile, error) { +func (fs *testFS) walkPath(pathname string) (*testFile, error) { wnames := strings.Split(pathname, "/") - return fs.Walk(wnames) + return fs.walk(wnames) } var testfs *testFS func init() { - testfs = &testFS{ - root: &testFile{ - stat: Stat{ - Qid: Qid{Path: 0, Type: QTDIR}, - Mode: FileMode(fs.ModeDir | 0755), - Name: "root", - Uid: "glenda", - Gid: "glenda", - Muid: "glenda", - }, - children: []*testFile{ - &testFile{ - content: []byte("a\n"), - stat: Stat{ - Qid: Qid{Path: 1, Type: QTFILE}, - Mode: FileMode(0644), - Name: "a", - Uid: "glenda", - Gid: "glenda", - Muid: "glenda", - }, - }, - &testFile{ - content: []byte("b\n"), - stat: Stat{ - Qid: Qid{Path: 2, Type: QTFILE}, - Mode: FileMode(0600), - Name: "b", - Uid: "ken", - Gid: "ken", - Muid: "ken", - }, - }, - &testFile{ - stat: Stat{ - Qid: Qid{Path: 3, Type: QTDIR}, - Mode: FileMode(fs.ModeDir | 0755), - Name: "dir", - Uid: "rob", - Gid: "rob", - Muid: "rob", - }, - children: []*testFile{ - &testFile{ - content: []byte("unko\n"), - stat: Stat{ - Qid: Qid{Path: 4, Type: QTFILE}, - Mode: FileMode(0666), - Name: "file", - Uid: "brian", - Gid: "brian", - Muid: "dennis", - }, - }, - }, - }, + fileTree := []struct { + pathname string + mode fs.FileMode + uid, gid, muid string + content string + }{ + {".", fs.ModeDir | 0755, "glenda", "glenda", "glenda", ""}, + {"a", 0644, "glenda", "glenda", "glenda", "a\n"}, + {"b", 0600, "ken", "ken", "ken", "b\n"}, + {"dir", fs.ModeDir | 0755, "rob", "rob", "rob", ""}, + {"dir/file", 0666, "brian", "brian", "dennis", "unko\n"}, + } + testfs = new(testFS) + for i, f := range fileTree { + var ff *testFile + if i == 0 { // root of the testfs + testfs.root = new(testFile) + ff = testfs.root + ff.parent = testfs.root + } else { + d, err := testfs.walkPath(path.Dir(f.pathname)) + if err != nil { + panic(err) + } + ff = new(testFile) + d.children = append(d.children, ff) + ff.parent = d + } + ff.content = []byte(f.content) + var qt QidType + if f.mode&fs.ModeDir != 0 { + qt = QTDIR + } else { + qt = QTFILE + } + ff.stat = Stat{ + Qid: Qid{Path: uint64(i), Type: qt}, + Mode: f.mode, + Name: path.Base(f.pathname), + Uid: f.uid, + Gid: f.gid, + Muid: f.muid, + } + ff.fsys = testfs + } + testfs.waitc = make(chan struct{}) + testfs.groups = map[string]group{ + "bell": group{ + leader: "glenda", + members: map[string]bool{ + "glenda": true, + "ken": true, + "dennis": true, + "brian": true, + "rob": true, }, }, - waitc: make(chan struct{}), - groups: map[string]group{ - "bell": group{ - leader: "glenda", - members: map[string]bool{ - "glenda": true, - "ken": true, - "dennis": true, - "brian": true, - "rob": true, - }, - }, - "kessoku": group{ - leader: "ijichi", - members: map[string]bool{ - "ijichi": true, - "yamada": true, - "gotoh": true, - "kita": true, - }, + "kessoku": group{ + leader: "ijichi", + members: map[string]bool{ + "ijichi": true, + "yamada": true, + "gotoh": true, + "kita": true, }, }, } - SetfsysAndparent(testfs, nil) -} - -// SetfsysAndparent sets file.fsys and file.parent for every file in the fsys. -// Pass nil as file to setup entire file system. -func SetfsysAndparent(fsys *testFS, file *testFile) { - if file == nil { - file = fsys.root - file.parent = fsys.root - file.fsys = fsys - } - for _, child := range file.children { - child.parent = file - child.fsys = fsys - SetfsysAndparent(fsys, child) - } } // This function does the actual work for TestWalk(). func testWalk(t *testing.T, fs *testFS, pathname string, file *testFile) { t.Logf("walk %s", pathname) - f, err := fs.Walk(strings.Split(pathname, "/")) + f, err := fs.walk(strings.Split(pathname, "/")) if err != nil { t.Errorf("open %s: %v", pathname, err) }