Skip to content

Commit

Permalink
Merge pull request #10 from joshdk/feature/test-linting
Browse files Browse the repository at this point in the history
Better linting and testing of code
  • Loading branch information
joshdk committed Apr 3, 2018
2 parents 8b45d0d + a42bece commit 92ca7b6
Show file tree
Hide file tree
Showing 12 changed files with 1,840 additions and 3 deletions.
20 changes: 19 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,22 @@ jobs:
working_directory: /go/src/github.com/joshdk/go-junit
steps:
- checkout
- run: gofmt -l -s .
- run:
name: Install build tools
command: |
go get -u github.com/alecthomas/gometalinter
go get -u github.com/jstemmer/go-junit-report
gometalinter --install
mkdir test-results
- run:
name: Lint Go code
command: gometalinter
- run:
name: Test Go code
command: go test -v 2>&1 | tee test-results/report.log
- run:
name: Generate test report
command: cat test-results/report.log | go-junit-report -go-version $GOLANG_VERSION > test-results/report.xml
when: always
- store_test_results:
path: test-results
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test-results/
32 changes: 32 additions & 0 deletions .gometalinter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"Disable": [
"gosimple",
"lll",
"staticcheck",
"unused"
],
"Enable": [
"deadcode",
"dupl",
"errcheck",
"gas",
"goconst",
"gocyclo",
"goimports",
"golint",
"gotype",
"gotypex",
"ineffassign",
"interfacer",
"maligned",
"megacheck",
"misspell",
"nakedret",
"safesql",
"structcheck",
"unconvert",
"unparam",
"varcheck",
"vet"
]
}
7 changes: 5 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

166 changes: 166 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,178 @@
package junit

import (
"encoding/xml"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReparent(t *testing.T) {
tests := []struct {
title string
input []byte
expected string
}{
{
title: "nil input",
expected: "<fake-root></fake-root>",
},
{
title: "empty input",
input: []byte(""),
expected: "<fake-root></fake-root>",
},
{
title: "xml input",
input: []byte(`<testcase name="unit tests" />`),
expected: `<fake-root><testcase name="unit tests" /></fake-root>`,
},
}

for index, test := range tests {
name := fmt.Sprintf("#%d - %s", index+1, test.title)

t.Run(name, func(t *testing.T) {
actual := reparentXML(test.input)

assert.Equal(t, test.expected, string(actual))
})
}
}

func TestParse(t *testing.T) {
tests := []struct {
title string
input []byte
expected []xmlNode
}{
{
title: "nil input",
},
{
title: "empty input",
input: []byte(``),
},
{
title: "plaintext input",
input: []byte(`This is some data that does not look like xml.`),
},
{
title: "json input",
input: []byte(`{"This is some data": "that looks like json"}`),
},
{
title: "single xml node",
input: []byte(`<this-is-a-tag/>`),
expected: []xmlNode{
{
XMLName: xml.Name{
Local: "this-is-a-tag",
},
},
},
},
{
title: "multiple xml nodes",
input: []byte(`
<this-is-a-tag/>
<this-is-also-a-tag/>
`),
expected: []xmlNode{
{
XMLName: xml.Name{
Local: "this-is-a-tag",
},
},
{
XMLName: xml.Name{
Local: "this-is-also-a-tag",
},
},
},
},
{
title: "single xml node with content",
input: []byte(`<this-is-a-tag>This is some content.</this-is-a-tag>`),
expected: []xmlNode{
{
XMLName: xml.Name{
Local: "this-is-a-tag",
},
Content: []byte("This is some content."),
},
},
},
{
title: "single xml node with encoded content",
input: []byte(`<this-is-a-tag>&lt;sender&gt;John Smith&lt;/sender&gt;</this-is-a-tag>`),
expected: []xmlNode{
{
XMLName: xml.Name{
Local: "this-is-a-tag",
},
Content: []byte("<sender>John Smith</sender>"),
},
},
},
{
title: "single xml node with cdata content",
input: []byte(`<this-is-a-tag><![CDATA[<sender>John Smith</sender>]]></this-is-a-tag>`),
expected: []xmlNode{
{
XMLName: xml.Name{
Local: "this-is-a-tag",
},
Content: []byte("<sender>John Smith</sender>"),
},
},
},
{
title: "single xml node with attributes",
input: []byte(`<this-is-a-tag name="my name" status="passed"></this-is-a-tag>`),
expected: []xmlNode{
{
XMLName: xml.Name{
Local: "this-is-a-tag",
},
Attrs: map[string]string{
"name": "my name",
"status": "passed",
},
},
},
},
{
title: "single xml node with encoded attributes",
input: []byte(`<this-is-a-tag name="&lt;sender&gt;John Smith&lt;/sender&gt;"></this-is-a-tag>`),
expected: []xmlNode{
{
XMLName: xml.Name{
Local: "this-is-a-tag",
},
Attrs: map[string]string{
"name": "<sender>John Smith</sender>",
},
},
},
},
}

for index, test := range tests {
name := fmt.Sprintf("#%d - %s", index+1, test.title)

t.Run(name, func(t *testing.T) {
actual, err := parse(test.input)

require.Nil(t, err)

assert.Equal(t, test.expected, actual)
})
}
}

func TestExtract(t *testing.T) {
tests := []struct {
title string
Expand Down
28 changes: 28 additions & 0 deletions vendor/github.com/stretchr/testify/require/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/stretchr/testify/require/forward_requirements.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 92ca7b6

Please sign in to comment.