-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_test.go
74 lines (64 loc) · 1.73 KB
/
filter_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
package factory
import (
"testing"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/stretchr/testify/assert"
)
func TestDecodeFilterBlock(t *testing.T) {
parser := hclparse.NewParser()
file, _ := parser.ParseHCL([]byte(`
filter {
include {
paths = ["hi/*"]
branches = ["bye/*"]
}
exclude {
paths = ["bar/*"]
branches = ["foo/*"]
}
}
stages = []
`), "test")
pipeline, diags := file.Body.Content(pipelineBlockSchema)
if diags.HasErrors() {
t.Fatalf("Error decoding filter block: %s", diags)
}
filter, d := decodeFilterBlock(pipeline.Blocks[0])
if d.HasErrors() {
t.Fatalf("Error decoding filter block: %s", d)
}
assert.Equal(t, []string{"hi/*"}, filter.Include.Paths)
assert.Equal(t, []string{"bye/*"}, filter.Include.Branches)
assert.Equal(t, []string{"bar/*"}, filter.Exclude.Paths)
assert.Equal(t, []string{"foo/*"}, filter.Exclude.Branches)
}
func TestDecodeFilterBlockReturnsErrorForIncorrectType(t *testing.T) {
parser := hclparse.NewParser()
file, _ := parser.ParseHCL([]byte(`
filter {
include {
paths = [123]
branches = [123]
}
exclude {
paths = [123]
branches = [123]
}
}
stages = []
`), "test")
pipeline, diags := file.Body.Content(pipelineBlockSchema)
if diags.HasErrors() {
t.Fatalf("Error decoding filter block: %s", diags)
}
_, d := decodeFilterBlock(pipeline.Blocks[0])
if !d.HasErrors() {
t.Error("Expected diags to have error but was empty")
}
errs := d.Errs()
assert.Len(t, errs, 4, "Expected diags to have 4 errors, got %d", len(errs))
for _, err := range d {
assert.Equal(t, "Value within paths or branches must be of string type", err.Summary)
assert.Equal(t, "Invalid type for branch or path", err.Detail)
}
}