-
Notifications
You must be signed in to change notification settings - Fork 2
/
fix_test.go
327 lines (277 loc) · 6.65 KB
/
fix_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package eclint
import (
"bytes"
"context"
"io"
"testing"
"github.com/editorconfig/editorconfig-core-go/v2"
"github.com/google/go-cmp/cmp"
)
func TestFixEndOfLine(t *testing.T) { //nolint:gocognit
tests := []struct {
Name string
Lines [][]byte
}{
{
Name: "a file with many lines",
Lines: [][]byte{
[]byte("A file"),
[]byte("With many lines"),
},
},
{
Name: "a file with many lines and a final newline",
Lines: [][]byte{
[]byte("A file"),
[]byte("With many lines"),
[]byte("and a final newline."),
[]byte(""),
},
},
}
ctx := context.TODO()
for _, tc := range tests {
tc := tc
file := bytes.Join(tc.Lines, []byte("\n"))
fileSize := int64(len(file))
// Test the nominal case
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
def, err := newDefinition(&editorconfig.Definition{
EndOfLine: editorconfig.EndOfLineLf,
})
if err != nil {
t.Fatal(err)
}
r := bytes.NewReader(file)
out, fixed, err := fix(ctx, r, fileSize, "utf-8", def)
if err != nil {
t.Fatalf("no errors where expected, got %s", err)
}
if fixed {
t.Errorf("file should not have been fixed")
}
result, err := io.ReadAll(out)
if err != nil {
t.Fatalf("cannot read result %s", err)
}
if !cmp.Equal(file, result) {
t.Errorf("diff %s", cmp.Diff(file, result))
}
})
// Test the inverse
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
def, err := newDefinition(&editorconfig.Definition{
EndOfLine: editorconfig.EndOfLineCrLf,
})
if err != nil {
t.Fatal(err)
}
r := bytes.NewReader(file)
out, fixed, err := fix(ctx, r, fileSize, "utf-8", def)
if err != nil {
t.Fatalf("no errors where expected, got %s", err)
}
if !fixed {
t.Errorf("file should have been fixed")
}
result, err := io.ReadAll(out)
if err != nil {
t.Fatalf("cannot read result %s", err)
}
if cmp.Equal(file, result) {
t.Errorf("no differences, the file was not fixed")
}
})
}
}
func TestFixIndentStyle(t *testing.T) {
tests := []struct {
Name string
IndentSize string
IndentStyle string
File []byte
}{
{
Name: "space to tab",
IndentStyle: "tab",
IndentSize: "2",
File: []byte("\t\t \tA line\n"),
},
{
Name: "tab to space",
IndentStyle: "space",
IndentSize: "2",
File: []byte("\t\t \tA line\n"),
},
}
ctx := context.TODO()
for _, tc := range tests {
tc := tc
fileSize := int64(len(tc.File))
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
def, err := newDefinition(&editorconfig.Definition{
EndOfLine: "lf",
IndentStyle: tc.IndentStyle,
IndentSize: tc.IndentSize,
})
if err != nil {
t.Fatal(err)
}
if err := indentStyle(tc.IndentStyle, def.IndentSize, tc.File); err == nil {
t.Errorf("the initial file should fail")
}
r := bytes.NewReader(tc.File)
out, _, err := fix(ctx, r, fileSize, "utf-8", def)
if err != nil {
t.Fatalf("no errors where expected, got %s", err)
}
result, err := io.ReadAll(out)
if err != nil {
t.Fatalf("cannot read result %s", err)
}
if cmp.Equal(tc.File, result) {
t.Errorf("no changes!?")
}
if err := indentStyle(tc.IndentStyle, def.IndentSize, result); err != nil {
t.Errorf("no errors were expected, got %s", err)
}
})
}
}
func TestFixTrimTrailingWhitespace(t *testing.T) {
tests := []struct {
Name string
Lines [][]byte
}{
{
Name: "space",
Lines: [][]byte{
[]byte("A file"),
[]byte(" with spaces "),
[]byte(" at the end "),
[]byte(" "),
},
},
{
Name: "tabs",
Lines: [][]byte{
[]byte("A file"),
[]byte(" with tabs\t"),
[]byte(" at the end\t\t"),
[]byte("\t"),
},
},
{
Name: "tabs and spaces",
Lines: [][]byte{
[]byte("A file"),
[]byte(" with tabs\t\t "),
[]byte(" and spaces\t \t"),
[]byte(" at the end \t"),
},
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
for _, l := range tc.Lines {
m, _ := fixTrailingWhitespace(l)
err := checkTrimTrailingWhitespace(m)
if err != nil {
t.Errorf("no errors were expected. %s", err)
}
}
})
}
}
func TestFixInsertFinalNewline(t *testing.T) {
eolVariants := [][]byte{
{cr},
{lf},
{cr, lf},
}
insertFinalNewlineVariants := []bool{true, false}
newlinesAtEOLVariants := []int{0, 1, 3}
type Test struct {
InsertFinalNewline bool
File []byte
EolVariant []byte
NewlinesAtEOL int
}
tests := make([]Test, 0, 54)
// single line tests
singleLineFile := []byte(`A single line file.`)
for _, eolVariant := range eolVariants {
for _, insertFinalNewlineVariant := range insertFinalNewlineVariants {
for newlinesAtEOL := range newlinesAtEOLVariants {
file := singleLineFile
for i := 0; i < newlinesAtEOL; i++ {
file = append(file, eolVariant...)
}
tests = append(tests,
Test{
InsertFinalNewline: insertFinalNewlineVariant,
File: file,
EolVariant: eolVariant,
NewlinesAtEOL: newlinesAtEOL,
},
)
}
}
}
// multiline tests
multilineComponents := [][]byte{[]byte(`A`), []byte(`multiline`), []byte(`file.`)}
for _, eolVariant := range eolVariants {
multilineFile := bytes.Join(multilineComponents, eolVariant)
for _, insertFinalNewlineVariant := range insertFinalNewlineVariants {
for newlinesAtEOL := range newlinesAtEOLVariants {
file := multilineFile
for i := 0; i < newlinesAtEOL; i++ {
file = append(file, eolVariant...)
}
tests = append(tests,
Test{
InsertFinalNewline: insertFinalNewlineVariant,
File: file,
EolVariant: eolVariant,
NewlinesAtEOL: newlinesAtEOL,
},
)
}
}
}
// empty file tests
emptyFile := []byte("")
for _, eolVariant := range eolVariants {
for _, insertFinalNewlineVariant := range insertFinalNewlineVariants {
tests = append(tests,
Test{
InsertFinalNewline: insertFinalNewlineVariant,
File: emptyFile,
EolVariant: eolVariant,
},
)
}
}
for _, tc := range tests {
tc := tc
t.Run("TestFixInsertFinalNewline", func(t *testing.T) {
t.Parallel()
buf := bytes.Buffer{}
buf.Write(tc.File)
before := buf.Bytes()
fixInsertFinalNewline(&buf, tc.InsertFinalNewline, tc.EolVariant)
after := buf.Bytes()
err := checkInsertFinalNewline(buf.Bytes(), tc.InsertFinalNewline)
if err != nil {
t.Logf("before: %q", string(before))
t.Logf("after: %q", string(after))
t.Errorf("encountered error %s with test configuration %+v", err, tc)
}
})
}
}