summaryrefslogtreecommitdiff
path: root/internal/vfs/memoryfs_test.go
blob: f6a9cf1cee7f53ff8a7e76f8c9db3515a0be73a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
		}
	}
}