Skip to content

Commit

Permalink
Fixes #2: Incorrect parsing of Testify output (#3)
Browse files Browse the repository at this point in the history
* Fixes #2: Incorrect parsing of Testify output

* Added artifact build

* Added non-verbose self-test

* Longer buffering
  • Loading branch information
Janos Bonic authored Nov 11, 2021
1 parent f551649 commit 5ed11e4
Show file tree
Hide file tree
Showing 30 changed files with 581 additions and 28 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ jobs:
- name: Run self-test
run: |
set -euo pipefail
go test -v ./... 2>&1 | tee /tmp/gotest.log | go run ./cmd/gotestfmt
go test ./... 2>&1 | tee /tmp/gotest.log | go run ./cmd/gotestfmt
- name: Run self-test (verbose)
run: |
set -euo pipefail
go test -v ./... 2>&1 | tee /tmp/gotest-verbose.log | go run ./cmd/gotestfmt
- name: Upload test log
uses: actions/upload-artifact@v2
with:
Expand Down Expand Up @@ -132,3 +136,8 @@ jobs:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: binaries
path: dist
7 changes: 7 additions & 0 deletions _testsource/testify/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "github.com/haveyoudebuggedit/example"

go 1.16

require (
github.com/stretchr/testify v1.7.0
)
10 changes: 10 additions & 0 deletions _testsource/testify/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions _testsource/testify/testify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package testify
12 changes: 12 additions & 0 deletions _testsource/testify/testify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package testify

import (
"testing"

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

func TestTestify(t *testing.T) {
assert.Equal(t, 50, 48)

}
7 changes: 7 additions & 0 deletions _testsource/testifymultipackage/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "github.com/haveyoudebuggedit/example"

go 1.16

require (
github.com/stretchr/testify v1.7.0
)
10 changes: 10 additions & 0 deletions _testsource/testifymultipackage/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions _testsource/testifymultipackage/subpackage1/testify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package subpackage1
12 changes: 12 additions & 0 deletions _testsource/testifymultipackage/subpackage1/testify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package subpackage1

import (
"testing"

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

func TestTestify(t *testing.T) {
assert.Equal(t, 50, 50)

}
1 change: 1 addition & 0 deletions _testsource/testifymultipackage/subpackage2/testify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package subpackage1
11 changes: 11 additions & 0 deletions _testsource/testifymultipackage/subpackage2/testify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package subpackage1

import (
"testing"

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

func TestTestify(t *testing.T) {
assert.Equal(t, 50, 48)
}
53 changes: 38 additions & 15 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func parse(
}
pkgTracker := &packageTracker{
currentPackage: nil,
packageList: []*Package{},
packages: map[string]*Package{},
testCases: nil,
lastTestCase: nil,
testCasesByName: map[string]*TestCase{},
Expand Down Expand Up @@ -142,16 +144,24 @@ func parse(
}
case tokenizer.ActionRun:
fallthrough
case tokenizer.ActionPass:
fallthrough
case tokenizer.ActionFail:
fallthrough
case tokenizer.ActionSkip:
fallthrough
case tokenizer.ActionCont:
lastTestCase := pkgTracker.GetLastTestCase()
if lastTestCase.Output == "" {
lastTestCase.Output = string(evt.Output)
} else {
lastTestCase.Output = fmt.Sprintf(
"%s\n%s",
lastTestCase.Output,
string(evt.Output),
)
if lastTestCase != nil && len(evt.Output) > 0 {
if lastTestCase.Output == "" {
lastTestCase.Output = string(evt.Output)
} else {
lastTestCase.Output = fmt.Sprintf(
"%s\n%s",
lastTestCase.Output,
string(evt.Output),
)
}
}
case tokenizer.ActionPackage:
lastPackage := pkgTracker.GetPackage()
Expand All @@ -164,6 +174,12 @@ func parse(
string(evt.Output),
)
}
case tokenizer.ActionFailFinal:
fallthrough
case tokenizer.ActionPassFinal:
fallthrough
case tokenizer.ActionSkipFinal:
pkgTracker.Write()
default:
if len(evt.Output) > 0 {
panic(fmt.Errorf("unexpected output after %s event: %s", lastAction, evt.Output))
Expand Down Expand Up @@ -194,28 +210,32 @@ func finish(evt tokenizer.Event, pkgTracker *packageTracker, result Result) {
Coverage: evt.Coverage,
},
)
pkgTracker.Write()
}
}

type packageTracker struct {
currentPackage *Package
packages map[string]*Package
testCases []*TestCase
lastTestCase *TestCase
testCasesByName map[string]*TestCase
target chan<- *Package
packageList []*Package
}

func (p *packageTracker) SetPackage(pkg *Package) {
if p.currentPackage == nil {
pkg.TestCases = p.testCases
p.currentPackage = pkg
if _, ok := p.packages[pkg.Name]; !ok {
p.packageList = append(p.packageList, pkg)
p.packages[pkg.Name] = pkg
}

p.testCases = nil
return
} else if p.currentPackage.Name != pkg.Name {
// Write the current package
p.Write()
p.currentPackage = nil
p.currentPackage = p.packages[pkg.Name]
}

if pkg.Result != "" {
Expand Down Expand Up @@ -260,10 +280,13 @@ func (p *packageTracker) Write() {
}
}
p.testCases = nil
} else {
p.target <- p.currentPackage
p.currentPackage = nil
}
for _, pkg := range p.packageList {
p.target <- pkg
}
p.packages = map[string]*Package{}
p.packageList = []*Package{}
p.currentPackage = nil
p.testCasesByName = map[string]*TestCase{}
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/coverage-nostatements.tokenizer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "TestNothing"
},
{
"action": "pass"
"action": "pass-final"
},
{
"action": "coverage_nostatements"
Expand Down
2 changes: 1 addition & 1 deletion testdata/coverage.tokenizer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "TestNothing"
},
{
"action": "pass"
"action": "pass-final"
},
{
"action": "coverage",
Expand Down
63 changes: 63 additions & 0 deletions testdata/gotestfmt.parser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"prefix": null,
"downloads": {
"packages": null,
"failed": false
},
"packages": [
{
"name": "github.com/haveyoudebuggedit/gotestfmt",
"result": "SKIP",
"duration": "0s",
"coverage": null,
"output": "",
"testcases": null,
"reason": "no test files"
},
{
"name": "github.com/haveyoudebuggedit/gotestfmt/cmd/gotestfmt",
"result": "SKIP",
"duration": "0s",
"coverage": null,
"output": "",
"testcases": null,
"reason": "no test files"
},
{
"name": "github.com/haveyoudebuggedit/gotestfmt/parser",
"result": "PASS",
"duration": "0s",
"coverage": null,
"output": "",
"testcases": null,
"reason": ""
},
{
"name": "github.com/haveyoudebuggedit/gotestfmt/renderer",
"result": "SKIP",
"duration": "0s",
"coverage": null,
"output": "",
"testcases": null,
"reason": "no test files"
},
{
"name": "github.com/haveyoudebuggedit/gotestfmt/testutil",
"result": "SKIP",
"duration": "0s",
"coverage": null,
"output": "",
"testcases": null,
"reason": "no test files"
},
{
"name": "github.com/haveyoudebuggedit/gotestfmt/tokenizer",
"result": "PASS",
"duration": "0s",
"coverage": null,
"output": "",
"testcases": null,
"reason": ""
}
]
}
72 changes: 72 additions & 0 deletions testdata/gotestfmt.tokenizer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[
{
"action": "skip",
"package": "github.com/haveyoudebuggedit/gotestfmt",
"version": "",
"test": "",
"elapsed": "0s",
"output": "bm8gdGVzdCBmaWxlcw==",
"cached": false,
"coverage": null
},
{
"action": "skip",
"package": "github.com/haveyoudebuggedit/gotestfmt/cmd/gotestfmt",
"version": "",
"test": "",
"elapsed": "0s",
"output": "bm8gdGVzdCBmaWxlcw==",
"cached": false,
"coverage": null
},
{
"action": "pass",
"package": "github.com/haveyoudebuggedit/gotestfmt/parser",
"version": "",
"test": "",
"elapsed": "0s",
"output": null,
"cached": true,
"coverage": null
},
{
"action": "skip",
"package": "github.com/haveyoudebuggedit/gotestfmt/renderer",
"version": "",
"test": "",
"elapsed": "0s",
"output": "bm8gdGVzdCBmaWxlcw==",
"cached": false,
"coverage": null
},
{
"action": "skip",
"package": "github.com/haveyoudebuggedit/gotestfmt/testutil",
"version": "",
"test": "",
"elapsed": "0s",
"output": "bm8gdGVzdCBmaWxlcw==",
"cached": false,
"coverage": null
},
{
"action": "pass",
"package": "github.com/haveyoudebuggedit/gotestfmt/tokenizer",
"version": "",
"test": "",
"elapsed": "0s",
"output": null,
"cached": true,
"coverage": null
},
{
"action": "stdout",
"package": "",
"version": "",
"test": "",
"elapsed": "0s",
"output": "",
"cached": false,
"coverage": null
}
]
6 changes: 6 additions & 0 deletions testdata/gotestfmt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
? github.com/haveyoudebuggedit/gotestfmt [no test files]
? github.com/haveyoudebuggedit/gotestfmt/cmd/gotestfmt [no test files]
ok github.com/haveyoudebuggedit/gotestfmt/parser (cached)
? github.com/haveyoudebuggedit/gotestfmt/renderer [no test files]
? github.com/haveyoudebuggedit/gotestfmt/testutil [no test files]
ok github.com/haveyoudebuggedit/gotestfmt/tokenizer (cached)
2 changes: 1 addition & 1 deletion testdata/mod-download.tokenizer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"test": "TestNothing"
},
{
"action": "pass"
"action": "pass-final"
},
{
"action": "pass",
Expand Down
2 changes: 1 addition & 1 deletion testdata/parallel.tokenizer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"elapsed": "10.020s"
},
{
"action": "pass"
"action": "pass-final"
},
{
"action": "pass",
Expand Down
Loading

0 comments on commit 5ed11e4

Please sign in to comment.