Fix golint suggestions

This commit is contained in:
Joël Stemmer 2015-02-06 13:57:44 +01:00
parent f992d5acaa
commit e705d170a3
4 changed files with 36 additions and 27 deletions

View File

@ -6,11 +6,13 @@ import (
"os" "os"
) )
var noXmlHeader bool var (
var packageName string noXMLHeader bool
packageName string
)
func init() { func init() {
flag.BoolVar(&noXmlHeader, "no-xml-header", false, "do not print xml header") flag.BoolVar(&noXMLHeader, "no-xml-header", false, "do not print xml header")
flag.StringVar(&packageName, "package-name", "", "specify a package name (compiled test have no package name in output)") flag.StringVar(&packageName, "package-name", "", "specify a package name (compiled test have no package name in output)")
} }
@ -25,7 +27,7 @@ func main() {
} }
// Write xml // Write xml
err = JUnitReportXML(report, noXmlHeader, os.Stdout) err = JUnitReportXML(report, noXMLHeader, os.Stdout)
if err != nil { if err != nil {
fmt.Printf("Error writing XML: %s\n", err) fmt.Printf("Error writing XML: %s\n", err)
os.Exit(1) os.Exit(1)

View File

@ -14,11 +14,11 @@ type TestCase struct {
name string name string
reportName string reportName string
report *Report report *Report
noXmlHeader bool noXMLHeader bool
packageName string packageName string
} }
var testCases []TestCase = []TestCase{ var testCases = []TestCase{
{ {
name: "01-pass.txt", name: "01-pass.txt",
reportName: "01-report.xml", reportName: "01-report.xml",
@ -155,7 +155,7 @@ var testCases []TestCase = []TestCase{
}, },
}, },
}, },
noXmlHeader: true, noXMLHeader: true,
}, },
{ {
name: "06-mixed.txt", name: "06-mixed.txt",
@ -205,7 +205,7 @@ var testCases []TestCase = []TestCase{
}, },
}, },
}, },
noXmlHeader: true, noXMLHeader: true,
}, },
{ {
name: "07-compiled_test.txt", name: "07-compiled_test.txt",
@ -308,7 +308,7 @@ func TestJUnitFormatter(t *testing.T) {
var junitReport bytes.Buffer var junitReport bytes.Buffer
if err = JUnitReportXML(testCase.report, testCase.noXmlHeader, &junitReport); err != nil { if err = JUnitReportXML(testCase.report, testCase.noXMLHeader, &junitReport); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -9,11 +9,14 @@ import (
"strings" "strings"
) )
// JUnitTestSuites is a collection of JUnit test suites.
type JUnitTestSuites struct { type JUnitTestSuites struct {
XMLName xml.Name `xml:"testsuites"` XMLName xml.Name `xml:"testsuites"`
Suites []JUnitTestSuite Suites []JUnitTestSuite
} }
// JUnitTestSuite is a single JUnit test suite which may contain many
// testcases.
type JUnitTestSuite struct { type JUnitTestSuite struct {
XMLName xml.Name `xml:"testsuite"` XMLName xml.Name `xml:"testsuite"`
Tests int `xml:"tests,attr"` Tests int `xml:"tests,attr"`
@ -24,6 +27,7 @@ type JUnitTestSuite struct {
TestCases []JUnitTestCase TestCases []JUnitTestCase
} }
// JUnitTestCase is a single test case with its result.
type JUnitTestCase struct { type JUnitTestCase struct {
XMLName xml.Name `xml:"testcase"` XMLName xml.Name `xml:"testcase"`
Classname string `xml:"classname,attr"` Classname string `xml:"classname,attr"`
@ -33,31 +37,27 @@ type JUnitTestCase struct {
Failure *JUnitFailure `xml:"failure,omitempty"` Failure *JUnitFailure `xml:"failure,omitempty"`
} }
// JUnitSkipMessage contains the reason why a testcase was skipped.
type JUnitSkipMessage struct { type JUnitSkipMessage struct {
Message string `xml:"message,attr"` Message string `xml:"message,attr"`
} }
// JUnitProperty represents a key/value pair used to define properties.
type JUnitProperty struct { type JUnitProperty struct {
Name string `xml:"name,attr"` Name string `xml:"name,attr"`
Value string `xml:"value,attr"` Value string `xml:"value,attr"`
} }
// JUnitFailure contains data related to a failed test.
type JUnitFailure struct { type JUnitFailure struct {
Message string `xml:"message,attr"` Message string `xml:"message,attr"`
Type string `xml:"type,attr"` Type string `xml:"type,attr"`
Contents string `xml:",chardata"` Contents string `xml:",chardata"`
} }
func NewJUnitProperty(name, value string) JUnitProperty { // JUnitReportXML writes a JUnit xml representation of the given report to w
return JUnitProperty{
Name: name,
Value: value,
}
}
// JUnitReportXML writes a junit xml representation of the given report to w
// in the format described at http://windyroad.org/dl/Open%20Source/JUnit.xsd // in the format described at http://windyroad.org/dl/Open%20Source/JUnit.xsd
func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error { func JUnitReportXML(report *Report, noXMLHeader bool, w io.Writer) error {
suites := JUnitTestSuites{} suites := JUnitTestSuites{}
// convert Report to JUnit test suites // convert Report to JUnit test suites
@ -77,7 +77,7 @@ func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error {
} }
// properties // properties
ts.Properties = append(ts.Properties, NewJUnitProperty("go.version", runtime.Version())) ts.Properties = append(ts.Properties, JUnitProperty{"go.version", runtime.Version()})
// individual test cases // individual test cases
for _, test := range pkg.Tests { for _, test := range pkg.Tests {
@ -89,8 +89,7 @@ func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error {
} }
if test.Result == FAIL { if test.Result == FAIL {
ts.Failures += 1 ts.Failures++
testCase.Failure = &JUnitFailure{ testCase.Failure = &JUnitFailure{
Message: "Failed", Message: "Failed",
Type: "", Type: "",
@ -116,7 +115,7 @@ func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error {
writer := bufio.NewWriter(w) writer := bufio.NewWriter(w)
if !noXmlHeader { if !noXMLHeader {
writer.WriteString(xml.Header) writer.WriteString(xml.Header)
} }
@ -130,7 +129,7 @@ func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error {
func countFailures(tests []Test) (result int) { func countFailures(tests []Test) (result int) {
for _, test := range tests { for _, test := range tests {
if test.Result == FAIL { if test.Result == FAIL {
result += 1 result++
} }
} }
return return

View File

@ -8,24 +8,29 @@ import (
"strings" "strings"
) )
// Result represents a test result.
type Result int type Result int
// Test result constants
const ( const (
PASS Result = iota PASS Result = iota
FAIL FAIL
SKIP SKIP
) )
// Report is a collection of package tests.
type Report struct { type Report struct {
Packages []Package Packages []Package
} }
// Package contains the test results of a single package.
type Package struct { type Package struct {
Name string Name string
Time int Time int
Tests []Test Tests []Test
} }
// Test contains the results of a single test.
type Test struct { type Test struct {
Name string Name string
Time int Time int
@ -38,13 +43,16 @@ var (
regexResult = regexp.MustCompile(`^(ok|FAIL)\s+(.+)\s(\d+\.\d+)s$`) regexResult = regexp.MustCompile(`^(ok|FAIL)\s+(.+)\s(\d+\.\d+)s$`)
) )
func Parse(r io.Reader, specifiedPackageName string) (*Report, error) { // Parse parses go test output from reader r and returns a report with the
// results. An optional pkgName can be given, which is used in case a package
// result line is missing.
func Parse(r io.Reader, pkgName string) (*Report, error) {
reader := bufio.NewReader(r) reader := bufio.NewReader(r)
report := &Report{make([]Package, 0)} report := &Report{make([]Package, 0)}
// keep track of tests we find // keep track of tests we find
tests := make([]Test, 0) var tests []Test
// sum of tests' time, use this if current test has no result line (when it is compiled test) // sum of tests' time, use this if current test has no result line (when it is compiled test)
testsTime := 0 testsTime := 0
@ -115,9 +123,9 @@ func Parse(r io.Reader, specifiedPackageName string) (*Report, error) {
tests = append(tests, *test) tests = append(tests, *test)
} }
if len(tests) > 0 { //no result line found if len(tests) > 0 { // no result line found
report.Packages = append(report.Packages, Package{ report.Packages = append(report.Packages, Package{
Name: specifiedPackageName, Name: pkgName,
Time: testsTime, Time: testsTime,
Tests: tests, Tests: tests,
}) })