-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck_test.go
More file actions
114 lines (111 loc) · 2.79 KB
/
check_test.go
File metadata and controls
114 lines (111 loc) · 2.79 KB
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
package diffence
import (
"bytes"
"io"
"reflect"
"testing"
)
func TestCheckDiffs(t *testing.T) {
type args struct {
r io.Reader
rules *[]Rule
ignorer Matcher
}
ruleSingle := getRuleFile("test/fixtures/rules/rules.json")
ruleMulti := getRuleFile("test/fixtures/rules/rules_multi.json")
rulesExtended := getRuleFile("test/fixtures/rules/rules_extended_regex.json")
tests := []struct {
name string
args args
want Result
}{
{
name: "Recognises an offensive diff - sqlpassword.diff",
args: args{
r: getFixtureFile("test/fixtures/diffs/sqlpassword.diff"),
rules: rulesExtended,
},
want: Result{
Matched: true,
MatchedRules: MatchedRules{
"6f41b9b0a8150e165cd297ae3e00129766cf8a9b:web/src/main/resources/db/migration/V0_4__AdminPassword.sql": *rulesExtended,
"4cc087a1b4731d1017844cc86323df43068b0409:web/src/main/resources/db/migration/V0_2__SeedData.sql": []Rule{(*rulesExtended)[0]},
},
},
},
{
name: "Recognises an offensive diff",
args: args{
r: getFixtureFile("test/fixtures/diffs/single_fail.diff"),
rules: ruleSingle,
},
want: Result{
Matched: true,
MatchedRules: MatchedRules{
"path/to/password.txt": *ruleSingle,
},
},
},
{
name: "Recognises an offensive diff - single_fail.diff",
args: args{
r: getFixtureFile("test/fixtures/diffs/single_fail.diff"),
rules: ruleMulti,
},
want: Result{
Matched: true,
MatchedRules: MatchedRules{
"path/to/password.txt": *ruleSingle,
},
},
},
{
name: "Recognises an offensive diff - multi_fail.diff",
args: args{
r: getFixtureFile("test/fixtures/diffs/multi_fail.diff"),
rules: ruleMulti,
},
want: Result{
Matched: true,
MatchedRules: MatchedRules{
"path/to/password.txt": []Rule{(*ruleMulti)[0]},
"another/file/aws.pem": []Rule{(*ruleMulti)[1]},
},
},
},
{
name: "Recognises an offensive diff - but excludes file in ignorer",
args: args{
r: getFixtureFile("test/fixtures/diffs/multi_fail.diff"),
rules: ruleMulti,
ignorer: Ignorer{patterns: []string{"another/file/*.pem"}},
},
want: Result{
Matched: true,
MatchedRules: MatchedRules{
"path/to/password.txt": []Rule{(*ruleMulti)[0]},
},
},
},
{
name: "Recognises non diff text",
args: args{
r: bytes.NewReader([]byte("not a diff")),
rules: ruleMulti,
},
want: Result{
Matched: false,
MatchedRules: MatchedRules{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dc := DiffChecker{Rules: tt.args.rules, Ignorer: tt.args.ignorer}
got, _ := dc.Check(tt.args.r)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CheckDiffs(): %s\n\n got:%#v\n\nwant: %#v", tt.name, got, tt.want)
}
})
}
}