diff options
| author | Shulhan <ms@kilabit.info> | 2023-11-14 02:33:46 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-11-14 02:35:28 +0700 |
| commit | 0021f75d8abb4e107287781b88e2556c83c9dc28 (patch) | |
| tree | f1f7880851ecd9b89684c1af3aab2439c076084b /lib/memfs/memfs_test.go | |
| parent | 05e1741b67591a53187fd9fe18a0558a9f401723 (diff) | |
| download | pakakeh.go-0021f75d8abb4e107287781b88e2556c83c9dc28.tar.xz | |
lib/memfs: improve the refresh method
Instead of refreshing from the Root, find the directory that closes
to the requested path.
While at it, simplify the returned error.
Diffstat (limited to 'lib/memfs/memfs_test.go')
| -rw-r--r-- | lib/memfs/memfs_test.go | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index 15aebf9b..eb10bcd4 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -5,6 +5,7 @@ package memfs import ( + "bytes" "encoding/json" "fmt" "log" @@ -359,6 +360,92 @@ func TestMemFS_Get(t *testing.T) { } } +func TestMemFS_Get_refresh(t *testing.T) { + var ( + tdata *test.Data + err error + ) + + tdata, err = test.LoadData(`internal/testdata/get_refresh_test.data`) + if err != nil { + t.Fatal(err) + } + + var ( + tempDir = t.TempDir() + opts = Options{ + Root: tempDir, + TryDirect: true, + } + + mfs *MemFS + ) + + mfs, err = New(&opts) + if err != nil { + t.Fatal(err) + } + + var ( + node *Node + filePath string + tag string + path string + expJson string + expError string + fileContent []byte + rawJson []byte + gotJson bytes.Buffer + ) + + for filePath, fileContent = range tdata.Input { + path = filepath.Join(tempDir, filepath.Dir(filePath)) + + err = os.MkdirAll(path, 0700) + if err != nil { + t.Fatal(err) + } + + if len(fileContent) != 0 { + // Only create the file if content is set. + path = filepath.Join(tempDir, filePath) + err = os.WriteFile(path, fileContent, 0600) + if err != nil { + t.Fatal(err) + } + } + + // Try Get the file. + + tag = filePath + `:error` + expError = string(tdata.Output[tag]) + + node, err = mfs.Get(filePath) + if err != nil { + test.Assert(t, tag, expError, err.Error()) + continue + } + + // Check the tree of MemFS. + + rawJson, err = mfs.Root.JSON(9999, true) + if err != nil { + t.Fatal(err) + } + + gotJson.Reset() + err = json.Indent(&gotJson, rawJson, ``, ` `) + if err != nil { + t.Fatal(err) + } + + test.Assert(t, filePath, string(fileContent), string(node.Content)) + + expJson = string(tdata.Output[filePath]) + test.Assert(t, filePath, expJson, gotJson.String()) + } +} + func TestMemFS_MarshalJSON(t *testing.T) { logp := "MarshalJSON" modTime := time.Date(2021, 7, 30, 20, 04, 00, 0, time.UTC) |
