Skip to content

Commit

Permalink
fix(parser): panic when processing a test run with no tests
Browse files Browse the repository at this point in the history
(fixes #3)
+semver:patch
  • Loading branch information
deltics committed Apr 22, 2024
1 parent 4222eca commit 625c87d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 48 deletions.
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# ide and macOS finder files
.idea/**
.vscode/**
**/.DS_Store

# in general we ignore vscode settings, with some exceptions
.vscode/**
# exceptions for vs code
!.vscode/spellright.dict

# Binaries for programs and plugins
Expand All @@ -19,14 +19,11 @@
# go coverage tool output
*.out

# workspace file
go.work

# dependency directories (uncomment the line below to include)
# vendor/

# test data (generated when parser_test.go is run)
testdata/.json
testdata/*.json

# any binary or test/sample report (md) in the root
test-report
Expand Down
4 changes: 4 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func (p *parser) recordFailure(line *line, rpt *testrun) {
// recordSkip records a test skip, updating the test result and incrementing
// the number of skipped tests in the testrun.
func (p *parser) recordSkip(line *line, rpt *testrun) {
if line.Test == nil {
p.pkgs[line.Package].passed = false
return
}
p.tests[line.Package][*line.Test].result = trSkipped
rpt.numSkipped++
}
Expand Down
125 changes: 83 additions & 42 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,103 @@ import (
func generate() {
pwd, _ := os.Getwd()
defer func() { _ = os.Chdir(pwd) }()

if err := os.Chdir("testdata"); err != nil {
fmt.Printf("cd testdata: %s\n", err)
fmt.Printf("chdir 'testdata': %s\n", err)
return
}

cmd := exec.Command("go", "test", "./...", "-json")
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%s: %s\n", cmd, err)
}
testPackages := func(dest string, pkgs ...string) {
cmd := exec.Command("go", append([]string{"test", "-json"}, pkgs...)...)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%s: %s\n", cmd, err)
}

file, err := os.Create(".json")
if err != nil {
fmt.Printf("create testdata/.json: %s\n", err)
}
defer file.Close()
if err := cmd.Run(); err != nil {
fmt.Printf("run '%s': %s\n", cmd, err)
}
file, err := os.Create(dest)
if err != nil {
fmt.Printf("create testdata/%s: %s\n", dest, err)
}
defer file.Close()
if err := cmd.Run(); err != nil {
fmt.Printf("run '%s': %s\n", cmd, err)
}

if _, err := io.Copy(file, bytes.NewReader(output)); err != nil {
fmt.Printf("save output to 'testdata/.json': %s\n", err)
if _, err := io.Copy(file, bytes.NewReader(output)); err != nil {
fmt.Printf("save output to 'testdata/%s': %s\n", dest, err)
}
}

testPackages("packages.json", "./pkga", "./pkgb")
testPackages("no-test-files.json", "./no-test-files")
testPackages("no-code.json", "./no-code")
}

func TestParse(t *testing.T) {
// ARRANGE
generate()

report := &testrun{}
input, err := os.Open("./testdata/.json")
if err != nil {
t.Fatalf("error loading test data: %s", err)
}
defer input.Close()
p := parser{}
// ARRANGE
testcases := []struct {
scenario string
exec func(t *testing.T)
}{
{scenario: "packages.json",
exec: func(t *testing.T) {
report := &testrun{}
input, err := os.Open("./testdata/packages.json")
if err != nil {
t.Fatalf("error loading test data: %s", err)
}
defer input.Close()
p := parser{}

// ACT
err = p.parse(input, report)

// ASSERT
test.Error(t, err).IsNil()
test.That(t, len(report.packages)).Equals(2, "number of packages")
test.That(t, report.numTests).Equals(9, "number of tests")
test.That(t, report.numPassed).Equals(3, "tests passed")
test.That(t, report.numFailed).Equals(4, "tests failed")
test.That(t, report.numSkipped).Equals(2, "tests skipped")

// ACT
err = p.parse(input, report)
test.Map(t, report.packages[1].tests[1].output).Equals(map[string][]string{
"pkgb_test.go:8": {
"this test fails",
"with four",
"lines of output",
" the last is indented",
},
})
},
},
{scenario: "no-test-files.json",
exec: func(t *testing.T) {
report := &testrun{}
input, err := os.Open("./testdata/no-test-files.json")
if err != nil {
t.Fatalf("error loading test data: %s", err)
}
defer input.Close()
p := parser{}

// ASSERT
test.Error(t, err).IsNil()
test.That(t, len(report.packages)).Equals(2, "number of packages")
test.That(t, report.numTests).Equals(9, "number of tests")
test.That(t, report.numPassed).Equals(3, "tests passed")
test.That(t, report.numFailed).Equals(4, "tests failed")
test.That(t, report.numSkipped).Equals(2, "tests skipped")
// ACT
err = p.parse(input, report)

test.Map(t, report.packages[1].tests[1].output).Equals(map[string][]string{
"pkgb_test.go:8": {
"this test fails",
"with four",
"lines of output",
" the last is indented",
// ASSERT
test.Error(t, err).IsNil()
test.That(t, len(report.packages)).Equals(1, "number of packages")
test.That(t, report.numTests).Equals(0, "number of tests")
test.That(t, report.numPassed).Equals(0, "tests passed")
test.That(t, report.numFailed).Equals(0, "tests failed")
test.That(t, report.numSkipped).Equals(0, "tests skipped")
},
},
})
// test.That(t, report.packages[1].tests[1].output["pkgb_test.go:12"][0]).Equals("TestFails")
// test.That(t, report.packages["github.com/blugnu/test-report/testdata/pkgb"]["TestFails"]).IsNotNil()
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
tc.exec(t)
})
}
}
2 changes: 2 additions & 0 deletions testdata/no-code/.empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
this is a placeholder file to force git to track this folder which is otherwise empty
for the purposes of this project
3 changes: 3 additions & 0 deletions testdata/no-test-files/no-test-files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package notests

const README = "this package contains no tests"

0 comments on commit 625c87d

Please sign in to comment.