...
1 package load
2
3 import (
4 "io/fs"
5 "os"
6 "path/filepath"
7 "slices"
8 "testing"
9 "testing/fstest"
10
11 "github.com/go-quicktest/qt"
12 )
13
14 func TestIOFS(t *testing.T) {
15 dir := t.TempDir()
16 onDiskFiles := []string{
17 "foo/bar/a",
18 "foo/bar/b",
19 "foo/baz",
20 "arble",
21 }
22 for _, f := range onDiskFiles {
23 writeFile(t, filepath.Join(dir, f), f)
24 }
25 var fsys fileSystem
26 overlayFiles := []string{
27 "foo/bar/a",
28 "foo/bar/c",
29 "other/x",
30 }
31 overlay := map[string]Source{}
32 for _, f := range overlayFiles {
33 overlay[filepath.Join(dir, f)] = FromString(f + " overlay")
34 }
35
36 err := fsys.init(filepath.Join(dir, "foo"), overlay)
37 qt.Assert(t, qt.IsNil(err))
38 ffsys := fsys.ioFS(dir)
39 err = fstest.TestFS(ffsys, append(slices.Clip(onDiskFiles), overlayFiles...)...)
40 qt.Assert(t, qt.IsNil(err))
41 checked := make(map[string]bool)
42 for _, f := range overlayFiles {
43 data, err := fs.ReadFile(ffsys, f)
44 qt.Assert(t, qt.IsNil(err))
45 qt.Assert(t, qt.Equals(string(data), f+" overlay"))
46 checked[f] = true
47 }
48 for _, f := range onDiskFiles {
49 if checked[f] {
50 continue
51 }
52 data, err := fs.ReadFile(ffsys, f)
53 qt.Assert(t, qt.IsNil(err))
54 qt.Assert(t, qt.Equals(string(data), f))
55 }
56 }
57
58 func writeFile(t *testing.T, fpath string, content string) {
59 err := os.MkdirAll(filepath.Dir(fpath), 0o777)
60 qt.Assert(t, qt.IsNil(err))
61 err = os.WriteFile(fpath, []byte(content), 0o666)
62 qt.Assert(t, qt.IsNil(err))
63 }
64
View as plain text