-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadfile_test.go
84 lines (66 loc) · 2.18 KB
/
readfile_test.go
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
package mergefs
import (
"embed"
"io/fs"
"os"
"strings"
"testing"
"testing/fstest"
"github.com/jcchavezs/mergefs/io"
"github.com/stretchr/testify/require"
)
func TestReadfile(t *testing.T) {
var aFS fs.ReadFileFS = fstest.MapFS{
"testdata": &fstest.MapFile{Mode: fs.ModeDir},
"testdata/a": &fstest.MapFile{Mode: fs.ModeDir},
"testdata/a/y": &fstest.MapFile{Mode: fs.ModeDir},
"testdata/a/y/foo.conf": &fstest.MapFile{Data: []byte("bar")},
}
var bFS fs.ReadFileFS = fstest.MapFS{
"testdata": &fstest.MapFile{Mode: fs.ModeDir},
"testdata/b": &fstest.MapFile{Mode: fs.ModeDir},
"testdata/b/y": &fstest.MapFile{Mode: fs.ModeDir},
"testdata/b/y/bar.conf": &fstest.MapFile{Data: []byte("bar")},
}
mfs := Merge(aFS, bFS)
t.Run("file exists", func(t *testing.T) {
content, err := fs.ReadFile(mfs, "testdata/a/y/foo.conf")
require.NoError(t, err)
require.Equal(t, "bar", string(content))
rfmfs, ok := mfs.(fs.ReadFileFS)
require.True(t, ok)
content, err = rfmfs.ReadFile("testdata/a/y/foo.conf")
require.NoError(t, err)
require.Equal(t, "bar", string(content))
})
t.Run("file does not exist", func(t *testing.T) {
_, err := fs.ReadFile(mfs, "testdata/a/y/baz.conf")
require.Error(t, err)
rfmfs, ok := mfs.(fs.ReadFileFS)
require.True(t, ok)
_, err = rfmfs.ReadFile("testdata/a/y/baz.conf")
require.Error(t, err)
})
}
//go:embed testdata
var testdataFS embed.FS
func TestAbsolutePath(t *testing.T) {
// Emulates github.com/corazawaf/coraza-coreruleset/blob/main/coreruleset.go usage of mergefs
rulesFS, err := fs.Sub(testdataFS, "testdata")
require.NoError(t, err)
mfs := Merge(rulesFS, io.OSFS)
f, err := os.CreateTemp(t.TempDir(), "fizz.conf")
require.NoError(t, err)
defer f.Close()
require.Equal(t, string(f.Name()[0]), "/")
_, err = fs.ReadFile(mfs, f.Name())
require.NoError(t, err)
rfmfs, ok := mfs.(fs.ReadFileFS)
require.True(t, ok)
_, err = rfmfs.ReadFile(f.Name())
require.NoError(t, err)
_, err = rfmfs.ReadFile("/tmp/doesnotexist.conf")
if !strings.Contains(err.Error(), "no such file or directory") {
t.Errorf("expected not found error. Got: %s", err.Error())
}
}