-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathlinter_test.go
180 lines (144 loc) · 4.36 KB
/
linter_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
177
178
179
180
package linter
import (
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"testing"
jsonnet "github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/internal/testutils"
)
var update = flag.Bool("update", false, "update .golden files")
type linterTest struct {
name string
input string
output string
}
type ChangedGoldensList struct {
changedGoldens []string
}
func runTest(t *testing.T, test *linterTest, changedGoldensList *ChangedGoldensList) {
read := func(file string) []byte {
bytz, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("reading file: %s: %v", file, err)
}
return bytz
}
input := read(test.input)
vm := jsonnet.MakeVM()
var outBuilder strings.Builder
errorsFound := LintSnippet(vm, &outBuilder, []Snippet{Snippet{FileName: test.name, Code: string(input)}})
outData := outBuilder.String()
if outData == "" && errorsFound {
t.Error(fmt.Errorf("return value indicates problems present, but no output was produced"))
}
if outData != "" && !errorsFound {
t.Error(fmt.Errorf("return value indicates no problems, but output is not empty:\n%v", outData))
}
if *update {
changed, err := testutils.UpdateGoldenFile(test.output, []byte(outData), 0666)
if err != nil {
t.Error(err)
}
if changed {
changedGoldensList.changedGoldens = append(changedGoldensList.changedGoldens, test.output)
}
} else {
golden, err := ioutil.ReadFile(test.output)
if err != nil {
t.Error(err)
return
}
if diff, hasDiff := testutils.CompareWithGolden(outData, golden); hasDiff {
t.Error(fmt.Errorf("golden file %v has diff:\n%v", test.input, diff))
}
}
}
func runTests(t *testing.T, tests []*linterTest) {
read := func(file string) []byte {
bytz, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("reading file: %s: %v", file, err)
}
return bytz
}
vm := jsonnet.MakeVM()
var snippets []Snippet
for _, test := range tests {
input := read(test.input)
snippets = append(snippets, Snippet{FileName: test.name, Code: string(input)})
}
var outBuilder strings.Builder
errorsFound := LintSnippet(vm, &outBuilder, snippets)
outData := outBuilder.String()
if outData == "" && errorsFound {
t.Error(fmt.Errorf("return value indicates problems present, but no output was produced"))
}
if outData != "" && !errorsFound {
t.Error(fmt.Errorf("return value indicates no problems, but output is not empty:\n%v", outData))
}
// passing as args for both importing file and imported file.
var import_snippets []Snippet
for _, filepath := range []string{"testdata/import.jsonnet", "testdata/call_integer.jsonnet"} {
input := read(filepath)
import_snippets = append(import_snippets, Snippet{FileName: filepath, Code: string(input)})
}
errorsFound = LintSnippet(vm, &outBuilder, import_snippets)
outData = outBuilder.String()
if outData == "" && errorsFound {
t.Error(fmt.Errorf("return value indicates problems present, but no output was produced"))
}
if outData != "" && !errorsFound {
t.Error(fmt.Errorf("return value indicates no problems, but output is not empty:\n%v", outData))
}
}
func TestLinter(t *testing.T) {
flag.Parse()
var tests []*linterTest
match, err := filepath.Glob("testdata/*.jsonnet")
if err != nil {
t.Fatal(err)
}
matchRegular, err := filepath.Glob("../testdata/*.jsonnet")
if err != nil {
t.Fatal(err)
}
match = append(match, matchRegular...)
jsonnetExtRE := regexp.MustCompile(`\.jsonnet$`)
for _, input := range match {
// Skip escaped filenames.
if strings.ContainsRune(input, '%') {
continue
}
name := jsonnetExtRE.ReplaceAllString(input, "")
golden := jsonnetExtRE.ReplaceAllString(input, ".linter.golden")
tests = append(tests, &linterTest{
name: name,
input: input,
output: golden,
})
}
changedGoldensList := ChangedGoldensList{}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
runTest(t, test, &changedGoldensList)
})
}
if *update {
// Little hack: a failed test which pritns update stats.
t.Run("Goldens Updated", func(t *testing.T) {
t.Logf("Expected failure, for printing update stats. Does not appear without `-update`.")
t.Logf("%d linter goldens updated:\n", len(changedGoldensList.changedGoldens))
for _, golden := range changedGoldensList.changedGoldens {
t.Log(golden)
}
t.Fail()
})
}
t.Run("passing multiple input files", func(t *testing.T) {
runTests(t, tests)
})
}