diff options
| -rw-r--r-- | Taskfile.yml | 2 | ||||
| -rw-r--r-- | internal/types/entry.go | 2 | ||||
| -rw-r--r-- | internal/vfs/memoryfs.go | 5 | ||||
| -rw-r--r-- | internal/vfs/memoryfs_test.go | 70 | ||||
| -rw-r--r-- | internal/vfs/realfs.go | 2 | ||||
| -rw-r--r-- | internal/vfs/vfs.go | 2 |
6 files changed, 77 insertions, 6 deletions
diff --git a/Taskfile.yml b/Taskfile.yml index 7a7ec41..a8347d2 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -14,7 +14,7 @@ tasks: - go test -v ./... vet: cmds: - - go vet **/*.go + - go vet ./... lint: cmds: - golangci-lint run diff --git a/internal/types/entry.go b/internal/types/entry.go index b4a05e4..c2ccb82 100644 --- a/internal/types/entry.go +++ b/internal/types/entry.go @@ -168,7 +168,7 @@ func (e Entry) SaveFile(filePath string) error { return err } - return e.vfs.SaveFile(filePath, jsonStr) + return e.vfs.WriteFile(filePath, jsonStr) } func (e Entry) String() string { diff --git a/internal/vfs/memoryfs.go b/internal/vfs/memoryfs.go index 22b1611..6250e39 100644 --- a/internal/vfs/memoryfs.go +++ b/internal/vfs/memoryfs.go @@ -5,6 +5,7 @@ import ( "strings" ) +// VFS implementaion especially for unit testing. type MemoryFS map[string][]byte func (fs MemoryFS) ReadFile(filePath string) ([]byte, error) { @@ -14,7 +15,7 @@ func (fs MemoryFS) ReadFile(filePath string) ([]byte, error) { return []byte{}, fmt.Errorf("no such file path: %s", filePath) } -func (fs MemoryFS) SaveFile(filePath string, bytes []byte) error { +func (fs MemoryFS) WriteFile(filePath string, bytes []byte) error { fs[filePath] = bytes return nil } @@ -23,7 +24,7 @@ func (fs MemoryFS) FindFiles(dataDir, suffix string) ([]string, error) { var filePaths []string for filePath := range fs { - if !strings.HasSuffix(filePath, suffix) { + if !strings.HasPrefix(filePath, dataDir) || !strings.HasSuffix(filePath, suffix) { continue } filePaths = append(filePaths, filePath) 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 + } + } +} diff --git a/internal/vfs/realfs.go b/internal/vfs/realfs.go index 8b9c9de..afd3eb1 100644 --- a/internal/vfs/realfs.go +++ b/internal/vfs/realfs.go @@ -13,7 +13,7 @@ func (RealFS) ReadFile(filePath string) ([]byte, error) { return os.ReadFile(filePath) } -func (RealFS) SaveFile(filePath string, bytes []byte) error { +func (RealFS) WriteFile(filePath string, bytes []byte) error { dir := filepath.Dir(filePath) if _, err := os.Stat(dir); os.IsNotExist(err) { if err := os.MkdirAll(dir, 0755); err != nil { diff --git a/internal/vfs/vfs.go b/internal/vfs/vfs.go index 0297ff9..c31a458 100644 --- a/internal/vfs/vfs.go +++ b/internal/vfs/vfs.go @@ -3,6 +3,6 @@ package vfs // virtual file system - useful for testing as well type VFS interface { ReadFile(name string) ([]byte, error) - SaveFile(filePath string, bytes []byte) error + WriteFile(filePath string, bytes []byte) error FindFiles(dataPath, suffix string) ([]string, error) } |
