summaryrefslogtreecommitdiff
path: root/internal/vfs
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-09-21 14:03:45 +0300
committerPaul Buetow <paul@buetow.org>2024-09-21 14:03:45 +0300
commitdff4d455e07d639b82a0bed814f41d0656e9b6d0 (patch)
tree56289a6fd80a00a9724992ab9cb8b3d7215ebedf /internal/vfs
parent780ade3dc066afb8a43be824373414f3d316ebd6 (diff)
cleanup
Diffstat (limited to 'internal/vfs')
-rw-r--r--internal/vfs/memoryfs.go34
-rw-r--r--internal/vfs/memoryfs_test.go108
-rw-r--r--internal/vfs/realfs.go44
3 files changed, 0 insertions, 186 deletions
diff --git a/internal/vfs/memoryfs.go b/internal/vfs/memoryfs.go
deleted file mode 100644
index 6250e39..0000000
--- a/internal/vfs/memoryfs.go
+++ /dev/null
@@ -1,34 +0,0 @@
-package vfs
-
-import (
- "fmt"
- "strings"
-)
-
-// VFS implementaion especially for unit testing.
-type MemoryFS map[string][]byte
-
-func (fs MemoryFS) ReadFile(filePath string) ([]byte, error) {
- if bytes, ok := fs[filePath]; ok {
- return bytes, nil
- }
- return []byte{}, fmt.Errorf("no such file path: %s", filePath)
-}
-
-func (fs MemoryFS) WriteFile(filePath string, bytes []byte) error {
- fs[filePath] = bytes
- return nil
-}
-
-func (fs MemoryFS) FindFiles(dataDir, suffix string) ([]string, error) {
- var filePaths []string
-
- for filePath := range fs {
- if !strings.HasPrefix(filePath, dataDir) || !strings.HasSuffix(filePath, suffix) {
- continue
- }
- filePaths = append(filePaths, filePath)
- }
-
- return filePaths, nil
-}
diff --git a/internal/vfs/memoryfs_test.go b/internal/vfs/memoryfs_test.go
deleted file mode 100644
index f6a9cf1..0000000
--- a/internal/vfs/memoryfs_test.go
+++ /dev/null
@@ -1,108 +0,0 @@
-package vfs
-
-import (
- "slices"
- "strings"
- "testing"
-)
-
-func TestMemoryFS(t *testing.T) {
- t.Parallel()
- fs := 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)
- _ = fs.WriteFile(path, bytes)
- }
-
- t.Run("files are there", func(t *testing.T) {
- testFilesAreThere(t, fs, writeFiles)
- })
-
- t.Run("file is not there", func(t *testing.T) {
- testFileNotThere(t, fs, "/dennis.rodman.txt")
- })
-
- t.Run("find json files", func(t *testing.T) {
- testFindFiles(t, fs, writeFiles, "/data/dir/subdir", ".json", 2)
- })
-
-}
-
-func TestPassByValue(t *testing.T) {
- t.Parallel()
- fs := 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",
- }
-
- // Should work, as the underlying data type is a map.
- func(fs MemoryFS) {
- for path, content := range writeFiles {
- bytes := []byte(content)
- _ = fs.WriteFile(path, bytes)
- }
- }(fs)
-
- t.Run("files are there", func(t *testing.T) {
- testFilesAreThere(t, fs, writeFiles)
- })
-}
-
-func testFilesAreThere(t *testing.T, fs MemoryFS, writeFiles map[string]string) {
- for path, content := range writeFiles {
- bytes, err := fs.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
- }
- }
-}
-
-func testFileNotThere(t *testing.T, fs MemoryFS, filePath string) {
- _, err := fs.ReadFile(filePath)
- if err == nil {
- t.Error("expected file", filePath, "not to be there, but it is")
- return
- }
- t.Log("file", filePath, "not there as expected:", err)
-}
-
-func testFindFiles(t *testing.T, fs MemoryFS, writeFiles map[string]string, dataDir, suffix string, count int) {
- filePaths, err := fs.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
- }
- }
-}
diff --git a/internal/vfs/realfs.go b/internal/vfs/realfs.go
deleted file mode 100644
index afd3eb1..0000000
--- a/internal/vfs/realfs.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package vfs
-
-import (
- "log"
- "os"
- "path/filepath"
- "strings"
-)
-
-type RealFS struct{}
-
-func (RealFS) ReadFile(filePath string) ([]byte, error) {
- return os.ReadFile(filePath)
-}
-
-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 {
- return err
- }
- }
- return os.WriteFile(filePath, bytes, 0644)
-}
-
-func (RealFS) FindFiles(dataDir, suffix string) ([]string, error) {
- var filePaths []string
-
- visit := func() filepath.WalkFunc {
- return func(path string, info os.FileInfo, err error) error {
- if err != nil {
- log.Println(err)
- return nil
- }
- if info.IsDir() || !strings.HasSuffix(path, suffix) {
- return nil
- }
- filePaths = append(filePaths, path)
- return nil
- }
- }
-
- return filePaths, filepath.Walk(dataDir, visit())
-}