dev: add printers unit tests. (#2461)
This commit is contained in:
parent
138699d60f
commit
e443887a4f
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/go-xmlfmt/xmlfmt"
|
"github.com/go-xmlfmt/xmlfmt"
|
||||||
|
|
||||||
@ -79,6 +80,10 @@ func (p Checkstyle) Print(ctx context.Context, issues []result.Issue) error {
|
|||||||
out.Files = append(out.Files, file)
|
out.Files = append(out.Files, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Slice(out.Files, func(i, j int) bool {
|
||||||
|
return out.Files[i].Name < out.Files[j].Name
|
||||||
|
})
|
||||||
|
|
||||||
data, err := xml.Marshal(&out)
|
data, err := xml.Marshal(&out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
57
pkg/printers/checkstyle_test.go
Normal file
57
pkg/printers/checkstyle_test.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
//nolint:dupl
|
||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheckstyle_Print(t *testing.T) {
|
||||||
|
issues := []result.Issue{
|
||||||
|
{
|
||||||
|
FromLinter: "linter-a",
|
||||||
|
Severity: "warning",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/filea.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FromLinter: "linter-b",
|
||||||
|
Severity: "error",
|
||||||
|
Text: "another issue",
|
||||||
|
SourceLines: []string{
|
||||||
|
"func foo() {",
|
||||||
|
"\tfmt.Println(\"bar\")",
|
||||||
|
"}",
|
||||||
|
},
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/fileb.go",
|
||||||
|
Offset: 5,
|
||||||
|
Line: 300,
|
||||||
|
Column: 9,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
printer := NewCheckstyle(buf)
|
||||||
|
|
||||||
|
err := printer.Print(context.Background(), issues)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
//nolint:lll
|
||||||
|
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"
|
||||||
|
|
||||||
|
assert.Equal(t, expected, buf.String())
|
||||||
|
}
|
57
pkg/printers/codeclimate_test.go
Normal file
57
pkg/printers/codeclimate_test.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
//nolint:dupl
|
||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCodeClimate_Print(t *testing.T) {
|
||||||
|
issues := []result.Issue{
|
||||||
|
{
|
||||||
|
FromLinter: "linter-a",
|
||||||
|
Severity: "warning",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/filea.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FromLinter: "linter-b",
|
||||||
|
Severity: "error",
|
||||||
|
Text: "another issue",
|
||||||
|
SourceLines: []string{
|
||||||
|
"func foo() {",
|
||||||
|
"\tfmt.Println(\"bar\")",
|
||||||
|
"}",
|
||||||
|
},
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/fileb.go",
|
||||||
|
Offset: 5,
|
||||||
|
Line: 300,
|
||||||
|
Column: 9,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
printer := NewCodeClimate(buf)
|
||||||
|
|
||||||
|
err := printer.Print(context.Background(), issues)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
//nolint:lll
|
||||||
|
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}}}]`
|
||||||
|
|
||||||
|
assert.Equal(t, expected, buf.String())
|
||||||
|
}
|
@ -1,14 +1,61 @@
|
|||||||
package printers
|
package printers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
"go/token"
|
"go/token"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestGithub_Print(t *testing.T) {
|
||||||
|
issues := []result.Issue{
|
||||||
|
{
|
||||||
|
FromLinter: "linter-a",
|
||||||
|
Severity: "warning",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/filea.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FromLinter: "linter-b",
|
||||||
|
Severity: "error",
|
||||||
|
Text: "another issue",
|
||||||
|
SourceLines: []string{
|
||||||
|
"func foo() {",
|
||||||
|
"\tfmt.Println(\"bar\")",
|
||||||
|
"}",
|
||||||
|
},
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/fileb.go",
|
||||||
|
Offset: 5,
|
||||||
|
Line: 300,
|
||||||
|
Column: 9,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
printer := NewGithub(buf)
|
||||||
|
|
||||||
|
err := printer.Print(context.Background(), issues)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
expected := `::warning file=path/to/filea.go,line=10,col=4::some issue (linter-a)
|
||||||
|
::error file=path/to/fileb.go,line=300,col=9::another issue (linter-b)
|
||||||
|
`
|
||||||
|
|
||||||
|
assert.Equal(t, expected, buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
func TestFormatGithubIssue(t *testing.T) {
|
func TestFormatGithubIssue(t *testing.T) {
|
||||||
sampleIssue := result.Issue{
|
sampleIssue := result.Issue{
|
||||||
FromLinter: "sample-linter",
|
FromLinter: "sample-linter",
|
||||||
|
160
pkg/printers/html_test.go
Normal file
160
pkg/printers/html_test.go
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
//nolint:lll
|
||||||
|
const expectedHTML = `<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>golangci-lint</title>
|
||||||
|
<link rel="shortcut icon" type="image/png" href="https://golangci-lint.run/favicon-32x32.png">
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.2/css/bulma.min.css"
|
||||||
|
integrity="sha512-byErQdWdTqREz6DLAA9pCnLbdoGGhXfU6gm1c8bkf7F51JVmUBlayGe2A31VpXWQP+eiJ3ilTAZHCR3vmMyybA=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer"/>
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/styles/default.min.css"
|
||||||
|
integrity="sha512-kZqGbhf9JTB4bVJ0G8HCkqmaPcRgo88F0dneK30yku5Y/dep7CZfCnNml2Je/sY4lBoqoksXz4PtVXS4GHSUzQ=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer"/>
|
||||||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/highlight.min.js"
|
||||||
|
integrity="sha512-s+tOYYcC3Jybgr9mVsdAxsRYlGNq4mlAurOrfNuGMQ/SCofNPu92tjE7YRZCsdEtWL1yGkqk15fU/ark206YTg=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/languages/go.min.js"
|
||||||
|
integrity="sha512-+UYV2NyyynWEQcZ4sMTKmeppyV331gqvMOGZ61/dqc89Tn1H40lF05ACd03RSD9EWwGutNwKj256mIR8waEJBQ=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"
|
||||||
|
integrity="sha512-qlzIeUtTg7eBpmEaS12NZgxz52YYZVF5myj89mjJEesBd/oE9UPsYOX2QAXzvOAZYEvQohKdcY8zKE02ifXDmA=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
|
<script type="text/javascript"
|
||||||
|
src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"
|
||||||
|
integrity="sha512-9jGNr5Piwe8nzLLYTk8QrEMPfjGU0px80GYzKZUxi7lmCfrBjtyCc1V5kkS5vxVwwIB7Qpzc7UxLiQxfAN30dw=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"
|
||||||
|
integrity="sha512-kp7YHLxuJDJcOzStgd6vtpxr4ZU9kjn77e6dBsivSz+pUuAuMlE2UTdKB7jjsWT84qbS8kdCWHPETnP/ctrFsA=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<section class="section">
|
||||||
|
<div class="container">
|
||||||
|
<div id="content"></div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<script>
|
||||||
|
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}"}]};
|
||||||
|
</script>
|
||||||
|
<script type="text/babel">
|
||||||
|
class Highlight extends React.Component {
|
||||||
|
componentDidMount() {
|
||||||
|
hljs.highlightElement(ReactDOM.findDOMNode(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return <pre className="go"><code>{this.props.code}</code></pre>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Issue extends React.Component {
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div className="issue box">
|
||||||
|
<div>
|
||||||
|
<div className="columns">
|
||||||
|
<div className="column is-four-fifths">
|
||||||
|
<h5 className="title is-5 has-text-danger-dark">{this.props.data.Title}</h5>
|
||||||
|
</div>
|
||||||
|
<div className="column is-one-fifth">
|
||||||
|
<h6 className="title is-6">{this.props.data.Linter}</h6>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<strong>{this.props.data.Pos}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="highlight">
|
||||||
|
<Highlight code={this.props.data.Code}/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Issues extends React.Component {
|
||||||
|
render() {
|
||||||
|
if (!this.props.data.Issues || this.props.data.Issues.length === 0) {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div className="notification">
|
||||||
|
No issues found!
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="issues">
|
||||||
|
{this.props.data.Issues.map(issue => (<Issue data={issue}/>))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ReactDOM.render(
|
||||||
|
<div className="content">
|
||||||
|
<div className="columns is-centered">
|
||||||
|
<div className="column is-three-quarters">
|
||||||
|
<Issues data={data}/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>,
|
||||||
|
document.getElementById("content")
|
||||||
|
);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>`
|
||||||
|
|
||||||
|
func TestHTML_Print(t *testing.T) {
|
||||||
|
issues := []result.Issue{
|
||||||
|
{
|
||||||
|
FromLinter: "linter-a",
|
||||||
|
Severity: "warning",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/filea.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FromLinter: "linter-b",
|
||||||
|
Severity: "error",
|
||||||
|
Text: "another issue",
|
||||||
|
SourceLines: []string{
|
||||||
|
"func foo() {",
|
||||||
|
"\tfmt.Println(\"bar\")",
|
||||||
|
"}",
|
||||||
|
},
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/fileb.go",
|
||||||
|
Offset: 5,
|
||||||
|
Line: 300,
|
||||||
|
Column: 9,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
printer := NewHTML(buf)
|
||||||
|
|
||||||
|
err := printer.Print(context.Background(), issues)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, expectedHTML, buf.String())
|
||||||
|
}
|
58
pkg/printers/json_test.go
Normal file
58
pkg/printers/json_test.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJSON_Print(t *testing.T) {
|
||||||
|
issues := []result.Issue{
|
||||||
|
{
|
||||||
|
FromLinter: "linter-a",
|
||||||
|
Severity: "warning",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/filea.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FromLinter: "linter-b",
|
||||||
|
Severity: "error",
|
||||||
|
Text: "another issue",
|
||||||
|
SourceLines: []string{
|
||||||
|
"func foo() {",
|
||||||
|
"\tfmt.Println(\"bar\")",
|
||||||
|
"}",
|
||||||
|
},
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/fileb.go",
|
||||||
|
Offset: 5,
|
||||||
|
Line: 300,
|
||||||
|
Column: 9,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
printer := NewJSON(nil, buf)
|
||||||
|
|
||||||
|
err := printer.Print(context.Background(), issues)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
//nolint:lll
|
||||||
|
expected := `{"Issues":[{"FromLinter":"linter-a","Text":"some issue","Severity":"warning","SourceLines":null,"Replacement":null,"Pos":{"Filename":"path/to/filea.go","Offset":2,"Line":10,"Column":4},"ExpectNoLint":false,"ExpectedNoLintLinter":""},{"FromLinter":"linter-b","Text":"another issue","Severity":"error","SourceLines":["func foo() {","\tfmt.Println(\"bar\")","}"],"Replacement":null,"Pos":{"Filename":"path/to/fileb.go","Offset":5,"Line":300,"Column":9},"ExpectNoLint":false,"ExpectedNoLintLinter":""}],"Report":null}
|
||||||
|
`
|
||||||
|
|
||||||
|
assert.Equal(t, expected, buf.String())
|
||||||
|
}
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
@ -75,6 +76,10 @@ func (p JunitXML) Print(ctx context.Context, issues []result.Issue) error {
|
|||||||
res.TestSuites = append(res.TestSuites, val)
|
res.TestSuites = append(res.TestSuites, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Slice(res.TestSuites, func(i, j int) bool {
|
||||||
|
return res.TestSuites[i].Suite < res.TestSuites[j].Suite
|
||||||
|
})
|
||||||
|
|
||||||
enc := xml.NewEncoder(p.w)
|
enc := xml.NewEncoder(p.w)
|
||||||
enc.Indent("", " ")
|
enc.Indent("", " ")
|
||||||
if err := enc.Encode(res); err != nil {
|
if err := enc.Encode(res); err != nil {
|
||||||
|
77
pkg/printers/junitxml_test.go
Normal file
77
pkg/printers/junitxml_test.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
//nolint:dupl
|
||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJunitXML_Print(t *testing.T) {
|
||||||
|
issues := []result.Issue{
|
||||||
|
{
|
||||||
|
FromLinter: "linter-a",
|
||||||
|
Severity: "warning",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/filea.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FromLinter: "linter-b",
|
||||||
|
Severity: "error",
|
||||||
|
Text: "another issue",
|
||||||
|
SourceLines: []string{
|
||||||
|
"func foo() {",
|
||||||
|
"\tfmt.Println(\"bar\")",
|
||||||
|
"}",
|
||||||
|
},
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/fileb.go",
|
||||||
|
Offset: 5,
|
||||||
|
Line: 300,
|
||||||
|
Column: 9,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
printer := NewJunitXML(buf)
|
||||||
|
|
||||||
|
err := printer.Print(context.Background(), issues)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
expected := `<testsuites>
|
||||||
|
<testsuite name="path/to/filea.go" tests="1" errors="0" failures="1">
|
||||||
|
<testcase name="linter-a" classname="path/to/filea.go:10:4">
|
||||||
|
<failure message="path/to/filea.go:10:4: some issue" type="warning"><![CDATA[warning: some issue
|
||||||
|
Category: linter-a
|
||||||
|
File: path/to/filea.go
|
||||||
|
Line: 10
|
||||||
|
Details: ]]></failure>
|
||||||
|
</testcase>
|
||||||
|
</testsuite>
|
||||||
|
<testsuite name="path/to/fileb.go" tests="1" errors="0" failures="1">
|
||||||
|
<testcase name="linter-b" classname="path/to/fileb.go:300:9">
|
||||||
|
<failure message="path/to/fileb.go:300:9: another issue" type="error"><![CDATA[error: another issue
|
||||||
|
Category: linter-b
|
||||||
|
File: path/to/fileb.go
|
||||||
|
Line: 300
|
||||||
|
Details: func foo() {
|
||||||
|
fmt.Println("bar")
|
||||||
|
}]]></failure>
|
||||||
|
</testcase>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>`
|
||||||
|
|
||||||
|
assert.Equal(t, expected, buf.String())
|
||||||
|
}
|
59
pkg/printers/tab_test.go
Normal file
59
pkg/printers/tab_test.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTab_Print(t *testing.T) {
|
||||||
|
issues := []result.Issue{
|
||||||
|
{
|
||||||
|
FromLinter: "linter-a",
|
||||||
|
Severity: "warning",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/filea.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FromLinter: "linter-b",
|
||||||
|
Severity: "error",
|
||||||
|
Text: "another issue",
|
||||||
|
SourceLines: []string{
|
||||||
|
"func foo() {",
|
||||||
|
"\tfmt.Println(\"bar\")",
|
||||||
|
"}",
|
||||||
|
},
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/fileb.go",
|
||||||
|
Offset: 5,
|
||||||
|
Line: 300,
|
||||||
|
Column: 9,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
printer := NewTab(true, logutils.NewStderrLog(""), buf)
|
||||||
|
|
||||||
|
err := printer.Print(context.Background(), issues)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
expected := `path/to/filea.go:10:4 linter-a some issue
|
||||||
|
path/to/fileb.go:300:9 linter-b another issue
|
||||||
|
`
|
||||||
|
|
||||||
|
assert.Equal(t, expected, buf.String())
|
||||||
|
}
|
62
pkg/printers/text_test.go
Normal file
62
pkg/printers/text_test.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestText_Print(t *testing.T) {
|
||||||
|
issues := []result.Issue{
|
||||||
|
{
|
||||||
|
FromLinter: "linter-a",
|
||||||
|
Severity: "warning",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/filea.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FromLinter: "linter-b",
|
||||||
|
Severity: "error",
|
||||||
|
Text: "another issue",
|
||||||
|
SourceLines: []string{
|
||||||
|
"func foo() {",
|
||||||
|
"\tfmt.Println(\"bar\")",
|
||||||
|
"}",
|
||||||
|
},
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/fileb.go",
|
||||||
|
Offset: 5,
|
||||||
|
Line: 300,
|
||||||
|
Column: 9,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
printer := NewText(true, false, true, logutils.NewStderrLog(""), buf)
|
||||||
|
|
||||||
|
err := printer.Print(context.Background(), issues)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
expected := `path/to/filea.go:10:4: some issue (linter-a)
|
||||||
|
path/to/fileb.go:300:9: another issue (linter-b)
|
||||||
|
func foo() {
|
||||||
|
fmt.Println("bar")
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
assert.Equal(t, expected, buf.String())
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user