This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
forked from kisielk/errcheck
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
224 lines (204 loc) · 5.34 KB
/
main_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
package main
import (
"bytes"
"io"
"os"
"regexp"
"strings"
"testing"
"github.com/golangci/errcheck/internal/errcheck"
)
func TestMain(t *testing.T) {
saveStderr := os.Stderr
saveStdout := os.Stdout
saveCwd, err := os.Getwd()
if err != nil {
t.Errorf("Cannot receive current directory: %v", err)
}
r, w, err := os.Pipe()
if err != nil {
t.Errorf("Cannot create pipe: %v", err)
}
os.Stderr = w
os.Stdout = w
bufChannel := make(chan string)
go func() {
buf := new(bytes.Buffer)
_, err = io.Copy(buf, r)
r.Close()
if err != nil {
t.Errorf("Cannot copy to buffer: %v", err)
}
bufChannel <- buf.String()
}()
exitCode := mainCmd([]string{"cmd name", "github.com/golangci/errcheck/testdata"})
w.Close()
os.Stderr = saveStderr
os.Stdout = saveStdout
os.Chdir(saveCwd)
out := <-bufChannel
if exitCode != exitUncheckedError {
t.Errorf("Exit code is %d, expected %d", exitCode, exitUncheckedError)
}
expectUnchecked := 15
if got := strings.Count(out, "UNCHECKED"); got != expectUnchecked {
t.Errorf("Got %d UNCHECKED errors, expected %d in:\n%s", got, expectUnchecked, out)
}
}
type parseTestCase struct {
args []string
paths []string
ignore map[string]string
tags []string
blank bool
asserts bool
error int
}
func TestParseFlags(t *testing.T) {
cases := []parseTestCase{
parseTestCase{
args: []string{"errcheck"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
tags: []string{},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-blank", "-asserts"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
tags: []string{},
blank: true,
asserts: true,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "foo", "bar"},
paths: []string{"foo", "bar"},
ignore: map[string]string{"fmt": dotStar.String()},
tags: []string{},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-ignore", "fmt:.*,encoding/binary:.*"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String(), "encoding/binary": dotStar.String()},
tags: []string{},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-ignore", "fmt:[FS]?[Pp]rint*"},
paths: []string{"."},
ignore: map[string]string{"fmt": "[FS]?[Pp]rint*"},
tags: []string{},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-ignore", "[rR]ead|[wW]rite"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String(), "": "[rR]ead|[wW]rite"},
tags: []string{},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-ignorepkg", "testing"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String(), "testing": dotStar.String()},
tags: []string{},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-ignorepkg", "testing,foo"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String(), "testing": dotStar.String(), "foo": dotStar.String()},
tags: []string{},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-tags", "foo"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
tags: []string{"foo"},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-tags", "foo bar !baz"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
tags: []string{"foo", "bar", "!baz"},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-tags", "foo bar !baz"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
tags: []string{"foo", "bar", "!baz"},
blank: false,
asserts: false,
error: exitCodeOk,
},
}
slicesEqual := func(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
ignoresEqual := func(a map[string]*regexp.Regexp, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if v.String() != b[k] {
return false
}
}
return true
}
for _, c := range cases {
checker := &errcheck.Checker{}
p, e := parseFlags(checker, c.args)
argsStr := strings.Join(c.args, " ")
if !slicesEqual(p, c.paths) {
t.Fatalf("%q: path got %q want %q", argsStr, p, c.paths)
}
if ign := checker.Ignore; !ignoresEqual(ign, c.ignore) {
t.Fatalf("%q: ignore got %q want %q", argsStr, ign, c.ignore)
}
if tags := checker.Tags; !slicesEqual(tags, c.tags) {
t.Fatalf("%q: tags got %v want %v", argsStr, tags, c.tags)
}
if b := checker.Blank; b != c.blank {
t.Fatalf("%q: blank got %v want %v", argsStr, b, c.blank)
}
if a := checker.Asserts; a != c.asserts {
t.Fatalf("%q: asserts got %v want %v", argsStr, a, c.asserts)
}
if e != c.error {
t.Fatalf("%q: error got %q want %q", argsStr, e, c.error)
}
}
}