forked from incu6us/goimports-reviser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs_test.go
150 lines (122 loc) · 2.62 KB
/
fs_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
// SPDX-FileCopyrightText: 2023 Christoph Mewes
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"strings"
"testing"
)
func TestDefaultExcludeFilterAgainstFilenames(t *testing.T) {
testcases := []struct {
filename string
expected bool
}{
{
filename: "main.go",
expected: false,
},
{
filename: "zz_generated.deepcopy.go",
expected: true,
},
{
filename: "zz_generated.go",
expected: true,
},
{
filename: "generated.pb.go",
expected: true,
},
}
for _, tt := range testcases {
t.Run(tt.filename, func(t *testing.T) {
skipped := isSkipped(tt.filename, defaultExcludes)
if skipped != tt.expected {
t.Errorf("Expected %v but got %v", tt.expected, skipped)
}
tt.filename = "pkg/" + tt.filename
skipped = isSkipped(tt.filename, defaultExcludes)
if skipped != tt.expected {
t.Errorf("Expected %v for %q, but got %v", tt.expected, tt.filename, skipped)
}
})
}
}
func TestIsGeneratedCode(t *testing.T) {
testcases := []struct {
comment string
expected bool
}{
{
comment: "",
expected: false,
},
{
comment: "// This file has been generated.",
expected: true,
},
{
comment: "// Code generated by MockGen. DO NOT EDIT.",
expected: true,
},
{
comment: "// Code generated by generate-imagename-constants.sh. DO NOT EDIT.",
expected: true,
},
{
comment: "// This file has been generated with Velero v1.5.3. Do not edit.",
expected: true,
},
}
for i, tt := range testcases {
code := fmt.Sprintf(`
%s
package main
func main() {
}
`, tt.comment)
t.Run(fmt.Sprintf("#%d vanilla", i+1), runGeneratedCodeTest(code, tt.expected))
code = fmt.Sprintf(`
// +build foo
%s
package main
func main() {
}
`, tt.comment)
t.Run(fmt.Sprintf("#%d with build constraint", i+1), runGeneratedCodeTest(code, tt.expected))
code = fmt.Sprintf(`
// +build foo
/*
I am a license header.
*/
%s
package main
func main() {
}
`, tt.comment)
t.Run(fmt.Sprintf("#%d with build constraint and license header", i+1), runGeneratedCodeTest(code, tt.expected))
code = fmt.Sprintf(`
// +build foo
/*
I am a license header.
*/
package main
%s
func main() {
}
`, tt.comment)
t.Run(fmt.Sprintf("#%d, but too late, so ignore it", i+1), runGeneratedCodeTest(code, false))
}
}
func runGeneratedCodeTest(code string, expected bool) func(t *testing.T) {
return func(t *testing.T) {
b := []byte(strings.TrimSpace(code))
generated, err := isGeneratedCode(b)
if err != nil {
t.Errorf("should not have errored, but got %v", err)
}
if generated != expected {
t.Errorf("Expected %v but got %v", expected, generated)
}
}
}