-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtemplates_test.go
128 lines (119 loc) · 2.8 KB
/
templates_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package policylib_test
import (
"io/fs"
"os"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
policylib "github.com/spacelift-io/spacelift-policies-example-library"
)
func TestTemplates(t *testing.T) {
require.NotPanics(t, func() {
templates := policylib.Templates()
require.Greater(t, len(templates), 1, "should have templates")
assertUniqueNames(t, templates, "template names must be unique")
}, "embedded templates should not panic")
}
func assertUniqueNames(t *testing.T, templates []policylib.Template, msg string) int {
set := map[string]bool{}
for _, template := range templates {
if set[template.Name] {
assert.Failf(t, "duplicate template name: "+template.Name, msg)
}
set[template.Name] = true
}
return len(set)
}
func TestTemplates_Parsing(t *testing.T) {
tests := []struct {
name string
fs fs.FS
wantErr bool
wantErrContains string
wantTemplates []policylib.Template
}{
{
name: "empty file system",
fs: fstest.MapFS{},
wantErr: false,
wantTemplates: nil,
},
{
name: "no templates",
fs: fstest.MapFS{
"noise.txt": {},
},
wantErr: false,
wantTemplates: nil,
},
{
name: "missing metadata",
fs: fstest.MapFS{
"foo.rego": {},
},
wantErr: false,
wantTemplates: nil,
},
{
name: "empty metadata",
fs: fstest.MapFS{
"foo.rego": {},
"foo.yml": {},
},
wantErr: true,
wantErrContains: "EOF",
},
{
name: "minimal",
fs: os.DirFS("./testdata/minimal"),
wantErr: false,
wantTemplates: []policylib.Template{
{
Name: "foo",
Type: "push",
Description: "",
Labels: nil,
Body: "package foo\n",
},
},
},
{
name: "full",
fs: os.DirFS("./testdata/full"),
wantErr: false,
wantTemplates: []policylib.Template{
{
Name: "foo",
Type: "push",
Description: "something\n",
Labels: []string{"foo", "test"},
Body: "package foo\n",
},
},
},
{
name: "invalid policy type",
fs: os.DirFS("./testdata/invalid-policy-type"),
wantErr: true,
wantErrContains: "type",
},
{
name: "missing name",
fs: os.DirFS("./testdata/missing-name"),
wantErr: true,
wantErrContains: "name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
templates, err := policylib.EParse(tt.fs)
if tt.wantErr {
assert.ErrorContains(t, err, tt.wantErrContains, "error should contain expected text")
} else {
assert.NoError(t, err, "no error is expected")
assert.Equal(t, tt.wantTemplates, templates, "templates should match")
}
})
}
}