-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package printer | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
|
||
"sort" | ||
|
||
"github.com/wata727/tflint/issue" | ||
) | ||
|
||
type Error struct { | ||
Line int `xml:"line,attr"` | ||
Severity string `xml:"severity,attr"` | ||
Message string `xml:"message,attr"` | ||
} | ||
|
||
type File struct { | ||
Name string `xml:"name,attr"` | ||
Errors []Error `xml:"error"` | ||
} | ||
|
||
type Checkstyle struct { | ||
XMLName xml.Name `xml:"checkstyle"` | ||
Files []File `xml:"file"` | ||
} | ||
|
||
func (p *Printer) CheckstylePrint(issues []*issue.Issue) { | ||
sort.Sort(issue.ByFile{Issues: issue.Issues(issues)}) | ||
|
||
v := &Checkstyle{} | ||
|
||
for _, i := range issues { | ||
if len(v.Files) == 0 || v.Files[len(v.Files)-1].Name != i.File { | ||
v.Files = append(v.Files, File{Name: i.File}) | ||
} | ||
v.Files[len(v.Files)-1].Errors = append(v.Files[len(v.Files)-1].Errors, Error{Line: i.Line, Severity: toSeverity(i.Type), Message: i.Message}) | ||
} | ||
|
||
result, err := xml.MarshalIndent(v, "", " ") | ||
if err != nil { | ||
fmt.Fprint(p.stderr, err) | ||
} | ||
fmt.Fprint(p.stdout, xml.Header) | ||
fmt.Fprint(p.stdout, string(result)) | ||
} | ||
|
||
func toSeverity(lintType string) string { | ||
switch lintType { | ||
case issue.ERROR: | ||
return "error" | ||
case issue.WARNING: | ||
return "warning" | ||
case issue.NOTICE: | ||
return "info" | ||
default: | ||
return "unknown" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package printer | ||
|
||
import ( | ||
"testing" | ||
|
||
"bytes" | ||
|
||
"github.com/wata727/tflint/issue" | ||
) | ||
|
||
func TestCheckstylePrint(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Input []*issue.Issue | ||
Result string | ||
}{ | ||
{ | ||
Name: "no issues", | ||
Input: []*issue.Issue{}, | ||
Result: `<?xml version="1.0" encoding="UTF-8"?> | ||
<checkstyle></checkstyle>`, | ||
}, | ||
{ | ||
Name: "multi files", | ||
Input: []*issue.Issue{ | ||
{ | ||
File: "template.tf", | ||
Line: 1, | ||
Type: "ERROR", | ||
Message: "example error message", | ||
}, | ||
{ | ||
File: "application.tf", | ||
Line: 10, | ||
Type: "NOTICE", | ||
Message: "example notice message", | ||
}, | ||
{ | ||
File: "template.tf", | ||
Line: 3, | ||
Type: "WARNING", | ||
Message: "example warning message", | ||
}, | ||
}, | ||
Result: `<?xml version="1.0" encoding="UTF-8"?> | ||
<checkstyle> | ||
<file name="application.tf"> | ||
<error line="10" severity="info" message="example notice message"></error> | ||
</file> | ||
<file name="template.tf"> | ||
<error line="1" severity="error" message="example error message"></error> | ||
<error line="3" severity="warning" message="example warning message"></error> | ||
</file> | ||
</checkstyle>`, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
stdout := &bytes.Buffer{} | ||
stderr := &bytes.Buffer{} | ||
p := NewPrinter(stdout, stderr) | ||
p.CheckstylePrint(tc.Input) | ||
result := stdout.String() | ||
|
||
if result != tc.Result { | ||
t.Fatalf("\nBad: %t\nExpected: %t\n\ntestcase: %s", result, tc.Result, tc.Name) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters