Skip to content

Commit

Permalink
dev: cleanup printers (#5361)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Jan 31, 2025
1 parent 980a911 commit dfd79c9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
8 changes: 4 additions & 4 deletions pkg/config/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const (
OutFormatCheckstyle = "checkstyle"
OutFormatCodeClimate = "code-climate"
OutFormatHTML = "html"
OutFormatJunitXML = "junit-xml"
OutFormatJunitXMLExtended = "junit-xml-extended"
OutFormatJUnitXML = "junit-xml"
OutFormatJUnitXMLExtended = "junit-xml-extended"
OutFormatGithubActions = "github-actions" // Deprecated
OutFormatTeamCity = "teamcity"
OutFormatSarif = "sarif"
Expand All @@ -32,8 +32,8 @@ var AllOutputFormats = []string{
OutFormatCheckstyle,
OutFormatCodeClimate,
OutFormatHTML,
OutFormatJunitXML,
OutFormatJunitXMLExtended,
OutFormatJUnitXML,
OutFormatJUnitXMLExtended,
OutFormatGithubActions,
OutFormatTeamCity,
OutFormatSarif,
Expand Down
26 changes: 14 additions & 12 deletions pkg/printers/codeclimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,37 @@ func NewCodeClimate(log logutils.Log, w io.Writer) *CodeClimate {
}

func (p *CodeClimate) Print(issues []result.Issue) error {
codeClimateIssues := make([]CodeClimateIssue, 0, len(issues))
ccIssues := make([]codeClimateIssue, 0, len(issues))

for i := range issues {
issue := issues[i]

codeClimateIssue := CodeClimateIssue{}
codeClimateIssue.Description = issue.Description()
codeClimateIssue.CheckName = issue.FromLinter
codeClimateIssue.Location.Path = issue.Pos.Filename
codeClimateIssue.Location.Lines.Begin = issue.Pos.Line
codeClimateIssue.Fingerprint = issue.Fingerprint()
codeClimateIssue.Severity = p.sanitizer.Sanitize(issue.Severity)
ccIssue := codeClimateIssue{
Description: issue.Description(),
CheckName: issue.FromLinter,
Severity: p.sanitizer.Sanitize(issue.Severity),
Fingerprint: issue.Fingerprint(),
}

codeClimateIssues = append(codeClimateIssues, codeClimateIssue)
ccIssue.Location.Path = issue.Pos.Filename
ccIssue.Location.Lines.Begin = issue.Pos.Line

ccIssues = append(ccIssues, ccIssue)
}

err := p.sanitizer.Err()
if err != nil {
p.log.Infof("%v", err)
}

return json.NewEncoder(p.w).Encode(codeClimateIssues)
return json.NewEncoder(p.w).Encode(ccIssues)
}

// CodeClimateIssue is a subset of the Code Climate spec.
// codeClimateIssue is a subset of the Code Climate spec.
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
// It is just enough to support GitLab CI Code Quality.
// https://docs.gitlab.com/ee/ci/testing/code_quality.html#code-quality-report-format
type CodeClimateIssue struct {
type codeClimateIssue struct {
Description string `json:"description"`
CheckName string `json:"check_name"`
Severity string `json:"severity,omitempty"`
Expand Down
10 changes: 5 additions & 5 deletions pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

// JunitXML prints issues in the Junit XML format.
// JUnitXML prints issues in the JUnit XML format.
// There is no official specification for the JUnit XML file format,
// and various tools generate and support different flavors of this format.
// https://github.com/testmoapp/junitxml
type JunitXML struct {
type JUnitXML struct {
extended bool
w io.Writer
}

func NewJunitXML(w io.Writer, extended bool) *JunitXML {
return &JunitXML{
func NewJUnitXML(w io.Writer, extended bool) *JUnitXML {
return &JUnitXML{
extended: extended,
w: w,
}
}

func (p JunitXML) Print(issues []result.Issue) error {
func (p JUnitXML) Print(issues []result.Issue) error {
suites := make(map[string]testSuiteXML) // use a map to group by file

for ind := range issues {
Expand Down
4 changes: 2 additions & 2 deletions pkg/printers/junitxml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

func TestJunitXML_Print(t *testing.T) {
func TestJUnitXML_Print(t *testing.T) {
issues := []result.Issue{
{
FromLinter: "linter-a",
Expand Down Expand Up @@ -105,7 +105,7 @@ Details: func foo() {
t.Parallel()

buf := new(bytes.Buffer)
printer := NewJunitXML(buf, test.extended)
printer := NewJUnitXML(buf, test.extended)

err := printer.Print(issues)
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions pkg/printers/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
p = NewCodeClimate(c.log, w)
case config.OutFormatHTML:
p = NewHTML(w)
case config.OutFormatJunitXML, config.OutFormatJunitXMLExtended:
p = NewJunitXML(w, format == config.OutFormatJunitXMLExtended)
case config.OutFormatJUnitXML, config.OutFormatJUnitXMLExtended:
p = NewJUnitXML(w, format == config.OutFormatJUnitXMLExtended)
case config.OutFormatGithubActions:
p = NewGitHubAction(w)
case config.OutFormatTeamCity:
Expand Down Expand Up @@ -168,11 +168,11 @@ func (s *severitySanitizer) Err() error {
return nil
}

var foo []string
var names []string
for k := range s.unsupportedSeverities {
foo = append(foo, "'"+k+"'")
names = append(names, "'"+k+"'")
}

return fmt.Errorf("some severities (%v) are not inside supported values (%v), fallback to '%s'",
strings.Join(foo, ", "), strings.Join(s.allowedSeverities, ", "), s.defaultSeverity)
return fmt.Errorf("severities (%v) are not inside supported values (%v), fallback to '%s'",
strings.Join(names, ", "), strings.Join(s.allowedSeverities, ", "), s.defaultSeverity)
}
2 changes: 1 addition & 1 deletion pkg/printers/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

// Tab prints issues using tabulation as field separator.
// Tab prints issues using tabulation as a field separator.
type Tab struct {
printLinterName bool
useColors bool
Expand Down

0 comments on commit dfd79c9

Please sign in to comment.