@@ -5,104 +5,77 @@ import (
55 "testing"
66)
77
8- func testRun (opt options ) (string , error ) {
8+ func testRun (opt options ) (string , exitCode ) {
99 var output bytes.Buffer
10- err := run (& output , opt )
11- if err != nil {
12- return "" , err
13- }
14- return output .String (), nil
10+ exitCode := run (& output , opt )
11+ return output .String (), exitCode
1512}
1613
17- func TestPass (t * testing.T ) {
18- opt := options {
19- directory : "../../test/data/pass" ,
20- format : "" ,
21- }
14+ func assertCode (t * testing.T , opt options , want exitCode ) {
15+ _ , got := testRun (opt )
2216
23- got , err := testRun (opt )
24- if err != nil {
25- t .Error (err )
17+ if got != want {
18+ t .Errorf ("Input: %v Want: %d Got: %d" , opt , want , got )
2619 }
20+ }
2721
28- want := `Everything ok ;)
29- `
30- if got != want {
31- t .Errorf ("Input: %v Want: '%s' Got: '%s'" , opt , want , got )
22+ func assert (t * testing.T , opt options , wantCode exitCode , want string ) {
23+ got , gotCode := testRun (opt )
24+
25+ if gotCode != wantCode || got != want {
26+ t .Errorf ("Input: %v Want: %d '%s' Got: %d '%s'" , opt , wantCode , want , gotCode , got )
3227 }
3328}
3429
30+ func TestPass (t * testing.T ) {
31+ assertCode (t , options {
32+ directory : "../../test/data/pass" ,
33+ format : "" ,
34+ }, successCode )
35+ }
36+
3537func TestNoOwners (t * testing.T ) {
36- opt := options {
38+ assert ( t , options {
3739 directory : "../../test/data/no_owners" ,
3840 format : "" ,
39- }
40-
41- got , err := testRun (opt )
42- if err != nil {
43- t .Error (err )
44- }
45-
46- want := `1 ::Error:: No owners specified [NoOwner]
47- `
48- if got != want {
49- t .Errorf ("Input: %v Want: '%s' Got: '%s'" , opt , want , got )
50- }
41+ }, errorCode , `1 ::Error:: No owners specified [NoOwner]
42+ ` )
5143}
5244
5345func TestCustomFormat (t * testing.T ) {
54- opt := options {
46+ assert ( t , options {
5547 directory : "../../test/data/noowners" ,
5648 format : "test" ,
57- }
58-
59- got , err := testRun (opt )
60- if err != nil {
61- t .Error (err )
62- }
63-
64- want := `test`
65- if got != want {
66- t .Errorf ("Input: %v Want: '%s' Got: '%s'" , opt , want , got )
67- }
49+ }, errorCode , `test
50+ ` )
6851}
6952
70- func TestInvalidFormat (t * testing.T ) {
71- opt := options {
53+ func TestInvalidFormatParse (t * testing.T ) {
54+ assertCode ( t , options {
7255 directory : "../../test/data/noowners" ,
73- format : " {{template \" one\" }} " ,
74- }
75-
76- _ , err := testRun (opt )
77- if err == nil {
78- t .Errorf ("Should have errored" )
79- }
56+ format : " {{template \" one " ,
57+ }, unexpectedErrorCode )
58+ }
8059
81- opt = options {
60+ func TestInvalidFormatExec (t * testing.T ) {
61+ assertCode (t , options {
8262 directory : "../../test/data/noowners" ,
83- format : " {{ . " ,
84- }
85-
86- _ , err = testRun (opt )
87- if err == nil {
88- t .Errorf ("Should have errored" )
89- }
63+ format : " {{template \" one\" }} " ,
64+ }, unexpectedErrorCode )
9065}
9166
9267func TestInvalidDirectory (t * testing.T ) {
93- opt := options {
68+ assert ( t , options {
9469 directory : "'" ,
9570 format : "" ,
96- }
97-
98- got , err := testRun (opt )
99- if err != nil {
100- t .Error (err )
101- }
71+ }, errorCode , `0 ::Error:: No CODEOWNERS file found [NoCodeowners]
72+ ` )
73+ }
10274
103- want := `0 ::Error:: No CODEOWNERS file found [NoCodeowners]
104- `
105- if got != want {
106- t .Errorf ("Input: %v Want: '%s' Got: '%s'" , opt , want , got )
107- }
75+ func TestMultipleCodeOwners (t * testing.T ) {
76+ assert (t , options {
77+ directory : "../../test/data/multiple_codeowners" ,
78+ format : "" ,
79+ }, warningCode , `0 ::Warning:: Multiple CODEOWNERS files found (CODEOWNERS, docs/CODEOWNERS) [MultipleCodeowners]
80+ ` )
10881}
0 commit comments