-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroveralls_test.go
435 lines (415 loc) · 11.3 KB
/
roveralls_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
"testing"
)
func TestRun(t *testing.T) {
initProgram(os.Args, os.Stdout, os.Stderr, os.Getenv("GOPATH"))
cases := []struct {
dir string
cmdArgs []string
wantExitCode int
wantOutRegexps []string
wantFiles []string
}{
{dir: "fixtures",
cmdArgs: []string{os.Args[0], "-covermode=count"},
wantExitCode: 0,
wantOutRegexps: []string{},
wantFiles: []string{
filepath.Join("fixtures", "good", "good.go"),
filepath.Join("fixtures", "good2", "good2.go"),
},
},
{dir: "fixtures",
cmdArgs: []string{os.Args[0], "-covermode=count", "-v"},
wantExitCode: 0,
wantOutRegexps: []string{
"^GOPATH: .*$",
"^Working dir: .*$",
"^No Go test files in dir: ., skipping$",
"^Processing dir: good$",
"^Processing: go test -covermode=count -coverprofile=profile.coverprofile -outputdir=.*$",
"^Processing dir: good2$",
"^Processing: go test -covermode=count -coverprofile=profile.coverprofile -outputdir=.*$",
"^No Go test files in dir: no-go-files, skipping$",
"^No Go test files in dir: no-test-files, skipping$",
"^Processing dir: short$",
"^Processing: go test -covermode=count -coverprofile=profile.coverprofile -outputdir=.*$",
},
wantFiles: []string{
filepath.Join("fixtures", "good", "good.go"),
filepath.Join("fixtures", "good2", "good2.go"),
},
},
{dir: "fixtures",
cmdArgs: []string{
os.Args[0],
"-covermode=count",
"-ignore=.git,vendor,good2",
"-short",
},
wantExitCode: 0,
wantOutRegexps: []string{},
wantFiles: []string{
filepath.Join("fixtures", "good", "good.go"),
filepath.Join("fixtures", "short", "short.go"),
},
},
{dir: "fixtures",
cmdArgs: []string{
os.Args[0],
"-covermode=count",
"-ignore=.git,vendor,good2",
"-v",
"-short",
},
wantExitCode: 0,
wantOutRegexps: []string{
"^GOPATH: .*$",
"^Working dir: .*$",
"^No Go test files in dir: ., skipping$",
"^Processing dir: good$",
"^Processing: go test -short -covermode=count -coverprofile=profile.coverprofile -outputdir=.*$",
"^No Go test files in dir: no-go-files, skipping$",
"^No Go test files in dir: no-test-files, skipping$",
"^Processing dir: short$",
"^Processing: go test -short -covermode=count -coverprofile=profile.coverprofile -outputdir=.*$",
},
wantFiles: []string{
filepath.Join("fixtures", "good", "good.go"),
filepath.Join("fixtures", "short", "short.go"),
},
},
{dir: "fixtures",
cmdArgs: []string{
os.Args[0],
"-covermode=count",
"-short",
},
wantExitCode: 0,
wantOutRegexps: []string{},
wantFiles: []string{
filepath.Join("fixtures", "good", "good.go"),
filepath.Join("fixtures", "good2", "good2.go"),
filepath.Join("fixtures", "short", "short.go"),
},
},
{dir: "fixtures",
cmdArgs: []string{os.Args[0], "-help"},
wantExitCode: 0,
wantOutRegexps: makeUsageMsgRegexps(),
wantFiles: []string{},
},
}
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
for _, c := range cases {
var gotOut bytes.Buffer
var gotErr bytes.Buffer
initProgram(c.cmdArgs, &gotOut, &gotErr, os.Getenv("GOPATH"))
if err := os.Chdir(wd); err != nil {
t.Fatalf("ChDir(%s) err: %s", c.dir, err)
}
if err := os.Chdir(c.dir); err != nil {
t.Fatalf("ChDir(%s) err: %s", c.dir, err)
}
os.Remove(filepath.Join("roveralls.coverprofile"))
exitCode := program.Run()
if exitCode != c.wantExitCode {
t.Errorf("Run: incorrect exit code, got: %d, want: %d",
exitCode, c.wantExitCode)
}
if gotErr.String() != "" {
t.Errorf("Run: gotErr: %s", gotErr.String())
}
if err := checkOutput(c.wantOutRegexps, gotOut.String()); err != nil {
t.Errorf("checkOutput: %s", err)
}
gotFiles, err := filesTested(wd, "roveralls.coverprofile")
if len(c.wantFiles) != 0 && err != nil {
t.Fatalf("filesTested err: %s", err)
}
if len(gotFiles) != len(c.wantFiles) {
t.Errorf("Wrong files tested (cmdArgs: %s). want: %s, got: %v",
c.cmdArgs, c.wantFiles, gotFiles)
}
for _, wantFile := range c.wantFiles {
if _, ok := gotFiles[wantFile]; !ok {
t.Errorf("No cover entries for file: %s", wantFile)
}
}
}
}
func TestRun_errors(t *testing.T) {
initProgram(os.Args, os.Stdout, os.Stderr, os.Getenv("GOPATH"))
cases := []struct {
dir string
cmdArgs []string
gopath string
wantExitCode int
wantOut string
wantErr string
}{
{dir: "fixtures",
cmdArgs: []string{os.Args[0], "-covermode=nothing"},
gopath: os.Getenv("GOPATH"),
wantExitCode: 1,
wantOut: "",
wantErr: "invalid covermode 'nothing'\n" + usageMsg(),
},
{dir: "fixtures",
cmdArgs: []string{os.Args[0], "-bob"},
gopath: os.Getenv("GOPATH"),
wantExitCode: 1,
wantOut: "",
wantErr: "flag provided but not defined: -bob\n" + usagePartialMsg(),
},
{dir: "fixtures",
cmdArgs: []string{os.Args[0], "-covermode=count"},
gopath: "",
wantExitCode: 1,
wantOut: "",
wantErr: "invalid GOPATH '.'\n",
},
{dir: "fixtures",
cmdArgs: []string{os.Args[0], "-covermode=count"},
gopath: ".",
wantExitCode: 1,
wantOut: "",
wantErr: "invalid GOPATH '.'\n",
},
}
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
for _, c := range cases {
var gotOut bytes.Buffer
var gotErr bytes.Buffer
initProgram(c.cmdArgs, &gotOut, &gotErr, c.gopath)
if err := os.Chdir(wd); err != nil {
t.Fatalf("ChDir(%s) err: %s", c.dir, err)
}
if err := os.Chdir(c.dir); err != nil {
t.Fatalf("ChDir(%s) err: %s", c.dir, err)
}
exitCode := program.Run()
if exitCode != c.wantExitCode {
t.Errorf("Run: incorrect exit code, got: %d, want: %d",
exitCode, c.wantExitCode)
}
if gotErr.String() != c.wantErr {
t.Errorf("Run: gotErr: %s, wantErr: %s", gotErr.String(), c.wantErr)
}
if gotOut.String() != c.wantOut {
t.Errorf("Run: gotOut: %s, wantOut: %s", gotOut.String(), c.wantOut)
}
}
}
func TestProcessDir_errors(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
cases := []struct {
cover string
path string
wantErr error
}{
{cover: "count",
path: ".",
wantErr: errors.New("can't create relative path"),
},
{cover: "count",
path: filepath.Join("fixtures", "nonexistant"),
wantErr: &os.PathError{
Op: "chdir",
Path: filepath.Join("fixtures", "nonexistant"),
Err: syscall.ENOENT,
},
},
{cover: "bob",
path: wd,
wantErr: goTestError{
stderr: "invalid flag argument for -covermode: \"bob\"",
stdout: "",
},
},
}
for i, c := range cases {
var gotOut bytes.Buffer
var pOut bytes.Buffer
program := &Program{cover: c.cover, out: &pOut, verbose: true}
err := program.processDir(wd, c.path, &gotOut)
checkErrorMatch(t, fmt.Sprintf("(%d) processDir: ", i), err, c.wantErr)
}
}
func TestUsage(t *testing.T) {
var gotErr bytes.Buffer
initProgram(os.Args, os.Stdout, &gotErr, os.Getenv("GOPATH"))
Usage()
want := usageMsg()
if gotErr.String() != want {
t.Errorf("Usage: got: %s, want: %s", gotErr.String(), want)
}
}
func TestGoTestErrorError(t *testing.T) {
err := goTestError{
stderr: "this is an error",
stdout: "baby did a bad bad thing",
}
want := "error from go test: this is an error\noutput: baby did a bad bad thing"
got := err.Error()
if got != want {
t.Errorf("Error() got: %s, want: %s", got, want)
}
}
func TestWalkingErrorError(t *testing.T) {
err := walkingError{
err: errors.New("this is an error"),
dir: "/tmp/someplace",
}
want := "could not walk working directory '/tmp/someplace': this is an error"
got := err.Error()
if got != want {
t.Errorf("Error() got: %s, want: %s", got, want)
}
}
/****************************
* Helper functions
****************************/
var fileTestedRegexp = regexp.MustCompile("^(.*?)(:\\d.*) (\\d+)$")
func makeUsageMsgRegexps() []string {
lines := strings.Split(usageMsg(), "\n")[:15]
r := make([]string, len(lines))
for i, l := range lines {
r[i] = regexp.QuoteMeta(l)
}
return r
}
func checkOutput(wantRegexp []string, gotOut string) error {
gotOutStrs := strings.Split(gotOut, "\n")
gotOutStrs = gotOutStrs[:len(gotOutStrs)-1]
if len(wantRegexp) != len(gotOutStrs) {
return fmt.Errorf("wantRegexp has %d lines, gotOut has %d lines",
len(wantRegexp), len(gotOutStrs))
}
i := 0
for _, r := range wantRegexp {
compiledRegexp, err := regexp.Compile(r)
if err != nil {
return err
}
if !compiledRegexp.MatchString(gotOutStrs[i]) {
return fmt.Errorf("line doesn't match got: %s, want: %s",
gotOutStrs[i], r)
}
i++
}
return nil
}
func filesTested(wd string, filename string) (map[string]bool, error) {
files := map[string]bool{}
gopath := filepath.Clean(os.Getenv("GOPATH"))
if len(gopath) == 0 {
return files, fmt.Errorf("invalid GOPATH '%s'", gopath)
}
srcpath := filepath.Join(gopath, "src")
project, err := filepath.Rel(srcpath, wd)
if err != nil {
return files, err
}
project = filepath.Clean(project)
f, err := os.Open(filename)
if err != nil {
return files, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if fileTestedRegexp.MatchString(line) {
file := fileTestedRegexp.ReplaceAllString(line, "$1")
file, err = filepath.Rel(project, file)
if err != nil {
return files, err
}
count := fileTestedRegexp.ReplaceAllString(line, "$3")
if count != "0" {
files[file] = true
}
}
}
return files, scanner.Err()
}
func checkErrorMatch(t *testing.T, context string, got, want error) {
if got == nil && want == nil {
return
}
if got == nil || want == nil {
t.Errorf("%s got err: %s, want : %s", context, got, want)
return
}
switch x := want.(type) {
case *os.PathError:
if err := checkPathErrorMatch(got, x); err != nil {
t.Errorf("%s %s", context, err)
}
return
case goTestError:
if err := checkGoTestErrorMatch(got, x); err != nil {
t.Errorf("%s %s", context, err)
}
return
}
if got.Error() != want.Error() {
t.Errorf("%s got err: %s, want : %s", context, got, want)
}
}
func checkPathErrorMatch(checkErr error, wantErr *os.PathError) error {
perr, ok := checkErr.(*os.PathError)
if !ok {
return fmt.Errorf("got err type: %T, want error type: os.PathError",
checkErr)
}
if perr.Op != wantErr.Op {
return fmt.Errorf("got perr.Op: %s, want: %s", perr.Op, wantErr.Op)
}
if filepath.Clean(perr.Path) != filepath.Clean(wantErr.Path) {
return fmt.Errorf("got perr.Path: %s, want: %s", perr.Path, wantErr.Path)
}
if perr.Err != wantErr.Err {
return fmt.Errorf("got perr.Err: %s, want: %s", perr.Err, wantErr.Err)
}
return nil
}
func checkGoTestErrorMatch(checkErr error, wantErr goTestError) error {
gerr, ok := checkErr.(goTestError)
if !ok {
return fmt.Errorf("got err type: %T, want error type: goTestError",
checkErr)
}
if strings.Trim(gerr.stderr, " \n") != strings.Trim(wantErr.stderr, " \n") {
return fmt.Errorf("got gerr.stderr: %s, want: %s",
gerr.stderr, wantErr.stderr)
}
if gerr.stdout != wantErr.stdout {
return fmt.Errorf("got gerr.stdout: %s, want: %s",
gerr.stdout, wantErr.stdout)
}
return nil
}