66 "go/token"
77 "testing"
88
9+ "github.com/fatih/color"
910 "github.com/stretchr/testify/assert"
1011 "github.com/stretchr/testify/require"
1112
@@ -14,6 +15,13 @@ import (
1415)
1516
1617func 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+
1725 issues := []result.Issue {
1826 {
1927 FromLinter : "linter-a" ,
@@ -44,19 +52,78 @@ func TestText_Print(t *testing.T) {
4452 },
4553 }
4654
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)
5580path/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
5690func foo() {
5791 fmt.Println("bar")
5892}
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 )
60120
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+ }
62129}
0 commit comments