diff options
| author | Shulhan <ms@kilabit.info> | 2024-02-03 15:58:48 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-02-03 16:21:34 +0700 |
| commit | 0bdc54f2a7a3b2d685b083675856631d967eb17b (patch) | |
| tree | c5f98678b59550469788d1f72ccc029b8bb2998a | |
| parent | 734d077775cee01f2b8b6fd53ad919e824ad6d1e (diff) | |
| download | pakakeh.go-0bdc54f2a7a3b2d685b083675856631d967eb17b.tar.xz | |
lib/memfs: fix flaky test on [Memfs.Get] with refresh
The test is flaky because we use range over map of [tdata.Input].
Since range over map will return random key, there is possibility that
the first test is writing "/dir-a/dir-b/file2" not "/dir-a/dir-b/file".
| -rw-r--r-- | lib/memfs/memfs.go | 11 | ||||
| -rw-r--r-- | lib/memfs/memfs_test.go | 51 |
2 files changed, 35 insertions, 27 deletions
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go index d8644ae4..4f270ac4 100644 --- a/lib/memfs/memfs.go +++ b/lib/memfs/memfs.go @@ -246,13 +246,13 @@ func (mfs *MemFS) AddFile(internalPath, externalPath string) (node *Node, err er return node, nil } -// Get the node representation of file in memory. If path is not exist it -// will return fs.ErrNotExist. +// Get the node representation of file in memory. +// If path is not exist it will return [fs.ErrNotExist]. func (mfs *MemFS) Get(path string) (node *Node, err error) { - logp := "Get" + var logp = `Get` if mfs == nil || mfs.PathNodes == nil { - return nil, fmt.Errorf("%s %s: %w", logp, path, fs.ErrNotExist) + return nil, fmt.Errorf(`%s %q: %w`, logp, path, fs.ErrNotExist) } path = strings.TrimSpace(path) if len(path) == 0 { @@ -271,8 +271,9 @@ func (mfs *MemFS) Get(path string) (node *Node, err error) { return node, nil } - // Get node from sub. + // Get node from sub fs. var sub *MemFS + for _, sub = range mfs.subfs { node, _ = sub.Get(path) if node != nil { diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index 411d1a06..fc889afe 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -364,6 +364,10 @@ func TestMemFS_Get(t *testing.T) { } func TestMemFS_Get_refresh(t *testing.T) { + type testCase struct { + filePath string + } + var ( tdata *test.Data err error @@ -389,30 +393,28 @@ func TestMemFS_Get_refresh(t *testing.T) { t.Fatal(err) } + var listCase = []testCase{{ + filePath: `/dir-a/dir-b/file`, + }, { + filePath: `/dir-a/dir-b/file2`, + }} + var ( - node *Node - filePath string - tag string - path string - expJSON string - expError string - fileContent []byte - rawJSON []byte - gotJSON bytes.Buffer + c testCase + gotJSON bytes.Buffer ) + for _, c = range listCase { + var fullpath = filepath.Join(tempDir, c.filePath) - for filePath, fileContent = range tdata.Input { - path = filepath.Join(tempDir, filepath.Dir(filePath)) - - err = os.MkdirAll(path, 0700) + err = os.MkdirAll(filepath.Dir(fullpath), 0700) if err != nil { t.Fatal(err) } - if len(fileContent) != 0 { + var expContent = tdata.Input[c.filePath] + if len(expContent) != 0 { // Only create the file if content is set. - path = filepath.Join(tempDir, filePath) - err = os.WriteFile(path, fileContent, 0600) + err = os.WriteFile(fullpath, expContent, 0600) if err != nil { t.Fatal(err) } @@ -420,10 +422,13 @@ func TestMemFS_Get_refresh(t *testing.T) { // Try Get the file. - tag = filePath + `:error` - expError = string(tdata.Output[tag]) + var ( + tag = c.filePath + `:error` + expError = string(tdata.Output[tag]) + node *Node + ) - node, err = mfs.Get(filePath) + node, err = mfs.Get(c.filePath) if err != nil { test.Assert(t, tag, expError, err.Error()) continue @@ -431,6 +436,8 @@ func TestMemFS_Get_refresh(t *testing.T) { // Check the tree of MemFS. + var rawJSON []byte + rawJSON, err = mfs.Root.JSON(9999, true, false) if err != nil { t.Fatal(err) @@ -442,10 +449,10 @@ func TestMemFS_Get_refresh(t *testing.T) { t.Fatal(err) } - test.Assert(t, filePath, string(fileContent), string(node.Content)) + test.Assert(t, c.filePath+` content`, string(expContent), string(node.Content)) - expJSON = string(tdata.Output[filePath]) - test.Assert(t, filePath, expJSON, gotJSON.String()) + var expJSON = string(tdata.Output[c.filePath]) + test.Assert(t, c.filePath+` JSON of memfs.Root`, expJSON, gotJSON.String()) } } |
