6
6
"go/token"
7
7
"testing"
8
8
9
+ "github.com/fatih/color"
9
10
"github.com/stretchr/testify/assert"
10
11
"github.com/stretchr/testify/require"
11
12
@@ -14,6 +15,13 @@ import (
14
15
)
15
16
16
17
func TestText_Print (t * testing.T ) {
18
+ // force color globally
19
+ backup := color .NoColor
20
+ t .Cleanup (func () {
21
+ color .NoColor = backup
22
+ })
23
+ color .NoColor = false
24
+
17
25
issues := []result.Issue {
18
26
{
19
27
FromLinter : "linter-a" ,
@@ -44,19 +52,78 @@ func TestText_Print(t *testing.T) {
44
52
},
45
53
}
46
54
47
- buf := new (bytes.Buffer )
48
-
49
- printer := NewText (true , false , true , logutils .NewStderrLog (logutils .DebugKeyEmpty ), buf )
50
-
51
- err := printer .Print (context .Background (), issues )
52
- require .NoError (t , err )
53
-
54
- expected := `path/to/filea.go:10:4: some issue (linter-a)
55
+ testCases := []struct {
56
+ desc string
57
+ printIssuedLine bool
58
+ printLinterName bool
59
+ useColors bool
60
+ expected string
61
+ }{
62
+ {
63
+ desc : "printIssuedLine and printLinterName" ,
64
+ printIssuedLine : true ,
65
+ printLinterName : true ,
66
+ useColors : false ,
67
+ expected : `path/to/filea.go:10:4: some issue (linter-a)
68
+ path/to/fileb.go:300:9: another issue (linter-b)
69
+ func foo() {
70
+ fmt.Println("bar")
71
+ }
72
+ ` ,
73
+ },
74
+ {
75
+ desc : "printLinterName only" ,
76
+ printIssuedLine : false ,
77
+ printLinterName : true ,
78
+ useColors : false ,
79
+ expected : `path/to/filea.go:10:4: some issue (linter-a)
55
80
path/to/fileb.go:300:9: another issue (linter-b)
81
+ ` ,
82
+ },
83
+ {
84
+ desc : "printIssuedLine only" ,
85
+ printIssuedLine : true ,
86
+ printLinterName : false ,
87
+ useColors : false ,
88
+ expected : `path/to/filea.go:10:4: some issue
89
+ path/to/fileb.go:300:9: another issue
56
90
func foo() {
57
91
fmt.Println("bar")
58
92
}
59
- `
93
+ ` ,
94
+ },
95
+ {
96
+ desc : "enable all options" ,
97
+ printIssuedLine : true ,
98
+ printLinterName : true ,
99
+ useColors : true ,
100
+ //nolint:lll // color characters must be in a simple string.
101
+ expected : "\x1b [1mpath/to/filea.go:10\x1b [0m:4: \x1b [31msome issue\x1b [0m (linter-a)\n \x1b [1mpath/to/fileb.go:300\x1b [0m:9: \x1b [31manother issue\x1b [0m (linter-b)\n func foo() {\n \t fmt.Println(\" bar\" )\n }\n " ,
102
+ },
103
+ {
104
+ desc : "disable all options" ,
105
+ printIssuedLine : false ,
106
+ printLinterName : false ,
107
+ useColors : false ,
108
+ expected : `path/to/filea.go:10:4: some issue
109
+ path/to/fileb.go:300:9: another issue
110
+ ` ,
111
+ },
112
+ }
113
+
114
+ for _ , test := range testCases {
115
+ test := test
116
+ t .Run (test .desc , func (t * testing.T ) {
117
+ t .Parallel ()
118
+
119
+ buf := new (bytes.Buffer )
60
120
61
- assert .Equal (t , expected , buf .String ())
121
+ printer := NewText (test .printIssuedLine , test .useColors , test .printLinterName , logutils .NewStderrLog (logutils .DebugKeyEmpty ), buf )
122
+
123
+ err := printer .Print (context .Background (), issues )
124
+ require .NoError (t , err )
125
+
126
+ assert .Equal (t , test .expected , buf .String ())
127
+ })
128
+ }
62
129
}
0 commit comments