Skip to content

Commit

Permalink
Add source attribute in the checkstyle format (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Jul 6, 2024
1 parent 39efd18 commit 45b8f41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
9 changes: 7 additions & 2 deletions formatter/checkstyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
)

type checkstyleError struct {
Rule string `xml:"rule,attr"`
Source string `xml:"source,attr"`
Line int `xml:"line,attr"`
Column int `xml:"column,attr"`
Severity string `xml:"severity,attr"`
Message string `xml:"message,attr"`
Link string `xml:"link,attr"`

// Deprecated: Use `source` instead
Rule string `xml:"rule,attr"`
}

type checkstyleFile struct {
Expand All @@ -30,12 +33,14 @@ func (f *Formatter) checkstylePrint(issues tflint.Issues, appErr error, sources
files := map[string]*checkstyleFile{}
for _, issue := range issues {
cherr := &checkstyleError{
Rule: issue.Rule.Name(),
Source: issue.Rule.Name(),
Line: issue.Range.Start.Line,
Column: issue.Range.Start.Column,
Severity: toSeverity(issue.Rule.Severity()),
Message: issue.Message,
Link: issue.Rule.Link(),

Rule: issue.Rule.Name(),
}

if file, exists := files[issue.Range.Filename]; exists {
Expand Down
18 changes: 10 additions & 8 deletions formatter/checkstyle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,23 @@ func Test_checkstylePrint(t *testing.T) {
Stdout: `<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="test.tf">
<error rule="test_rule" line="1" column="1" severity="error" message="test" link="https://github.com"></error>
<error source="test_rule" line="1" column="1" severity="error" message="test" link="https://github.com" rule="test_rule"></error>
</file>
</checkstyle>`,
},
}

for _, tc := range cases {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
formatter := &Formatter{Stdout: stdout, Stderr: stderr}
t.Run(tc.Name, func(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
formatter := &Formatter{Stdout: stdout, Stderr: stderr}

formatter.checkstylePrint(tc.Issues, tc.Error, map[string][]byte{})
formatter.checkstylePrint(tc.Issues, tc.Error, map[string][]byte{})

if stdout.String() != tc.Stdout {
t.Fatalf("Failed %s test: expected=%s, stdout=%s", tc.Name, tc.Stdout, stdout.String())
}
if stdout.String() != tc.Stdout {
t.Fatalf("expected=%s, stdout=%s", tc.Stdout, stdout.String())
}
})
}
}

0 comments on commit 45b8f41

Please sign in to comment.