forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilefs_test.go
64 lines (52 loc) · 1.4 KB
/
filefs_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
//go:build go1.16
// +build go1.16
package bundle
import (
"os"
"strings"
"testing"
"testing/fstest"
)
func TestFSLoader(t *testing.T) {
archiveFS := make(fstest.MapFS)
for k, v := range archiveFiles {
file := strings.TrimPrefix(k, "/")
archiveFS[file] = &fstest.MapFile{
Data: []byte(v),
}
}
loader, err := NewFSLoader(archiveFS)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
testLoader(t, loader, "", archiveFiles)
}
func TestFSLoaderWithFilter(t *testing.T) {
files := map[string]string{
"/a/data.json": `{"foo": "not-bar"}`,
"/policy.rego": "package foo\n p = 1",
"/policy_test.rego": "package foo\n test_p { p }",
"/a/b/c/policy.rego": "package bar\n q = 1",
"/a/b/c/policy_test.rego": "package bar\n test_q { q }",
}
expectedFiles := map[string]string{
"/a/data.json": `{"foo": "not-bar"}`,
"/policy.rego": "package foo\n p = 1",
"/a/b/c/policy.rego": "package bar\n q = 1",
}
archiveFS := make(fstest.MapFS)
for k, v := range files {
file := strings.TrimPrefix(k, "/")
archiveFS[file] = &fstest.MapFile{
Data: []byte(v),
}
}
loader, err := NewFSLoader(archiveFS)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
loader.WithFilter(func(abspath string, info os.FileInfo, depth int) bool {
return getFilter("*_test.rego", 1)(abspath, info, depth)
})
testLoader(t, loader, "", expectedFiles)
}