diff options
| author | Paul Buetow <paul@buetow.org> | 2024-05-25 23:57:29 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-05-25 23:57:29 +0300 |
| commit | 9852cfaf6c44b9e2960719feb57c6d0e53598f3c (patch) | |
| tree | 9397202cb143503d49a5b226ce5be8fcfa202be8 /internal/vfs/memoryfs_test.go | |
| parent | 6e631b4ccbe71299137469a69900b874d709fe35 (diff) | |
add memoryfs vfs implementation
Diffstat (limited to 'internal/vfs/memoryfs_test.go')
| -rw-r--r-- | internal/vfs/memoryfs_test.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/internal/vfs/memoryfs_test.go b/internal/vfs/memoryfs_test.go new file mode 100644 index 0000000..46607e0 --- /dev/null +++ b/internal/vfs/memoryfs_test.go @@ -0,0 +1,70 @@ +package vfs + +import ( + "slices" + "strings" + "testing" +) + +func TestMemoryFS(t *testing.T) { + t.Parallel() + var vfs VFS = make(MemoryFS) + + writeFiles := map[string]string{ + "/data/dir/foo.json": "hello world", + "/data/dir/subdir/bar.json": "hello solar system", + "/data/dir/subdir/baz.json": "hello universe", + "/data/dir/subdir/bay.txt": "hello bar keeper", + } + + for path, content := range writeFiles { + bytes := []byte(content) + _ = vfs.WriteFile(path, bytes) + } + + t.Run("files are there", func(t *testing.T) { + testFilesAreThere(t, vfs, writeFiles) + }) + + t.Run("find json files", func(t *testing.T) { + testFindFiles(t, vfs, writeFiles, "/data/dir/subdir", ".json", 2) + }) +} + +func testFindFiles(t *testing.T, vfs VFS, writeFiles map[string]string, dataDir, suffix string, count int) { + filePaths, err := vfs.FindFiles(dataDir, suffix) + if err != nil { + t.Error(err) + return + } + + if len(filePaths) != count { + t.Error("expected", count, "json files, but got", filePaths) + return + } + + for filePath := range writeFiles { + if !strings.HasPrefix(filePath, dataDir) || !strings.HasSuffix(filePath, suffix) { + continue + } + + if !slices.Contains(filePaths, filePath) { + t.Error("expected file", filePath, "to be there, but it isn't in", filePaths) + return + } + } +} + +func testFilesAreThere(t *testing.T, vfs VFS, writeFiles map[string]string) { + for path, content := range writeFiles { + bytes, err := vfs.ReadFile(path) + if err != nil { + t.Error(err) + return + } + if content != string(bytes) { + t.Error("expected", content, "in file", path, "but got", string(bytes)) + return + } + } +} |
