Skip to content

Commit 54127fd

Browse files
committed
dev: add printers unit tests.
1 parent eaed228 commit 54127fd

11 files changed

+589
-3
lines changed

go.sum

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/printers/checkstyle.go

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/xml"
66
"fmt"
77
"io"
8+
"sort"
89

910
"github.com/go-xmlfmt/xmlfmt"
1011

@@ -79,6 +80,10 @@ func (p Checkstyle) Print(ctx context.Context, issues []result.Issue) error {
7980
out.Files = append(out.Files, file)
8081
}
8182

83+
sort.Slice(out.Files, func(i, j int) bool {
84+
return out.Files[i].Name < out.Files[j].Name
85+
})
86+
8287
data, err := xml.Marshal(&out)
8388
if err != nil {
8489
return err

pkg/printers/checkstyle_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//nolint:dupl
2+
package printers
3+
4+
import (
5+
"bytes"
6+
"context"
7+
"go/token"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/golangci/golangci-lint/pkg/result"
14+
)
15+
16+
func TestCheckstyle_Print(t *testing.T) {
17+
issues := []result.Issue{
18+
{
19+
FromLinter: "linter-a",
20+
Severity: "warning",
21+
Text: "some issue",
22+
Pos: token.Position{
23+
Filename: "path/to/filea.go",
24+
Offset: 2,
25+
Line: 10,
26+
Column: 4,
27+
},
28+
},
29+
{
30+
FromLinter: "linter-b",
31+
Severity: "error",
32+
Text: "another issue",
33+
SourceLines: []string{
34+
"func foo() {",
35+
"\tfmt.Println(\"bar\")",
36+
"}",
37+
},
38+
Pos: token.Position{
39+
Filename: "path/to/fileb.go",
40+
Offset: 5,
41+
Line: 300,
42+
Column: 9,
43+
},
44+
},
45+
}
46+
47+
buf := new(bytes.Buffer)
48+
printer := NewCheckstyle(buf)
49+
50+
err := printer.Print(context.Background(), issues)
51+
require.NoError(t, err)
52+
53+
//nolint:lll
54+
expected := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\r\n<checkstyle version=\"5.0\">\r\n <file name=\"path/to/filea.go\">\r\n <error column=\"4\" line=\"10\" message=\"some issue\" severity=\"warning\" source=\"linter-a\">\r\n </error>\r\n </file>\r\n <file name=\"path/to/fileb.go\">\r\n <error column=\"9\" line=\"300\" message=\"another issue\" severity=\"error\" source=\"linter-b\">\r\n </error>\r\n </file>\r\n</checkstyle>\n"
55+
56+
assert.Equal(t, expected, buf.String())
57+
}

pkg/printers/codeclimate_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//nolint:dupl
2+
package printers
3+
4+
import (
5+
"bytes"
6+
"context"
7+
"go/token"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/golangci/golangci-lint/pkg/result"
14+
)
15+
16+
func TestCodeClimate_Print(t *testing.T) {
17+
issues := []result.Issue{
18+
{
19+
FromLinter: "linter-a",
20+
Severity: "warning",
21+
Text: "some issue",
22+
Pos: token.Position{
23+
Filename: "path/to/filea.go",
24+
Offset: 2,
25+
Line: 10,
26+
Column: 4,
27+
},
28+
},
29+
{
30+
FromLinter: "linter-b",
31+
Severity: "error",
32+
Text: "another issue",
33+
SourceLines: []string{
34+
"func foo() {",
35+
"\tfmt.Println(\"bar\")",
36+
"}",
37+
},
38+
Pos: token.Position{
39+
Filename: "path/to/fileb.go",
40+
Offset: 5,
41+
Line: 300,
42+
Column: 9,
43+
},
44+
},
45+
}
46+
47+
buf := new(bytes.Buffer)
48+
printer := NewCodeClimate(buf)
49+
50+
err := printer.Print(context.Background(), issues)
51+
require.NoError(t, err)
52+
53+
//nolint:lll
54+
expected := `[{"description":"linter-a: some issue","severity":"warning","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","severity":"error","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}}]`
55+
56+
assert.Equal(t, expected, buf.String())
57+
}

pkg/printers/github_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,61 @@
11
package printers
22

33
import (
4+
"bytes"
5+
"context"
46
"go/token"
57
"testing"
68

9+
"github.com/stretchr/testify/assert"
710
"github.com/stretchr/testify/require"
811

912
"github.com/golangci/golangci-lint/pkg/result"
1013
)
1114

15+
func TestGithub_Print(t *testing.T) {
16+
issues := []result.Issue{
17+
{
18+
FromLinter: "linter-a",
19+
Severity: "warning",
20+
Text: "some issue",
21+
Pos: token.Position{
22+
Filename: "path/to/filea.go",
23+
Offset: 2,
24+
Line: 10,
25+
Column: 4,
26+
},
27+
},
28+
{
29+
FromLinter: "linter-b",
30+
Severity: "error",
31+
Text: "another issue",
32+
SourceLines: []string{
33+
"func foo() {",
34+
"\tfmt.Println(\"bar\")",
35+
"}",
36+
},
37+
Pos: token.Position{
38+
Filename: "path/to/fileb.go",
39+
Offset: 5,
40+
Line: 300,
41+
Column: 9,
42+
},
43+
},
44+
}
45+
46+
buf := new(bytes.Buffer)
47+
printer := NewGithub(buf)
48+
49+
err := printer.Print(context.Background(), issues)
50+
require.NoError(t, err)
51+
52+
expected := `::warning file=path/to/filea.go,line=10,col=4::some issue (linter-a)
53+
::error file=path/to/fileb.go,line=300,col=9::another issue (linter-b)
54+
`
55+
56+
assert.Equal(t, expected, buf.String())
57+
}
58+
1259
func TestFormatGithubIssue(t *testing.T) {
1360
sampleIssue := result.Issue{
1461
FromLinter: "sample-linter",

pkg/printers/html_test.go

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package printers
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"go/token"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/golangci/golangci-lint/pkg/result"
13+
)
14+
15+
//nolint:lll
16+
const expectedHTML = `<!doctype html>
17+
<html lang="en">
18+
<head>
19+
<meta charset="utf-8">
20+
<title>golangci-lint</title>
21+
<link rel="shortcut icon" type="image/png" href="https://golangci-lint.run/favicon-32x32.png">
22+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.2/css/bulma.min.css"
23+
integrity="sha512-byErQdWdTqREz6DLAA9pCnLbdoGGhXfU6gm1c8bkf7F51JVmUBlayGe2A31VpXWQP+eiJ3ilTAZHCR3vmMyybA=="
24+
crossorigin="anonymous" referrerpolicy="no-referrer"/>
25+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/styles/default.min.css"
26+
integrity="sha512-kZqGbhf9JTB4bVJ0G8HCkqmaPcRgo88F0dneK30yku5Y/dep7CZfCnNml2Je/sY4lBoqoksXz4PtVXS4GHSUzQ=="
27+
crossorigin="anonymous" referrerpolicy="no-referrer"/>
28+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/highlight.min.js"
29+
integrity="sha512-s+tOYYcC3Jybgr9mVsdAxsRYlGNq4mlAurOrfNuGMQ/SCofNPu92tjE7YRZCsdEtWL1yGkqk15fU/ark206YTg=="
30+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
31+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/languages/go.min.js"
32+
integrity="sha512-+UYV2NyyynWEQcZ4sMTKmeppyV331gqvMOGZ61/dqc89Tn1H40lF05ACd03RSD9EWwGutNwKj256mIR8waEJBQ=="
33+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
34+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"
35+
integrity="sha512-qlzIeUtTg7eBpmEaS12NZgxz52YYZVF5myj89mjJEesBd/oE9UPsYOX2QAXzvOAZYEvQohKdcY8zKE02ifXDmA=="
36+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
37+
<script type="text/javascript"
38+
src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"
39+
integrity="sha512-9jGNr5Piwe8nzLLYTk8QrEMPfjGU0px80GYzKZUxi7lmCfrBjtyCc1V5kkS5vxVwwIB7Qpzc7UxLiQxfAN30dw=="
40+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
41+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"
42+
integrity="sha512-kp7YHLxuJDJcOzStgd6vtpxr4ZU9kjn77e6dBsivSz+pUuAuMlE2UTdKB7jjsWT84qbS8kdCWHPETnP/ctrFsA=="
43+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
44+
</head>
45+
<body>
46+
<section class="section">
47+
<div class="container">
48+
<div id="content"></div>
49+
</div>
50+
</section>
51+
<script>
52+
const data = {"Issues":[{"Title":"some issue","Pos":"path/to/filea.go:10:4","Linter":"linter-a","Code":""},{"Title":"another issue","Pos":"path/to/fileb.go:300:9","Linter":"linter-b","Code":"func foo() {\n\tfmt.Println(\"bar\")\n}"}]};
53+
</script>
54+
<script type="text/babel">
55+
class Highlight extends React.Component {
56+
componentDidMount() {
57+
hljs.highlightElement(ReactDOM.findDOMNode(this));
58+
}
59+
60+
render() {
61+
return <pre className="go"><code>{this.props.code}</code></pre>;
62+
}
63+
}
64+
65+
class Issue extends React.Component {
66+
render() {
67+
return (
68+
<div className="issue box">
69+
<div>
70+
<div className="columns">
71+
<div className="column is-four-fifths">
72+
<h5 className="title is-5 has-text-danger-dark">{this.props.data.Title}</h5>
73+
</div>
74+
<div className="column is-one-fifth">
75+
<h6 className="title is-6">{this.props.data.Linter}</h6>
76+
</div>
77+
</div>
78+
<strong>{this.props.data.Pos}</strong>
79+
</div>
80+
<div className="highlight">
81+
<Highlight code={this.props.data.Code}/>
82+
</div>
83+
</div>
84+
);
85+
}
86+
}
87+
88+
class Issues extends React.Component {
89+
render() {
90+
if (!this.props.data.Issues || this.props.data.Issues.length === 0) {
91+
return (
92+
<div>
93+
<div className="notification">
94+
No issues found!
95+
</div>
96+
</div>
97+
);
98+
}
99+
100+
return (
101+
<div className="issues">
102+
{this.props.data.Issues.map(issue => (<Issue data={issue}/>))}
103+
</div>
104+
);
105+
}
106+
}
107+
108+
ReactDOM.render(
109+
<div className="content">
110+
<div className="columns is-centered">
111+
<div className="column is-three-quarters">
112+
<Issues data={data}/>
113+
</div>
114+
</div>
115+
</div>,
116+
document.getElementById("content")
117+
);
118+
</script>
119+
</body>
120+
</html>`
121+
122+
func TestHTML_Print(t *testing.T) {
123+
issues := []result.Issue{
124+
{
125+
FromLinter: "linter-a",
126+
Severity: "warning",
127+
Text: "some issue",
128+
Pos: token.Position{
129+
Filename: "path/to/filea.go",
130+
Offset: 2,
131+
Line: 10,
132+
Column: 4,
133+
},
134+
},
135+
{
136+
FromLinter: "linter-b",
137+
Severity: "error",
138+
Text: "another issue",
139+
SourceLines: []string{
140+
"func foo() {",
141+
"\tfmt.Println(\"bar\")",
142+
"}",
143+
},
144+
Pos: token.Position{
145+
Filename: "path/to/fileb.go",
146+
Offset: 5,
147+
Line: 300,
148+
Column: 9,
149+
},
150+
},
151+
}
152+
153+
buf := new(bytes.Buffer)
154+
printer := NewHTML(buf)
155+
156+
err := printer.Print(context.Background(), issues)
157+
require.NoError(t, err)
158+
159+
assert.Equal(t, expectedHTML, buf.String())
160+
}

0 commit comments

Comments
 (0)