-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
176 lines (174 loc) · 5.57 KB
/
config_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package directive
import (
"github.com/google/go-cmp/cmp"
"github.com/vektah/gqlparser/v2/ast"
"os"
"testing"
)
func TestParseConfigFile(t *testing.T) {
type args struct {
configFile string
}
tests := []struct {
name string
args args
want *Config
wantErr bool
}{
{
name: "constraint directive",
args: args{
configFile: "./testdata/constraint/directive.yaml",
},
want: &Config{
Analyzer: []*AnalyzerConfig{
{
AnalyzerName: "constraint directive",
Description: "constraint directive exists on the field",
FieldConfig: []*FieldConfig{
{
Description: "constraint directive exists on the input field",
Directive: "constraint",
Kinds: []ast.DefinitionKind{ast.InputObject},
FieldParentTypePatterns: []string{`.+`},
FieldTypePatterns: []string{`^\[?Int\]?$`, `^\[?Float\]?$`, `^\[?String\]?$`, `^\[?Decimal\]?$`, `^\[?URL\]?$`},
ExcludeFieldPatterns: []string{`^first$`, `^last$`, `^after$`, `^before$`},
ReportFormat: "%s has no constraint directive",
},
},
ArgumentConfig: []*ArgumentConfig{
{
Description: "constraint directive exists on the object field argument",
Directive: "constraint",
Kinds: []ast.DefinitionKind{ast.Object},
ArgumentTypePatterns: []string{`^\[?Int\]?$`, `^\[?Float\]?$`, `^\[?String\]?$`, `^\[?Decimal\]?$`, `^\[?URL\]?$`},
ExcludeArgumentPatterns: []string{`^first$`, `^last$`, `^after$`, `^before$`},
ReportFormat: "argument %s has no constraint directive",
},
},
},
},
},
wantErr: false,
},
{
name: "id directive",
args: args{
configFile: "./testdata/id/directive.yaml",
},
want: &Config{
Analyzer: []*AnalyzerConfig{
{
AnalyzerName: "id directive",
Description: "id directive exists on the field",
FieldConfig: []*FieldConfig{
{
Description: "id directive exists on the input field",
Directive: "id",
Kinds: []ast.DefinitionKind{ast.InputObject},
FieldParentTypePatterns: []string{`.+`},
FieldTypePatterns: []string{`^\[?ID\]?$`},
ReportFormat: "%s has no id directive",
},
},
ArgumentConfig: []*ArgumentConfig{
{
Description: "id directive exists on the object field argument",
Directive: "id",
Kinds: []ast.DefinitionKind{ast.Object},
ArgumentTypePatterns: []string{`^\[?ID\]?$`},
ReportFormat: "argument %s has no id directive",
},
},
},
},
},
wantErr: false,
},
{
name: "list directive",
args: args{
configFile: "./testdata/list/directive.yaml",
},
want: &Config{
Analyzer: []*AnalyzerConfig{
{
AnalyzerName: "list directive",
Description: "list directive exists on the array field",
FieldConfig: []*FieldConfig{
{
Description: "list directive exists on the input array field",
Directive: "list",
Kinds: []ast.DefinitionKind{ast.InputObject},
FieldParentTypePatterns: []string{`.+`},
FieldTypePatterns: []string{`\[.+\]`},
ReportFormat: "%s has no list directive",
},
},
ArgumentConfig: []*ArgumentConfig{
{
Description: "list directive exists on the object field array argument",
Directive: "list",
Kinds: []ast.DefinitionKind{ast.Object},
ArgumentTypePatterns: []string{`\[.+\]`},
ReportFormat: "argument %s has no list directive",
},
},
},
},
},
wantErr: false,
},
{
name: "permission directive",
args: args{
configFile: "./testdata/permission/directive.yaml",
},
want: &Config{
Analyzer: []*AnalyzerConfig{
{
AnalyzerName: "permission directive",
Description: "permission directive exists on the definition",
DefinitionConfig: []*DefinitionConfig{
{
Description: "permission directive exists on the definition",
Directive: "permission",
Kinds: []ast.DefinitionKind{ast.Object, ast.Interface},
DefinitionPatterns: []string{".+"},
ExcludeDefinitionPatterns: []string{"^Query$", "^Mutation$", "^Subscription$", "^PageInfo$"},
ReportFormat: "%s has no permission directive",
},
},
FieldConfig: []*FieldConfig{
{
Description: "permission directive exists on the mutation",
Directive: "permission",
Kinds: []ast.DefinitionKind{ast.Object},
FieldParentTypePatterns: []string{`^Mutation$`},
FieldTypePatterns: []string{".+"},
ReportFormat: "%s has no permission directive",
},
},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
configFile, err := os.Open(tt.args.configFile)
if err != nil {
t.Fatalf("failed to open config file: %v", err)
}
got, err := ParseConfigFile(configFile)
if (err != nil) != tt.wantErr {
t.Errorf("ParseConfigFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("ParseConfigFile()\n%v", diff)
}
})
}
}