Skip to content

Commit bf3547f

Browse files
committed
tests: add more tests
1 parent 28a30a9 commit bf3547f

File tree

2 files changed

+125
-18
lines changed

2 files changed

+125
-18
lines changed

pkg/printers/tab_test.go

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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

1617
func TestTab_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,16 +52,49 @@ func TestTab_Print(t *testing.T) {
4452
},
4553
}
4654

47-
buf := new(bytes.Buffer)
55+
testCases := []struct {
56+
desc string
57+
printLinterName bool
58+
useColors bool
59+
expected string
60+
}{
61+
{
62+
desc: "with linter name",
63+
printLinterName: true,
64+
useColors: false,
65+
expected: `path/to/filea.go:10:4 linter-a some issue
66+
path/to/fileb.go:300:9 linter-b another issue
67+
`,
68+
},
69+
{
70+
desc: "disable all options",
71+
printLinterName: false,
72+
useColors: false,
73+
expected: `path/to/filea.go:10:4 some issue
74+
path/to/fileb.go:300:9 another issue
75+
`,
76+
},
77+
{
78+
desc: "enable all options",
79+
printLinterName: true,
80+
useColors: true,
81+
expected: "\x1b[1mpath/to/filea.go:10\x1b[0m:4 linter-a \x1b[31msome issue\x1b[0m\n\x1b[1mpath/to/fileb.go:300\x1b[0m:9 linter-b \x1b[31manother issue\x1b[0m\n",
82+
},
83+
}
84+
85+
for _, test := range testCases {
86+
test := test
87+
t.Run(test.desc, func(t *testing.T) {
88+
t.Parallel()
4889

49-
printer := NewTab(true, false, logutils.NewStderrLog(logutils.DebugKeyEmpty), buf)
90+
buf := new(bytes.Buffer)
5091

51-
err := printer.Print(context.Background(), issues)
52-
require.NoError(t, err)
92+
printer := NewTab(test.printLinterName, test.useColors, logutils.NewStderrLog(logutils.DebugKeyEmpty), buf)
5393

54-
expected := `path/to/filea.go:10:4 linter-a some issue
55-
path/to/fileb.go:300:9 linter-b another issue
56-
`
94+
err := printer.Print(context.Background(), issues)
95+
require.NoError(t, err)
5796

58-
assert.Equal(t, expected, buf.String())
97+
assert.Equal(t, test.expected, buf.String())
98+
})
99+
}
59100
}

pkg/printers/text_test.go

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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

1617
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+
1725
issues := []result.Issue{
1826
{
1927
FromLinter: "linter-a",
@@ -44,19 +52,77 @@ 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)
5580
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
5690
func 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+
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)\nfunc foo() {\n\tfmt.Println(\"bar\")\n}\n",
101+
},
102+
{
103+
desc: "disable all options",
104+
printIssuedLine: false,
105+
printLinterName: false,
106+
useColors: false,
107+
expected: `path/to/filea.go:10:4: some issue
108+
path/to/fileb.go:300:9: another issue
109+
`,
110+
},
111+
}
112+
113+
for _, test := range testCases {
114+
test := test
115+
t.Run(test.desc, func(t *testing.T) {
116+
t.Parallel()
117+
118+
buf := new(bytes.Buffer)
60119

61-
assert.Equal(t, expected, buf.String())
120+
printer := NewText(test.printIssuedLine, test.useColors, test.printLinterName, logutils.NewStderrLog(logutils.DebugKeyEmpty), buf)
121+
122+
err := printer.Print(context.Background(), issues)
123+
require.NoError(t, err)
124+
125+
assert.Equal(t, test.expected, buf.String())
126+
})
127+
}
62128
}

0 commit comments

Comments
 (0)