forked from confluentinc/bincover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_bin_test.go
600 lines (587 loc) · 16.6 KB
/
run_bin_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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package bincover
import (
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
// Build necessary binaries before executing unit tests.
buildTestCmd := exec.Command("go", []string{"test", "./test_bins", "-tags", "testrunmain", "-coverpkg=./...", "-c", "-o", "set_covermode"}...)
output, err := buildTestCmd.CombinedOutput()
if err != nil {
log.Println(output)
panic(err)
}
exitCode := m.Run()
err = os.Remove("set_covermode")
if err != nil {
panic(err)
}
os.Exit(exitCode)
}
func TestCoverageCollector_Setup(t *testing.T) {
type fields struct {
MergedCoverageFilename string
CollectCoverage bool
setupFinished bool
tmpArgsFilePrefix string
}
tests := []struct {
name string
fields fields
wantErr bool
errMessage string
}{
{
name: "succeed setting up",
fields: fields{
MergedCoverageFilename: "test-file.out",
CollectCoverage: false,
setupFinished: true,
tmpArgsFilePrefix: defaultTmpArgsFilePrefix,
},
wantErr: false,
},
{
name: "fail setting up with empty filename",
fields: fields{
MergedCoverageFilename: "",
CollectCoverage: true,
setupFinished: false,
tmpArgsFilePrefix: defaultTmpArgsFilePrefix,
},
wantErr: true,
errMessage: "merged coverage profile filename cannot be empty when CollectCoverage is true",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CoverageCollector{
MergedCoverageFilename: tt.fields.MergedCoverageFilename,
CollectCoverage: tt.fields.CollectCoverage,
}
if err := c.Setup(); (err != nil) != tt.wantErr {
t.Errorf("Setup() error = %v, wantErr %v", err, tt.wantErr)
} else if tt.wantErr && tt.errMessage != "" {
require.EqualError(t, err, tt.errMessage)
}
require.Equal(t, tt.fields.setupFinished, c.setupFinished)
})
}
}
func TestCoverageCollector_TearDown(t *testing.T) {
type fields struct {
MergedCoverageFilename string
CollectCoverage bool
coverMode string
tmpCoverageFiles []*os.File
}
tests := []struct {
name string
fields fields
wantErr bool
wantPanic bool
panicMessage string
errMessageContains string
errMessage string
mergedFileContents string
}{
{
name: "succeed tearing down with no tests",
fields: fields{},
wantErr: false,
},
{
name: "succeed tearing down with tests",
fields: fields{
MergedCoverageFilename: "temp_merged.out",
CollectCoverage: true,
tmpCoverageFiles: func() []*os.File {
f1 := tempFileWithContent(t, "mode: set\nfirst file\n")
f2 := tempFileWithContent(t, "mode: set\nsecond file\n")
f3 := tempFileWithContent(t, "mode: set\nthird file\n")
return []*os.File{f1, f2, f3}
}(),
coverMode: "set",
},
mergedFileContents: "mode: set\nfirst file\nsecond file\nthird file",
wantErr: false,
},
{
name: "fail tearing down with missing coverage mode",
fields: fields{
MergedCoverageFilename: "temp_merged.out",
CollectCoverage: true,
tmpCoverageFiles: func() []*os.File {
f1 := tempFileWithContent(t, "mode: set\nfirst file\n")
missingHeaderFile := tempFileWithContent(t, "second file\n")
return []*os.File{f1, missingHeaderFile}
}(),
},
wantErr: true,
errMessage: "error parsing coverage profile: missing coverage mode from coverage profile. Maybe the file got corrupted while writing?",
},
{
name: "fail tearing down with missing temp coverage profiles",
fields: fields{
MergedCoverageFilename: "temp_merged.out",
CollectCoverage: true,
tmpCoverageFiles: func() []*os.File {
f := tempFile(t)
require.NoError(t, f.Close())
return []*os.File{f}
}(),
},
wantErr: true,
errMessageContains: "error reading temp coverage profiles",
},
{
name: "fail tearing down with invalid merged coverage filename",
fields: fields{
MergedCoverageFilename: "inval?df!l3Nam3/.%",
CollectCoverage: true,
tmpCoverageFiles: func() []*os.File {
f1 := tempFileWithContent(t, "mode: set\nfirst file\n")
f2 := tempFileWithContent(t, "mode: set\nsecond file\n")
f3 := tempFileWithContent(t, "mode: set\nthird file\n")
return []*os.File{f1, f2, f3}
}(),
},
wantErr: true,
errMessageContains: "error writing merged coverage profile",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CoverageCollector{
MergedCoverageFilename: tt.fields.MergedCoverageFilename,
CollectCoverage: tt.fields.CollectCoverage,
coverMode: tt.fields.coverMode,
tmpCoverageFiles: tt.fields.tmpCoverageFiles,
}
if tt.wantPanic {
require.PanicsWithValue(t, tt.panicMessage, func() { _ = c.TearDown() })
return
}
if err := c.TearDown(); (err != nil) != tt.wantErr {
t.Errorf("TearDown() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil && tt.errMessageContains != "" {
require.Contains(t, err.Error(), tt.errMessageContains)
} else if err != nil && tt.errMessage != "" {
require.EqualError(t, err, tt.errMessage)
}
if c.MergedCoverageFilename != "" && !tt.wantErr {
defer os.Remove(c.MergedCoverageFilename)
buf, err := ioutil.ReadFile(c.MergedCoverageFilename)
require.NoError(t, err)
contents := string(buf)
require.Equal(t, tt.mergedFileContents, contents)
}
})
}
}
func TestCoverageCollector_removeTempFiles(t *testing.T) {
tests := []struct {
name string
tmpCoverageFiles []*os.File
tmpArgsFile *os.File
stdErrOutputFmt string
}{
{
name: "succeed removing temp files",
tmpCoverageFiles: func() []*os.File {
return []*os.File{tempFile(t)}
}(),
tmpArgsFile: func() *os.File {
return tempFile(t)
}(),
stdErrOutputFmt: "",
},
{
name: "fail silently removing nonexistent temp files",
tmpCoverageFiles: func() []*os.File {
f := removedTempFile(t)
return []*os.File{f}
}(),
tmpArgsFile: func() *os.File {
return removedTempFile(t)
}(),
stdErrOutputFmt: ".*error removing.*\n.*error removing temp arg file.*",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CoverageCollector{}
c.tmpCoverageFiles = tt.tmpCoverageFiles
c.tmpArgsFile = tt.tmpArgsFile
var buf bytes.Buffer
log.SetOutput(&buf)
c.removeTempFiles()
log.SetOutput(os.Stderr)
require.Regexp(t, tt.stdErrOutputFmt, buf.String())
for _, file := range c.tmpCoverageFiles {
_, err := os.Stat(file.Name())
require.True(t, os.IsNotExist(err))
}
_, err := os.Stat(c.tmpArgsFile.Name())
require.True(t, os.IsNotExist(err))
})
}
}
func TestNewCoverageCollector(t *testing.T) {
type args struct {
mergedCoverageFilename string
collectCoverage bool
}
tests := []struct {
name string
args args
want *CoverageCollector
}{
{
name: "succeed creating CoverageCollector instance",
args: args{
mergedCoverageFilename: "fake.file",
collectCoverage: false,
},
want: &CoverageCollector{
MergedCoverageFilename: "fake.file",
CollectCoverage: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewCoverageCollector(tt.args.mergedCoverageFilename, tt.args.collectCoverage); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewCoverageCollector() = %v, want %v", got, tt.want)
}
})
}
}
func Test_parseCommandOutput(t *testing.T) {
type args struct {
output string
}
tests := []struct {
name string
args args
wantCmdOutput string
wantCoverMode string
wantExitCode int
wantPanic bool
panicMessage string
}{
{
name: "panic if metadata start marker is missing",
args: args{output: ""},
wantPanic: true,
panicMessage: "metadata start marker is unexpectedly missing",
},
{
name: "panic if metadata end marker is missing",
args: args{output: startOfMetadataMarker},
wantPanic: true,
panicMessage: "metadata end marker is unexpectedly missing",
},
{
name: "panic if error occurs while unmarshalling testMetadata",
args: args{output: startOfMetadataMarker + "invalid" + endOfMetadataMarker},
wantPanic: true,
panicMessage: "error unmarshalling testMetadata struct from RunTest",
},
{
name: "succeed parsing command output",
args: args{
output: "test output" + startOfMetadataMarker + "\n{\"cover_mode\":\"set\",\"exit_code\":1}\n" + endOfMetadataMarker + "\n",
},
wantCmdOutput: "test output",
wantCoverMode: "set",
wantExitCode: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.wantPanic {
require.PanicsWithValue(t, tt.panicMessage, func() { parseCommandOutput(tt.args.output) })
return
}
gotCmdOutput, gotCoverMode, gotExitCode := parseCommandOutput(tt.args.output)
if gotCmdOutput != tt.wantCmdOutput {
t.Errorf("parseCommandOutput() gotCmdOutput = %v, want %v", gotCmdOutput, tt.wantCmdOutput)
}
if gotCoverMode != tt.wantCoverMode {
t.Errorf("parseCommandOutput() gotCoverMode = %v, want %v", gotCoverMode, tt.wantCoverMode)
}
if gotExitCode != tt.wantExitCode {
t.Errorf("parseCommandOutput() gotExitCode = %v, want %v", gotExitCode, tt.wantExitCode)
}
})
}
}
func TestCoverageCollector_writeArgs(t *testing.T) {
type fields struct {
MergedCoverageFilename string
CollectCoverage bool
tmpArgsFile *os.File
coverMode string
tmpCoverageFiles []*os.File
setupFinished bool
}
type args struct {
args []string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
wantArgFileContent string
}{
{
name: "fail when writing args to closed file",
fields: fields{
tmpArgsFile: func() *os.File {
f := tempFile(t)
require.NoError(t, f.Close())
return f
}(),
},
wantErr: true,
},
{
name: "succeed writing args",
fields: fields{
tmpArgsFile: func() *os.File {
return tempFile(t)
}(),
},
args: args{args: []string{"first", "second", "third"}},
wantArgFileContent: "first\nsecond\nthird",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer os.Remove(tt.fields.tmpArgsFile.Name())
c := &CoverageCollector{
MergedCoverageFilename: tt.fields.MergedCoverageFilename,
CollectCoverage: tt.fields.CollectCoverage,
tmpArgsFile: tt.fields.tmpArgsFile,
coverMode: tt.fields.coverMode,
tmpCoverageFiles: tt.fields.tmpCoverageFiles,
setupFinished: tt.fields.setupFinished,
}
if err := c.writeArgs(tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("writeArgs() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr {
buf, err := ioutil.ReadAll(c.tmpArgsFile)
require.NoError(t, err)
require.Equal(t, tt.wantArgFileContent, string(buf))
}
})
}
}
func TestCoverageCollector_RunBinary(t *testing.T) {
type fields struct {
MergedCoverageFilename string
CollectCoverage bool
coverMode string
}
type args struct {
binPath string
mainTestName string
env []string
args []string
}
tests := []struct {
name string
fields fields
args args
wantOutput string
wantExitCode int
wantErr bool
errMessage string
wantPanic bool
panicMessage string
skipSetup bool
}{
{
name: "panic if Setup not called",
wantPanic: true,
panicMessage: "RunBinary called before Setup",
skipSetup: true,
},
{
name: "fail if error exists and is not an ExitError when executing command at 'binPath'",
wantErr: true,
errMessage: "unexpected error running command \"invalid.exec\": exec: \"invalid.exec\": executable file not found in $PATH",
args: args{
binPath: "invalid.exec",
mainTestName: "",
env: nil,
args: nil,
},
wantExitCode: -1,
},
{
name: "fail if error exists and is an ExitError when executing command at 'binPath'",
wantErr: true,
errMessage: "unsuccessful exit by command \"./test_bins/exit_1.sh\"\nExit code: 1\nOutput:\nHello world\n: exit status 1",
args: args{
binPath: "./test_bins/exit_1.sh",
mainTestName: "",
env: nil,
args: nil,
},
wantExitCode: 1,
},
{
name: "succeed running binary when coverage is disabled",
args: args{
binPath: "./set_covermode",
mainTestName: "TestRunMain",
env: nil,
args: nil,
},
fields: fields{
MergedCoverageFilename: "",
CollectCoverage: false,
},
wantOutput: "Hello world\n",
wantExitCode: 1,
},
{
name: "succeed running binary when coverage is enabled",
args: args{
binPath: "./set_covermode",
mainTestName: "TestRunMain",
env: nil,
args: nil,
},
fields: fields{
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: true,
},
wantOutput: "Hello world\n",
wantExitCode: 1,
},
{
name: "panic running binary which outputs empty coverage mode",
args: args{
binPath: "./test_bins/empty_covermode.sh",
mainTestName: "",
env: nil,
args: nil,
},
fields: fields{
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: true,
},
wantPanic: true,
panicMessage: "coverage mode cannot be empty. test coverage must be enabled when CollectCoverage is set to true",
},
{
name: "panic running binary which outputs different coverage mode",
args: args{
binPath: "./set_covermode",
mainTestName: "TestRunMain",
env: nil,
args: nil,
},
fields: fields{
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: true,
coverMode: "atomic",
},
wantPanic: true,
panicMessage: "cannot merge profiles with different coverage modes",
},
{
name: "panic running binary which outputs unexpected coverage mode",
args: args{
binPath: "./test_bins/unexpected_covermode.sh",
mainTestName: "",
env: nil,
args: nil,
},
fields: fields{
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: true,
},
wantPanic: true,
panicMessage: "unexpected coverage mode \"evil\" encountered. Coverage mode must be set, count, or atomic",
},
{
name: "fail running binary if there are no tests to run",
args: args{
binPath: "./test_bins/no_tests.sh",
mainTestName: "",
env: nil,
args: nil,
},
wantErr: true,
errMessage: "testing: warning: no tests to run\n",
wantExitCode: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewCoverageCollector(tt.fields.MergedCoverageFilename, tt.fields.CollectCoverage)
c.coverMode = tt.fields.coverMode
if !tt.skipSetup {
require.NoError(t, c.Setup())
}
if tt.wantPanic {
require.PanicsWithValue(t,
tt.panicMessage,
func() { _, _, _ = c.RunBinary(tt.args.binPath, tt.args.mainTestName, tt.args.env, tt.args.args) },
)
return
}
gotOutput, gotExitCode, err := c.RunBinary(tt.args.binPath, tt.args.mainTestName, tt.args.env, tt.args.args)
if (err != nil) != tt.wantErr {
t.Errorf("RunBinary() error = %v, wantErr %v", err, tt.wantErr)
return
} else if err != nil && tt.errMessage != err.Error() {
t.Errorf("RunBinary() error =\n%v\nerrMessage =\n%v", err, tt.errMessage)
}
if tt.fields.CollectCoverage {
require.Equal(t, 1, len(c.tmpCoverageFiles))
buf, err := ioutil.ReadAll(c.tmpCoverageFiles[0])
require.NoError(t, err)
require.NotZero(t, len(buf))
} else {
require.Zero(t, len(c.tmpCoverageFiles))
}
if gotOutput != tt.wantOutput {
t.Errorf("RunBinary() gotOutput = %v, want %v", gotOutput, tt.wantOutput)
}
if gotExitCode != tt.wantExitCode {
t.Errorf("RunBinary() gotExitCode = %v, want %v", gotExitCode, tt.wantExitCode)
}
})
}
}
func tempFile(t *testing.T) *os.File {
f, err := ioutil.TempFile("", "")
require.NoError(t, err)
return f
}
func removedTempFile(t *testing.T) *os.File {
f := tempFile(t)
require.NoError(t, os.Remove(f.Name()))
return f
}
func tempFileWithContent(t *testing.T, content string) *os.File {
f := tempFile(t)
_, err := f.WriteString(content)
require.NoError(t, err)
_, err = f.Seek(0, 0)
require.NoError(t, err)
return f
}