-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 'checkstyle' output (#209)
* Add support for 'checkstyle' output * improve code comments * remove severity translation * use xml encoder * use xml encoder * update checkstyle usage to show exact xml output * use assert.PanicsWithError
- Loading branch information
1 parent
9d4a722
commit e588a3e
Showing
6 changed files
with
176 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package printer | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/get-woke/woke/pkg/result" | ||
) | ||
|
||
// Checkstyle is a Checkstyle printer meant for use by a Checkstyle annotation | ||
type Checkstyle struct { | ||
writer io.Writer | ||
encoder *xml.Encoder | ||
} | ||
|
||
// NewCheckstyle returns a new Checkstyle printer | ||
func NewCheckstyle(w io.Writer) *Checkstyle { | ||
return &Checkstyle{ | ||
writer: w, | ||
encoder: xml.NewEncoder(w), | ||
} | ||
} | ||
|
||
type File struct { | ||
XMLName xml.Name `xml:"file"` | ||
Name string `xml:"name,attr"` | ||
Errors []Error `xml:"error"` | ||
} | ||
type Error struct { | ||
XMLName xml.Name `xml:"error"` | ||
Column int `xml:"column,attr"` | ||
Line int `xml:"line,attr"` | ||
Message string `xml:"message,attr"` | ||
Severity string `xml:"severity,attr"` | ||
Source string `xml:"source,attr"` | ||
} | ||
|
||
func (p *Checkstyle) PrintSuccessExitMessage() bool { | ||
return true | ||
} | ||
|
||
// Print prints in the format for Checkstyle. | ||
// https://github.com/checkstyle/checkstyle | ||
func (p *Checkstyle) Print(fs *result.FileResults) error { | ||
var f File | ||
f.Name = fs.Filename | ||
for _, r := range fs.Results { | ||
f.Errors = append(f.Errors, Error{ | ||
Column: r.GetStartPosition().Column, | ||
Line: r.GetStartPosition().Line, | ||
Message: r.Reason(), | ||
Severity: r.GetSeverity().String(), | ||
Source: "woke", | ||
}) | ||
} | ||
return p.encoder.Encode(f) | ||
} | ||
|
||
func (p *Checkstyle) Start() { | ||
fmt.Fprint(p.writer, xml.Header) | ||
p.encoder.Indent("", " ") | ||
if err := p.encoder.EncodeToken(xml.StartElement{ | ||
Name: xml.Name{Local: "checkstyle"}, | ||
Attr: []xml.Attr{ | ||
{Name: xml.Name{Local: "version"}, Value: "5.0"}, | ||
}, | ||
}); err != nil { | ||
panic(err) | ||
} | ||
if err := p.encoder.Flush(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (p *Checkstyle) End() { | ||
if err := p.encoder.EncodeToken(xml.EndElement{Name: xml.Name{Local: "checkstyle"}}); err != nil { | ||
panic(err) | ||
} | ||
if err := p.encoder.Flush(); err != nil { | ||
panic(err) | ||
} | ||
} |
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,65 @@ | ||
package printer | ||
|
||
import ( | ||
"bytes" | ||
"go/token" | ||
"testing" | ||
|
||
"github.com/get-woke/woke/pkg/result" | ||
"github.com/get-woke/woke/pkg/rule" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFormatResultForCheckstyle(t *testing.T) { | ||
testResults := result.FileResults{ | ||
Filename: "my/file", | ||
Results: []result.Result{ | ||
result.LineResult{ | ||
Rule: &rule.TestRule, | ||
Finding: "whitelist", | ||
StartPosition: &token.Position{ | ||
Filename: "my/file", | ||
Offset: 0, | ||
Line: 5, | ||
Column: 3, | ||
}, | ||
EndPosition: &token.Position{ | ||
Filename: "my/file", | ||
Offset: 0, | ||
Line: 5, | ||
Column: 12, | ||
}, | ||
}, | ||
}, | ||
} | ||
buf := new(bytes.Buffer) | ||
p := NewCheckstyle(buf) | ||
p.Start() | ||
p.Print(&testResults) | ||
p.End() | ||
expected := `<?xml version="1.0" encoding="UTF-8"?> | ||
<checkstyle version="5.0"> | ||
<file name="my/file"> | ||
<error column="3" line="5" message="` + "`" + `whitelist` + "`" + ` may be insensitive, use ` + "`" + `allowlist` + "`" + ` instead" severity="warning" source="woke"></error> | ||
</file> | ||
</checkstyle>` | ||
assert.Equal(t, expected, buf.String()) | ||
} | ||
|
||
func TestCheckstyle_Start(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
p := NewCheckstyle(buf) | ||
p.Start() | ||
got := buf.String() | ||
|
||
expected := `<?xml version="1.0" encoding="UTF-8"?> | ||
<checkstyle version="5.0">` | ||
assert.Equal(t, expected, got) | ||
} | ||
|
||
func TestCheckstyle_End(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
p := NewCheckstyle(buf) | ||
assert.PanicsWithError(t, "xml: end tag </checkstyle> without start tag", func() { p.End() }) | ||
} |
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