Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing "pass" event for subtests #188

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ type Package struct {
Skipped []TestCase
Passed []TestCase

// mapping of root TestCase ID to all sub test IDs. Used to mitigate
// github.com/golang/go/issues/29755.
// In the future when that bug is fixed this mapping can likely be removed.
subTests map[int][]int
// mapping of root TestCase ID to most recent subtest TestCases. Used to
// mitigate github.com/golang/go/issues/29755, and also to infer passage of
// subtests from passage of the parent test.
subTests map[int][]TestCase

// output printed by test cases, indexed by TestCase.ID. Package output is
// saved with key 0.
Expand Down Expand Up @@ -156,7 +156,7 @@ func (p *Package) OutputLines(tc TestCase) []string {
result := make([]string, 0, len(lines)+1)
result = append(result, lines...)
for _, sub := range p.subTests[tc.ID] {
result = append(result, p.output[sub]...)
result = append(result, p.output[sub.ID]...)
}
return result
}
Expand Down Expand Up @@ -191,8 +191,8 @@ func (p *Package) removeOutput(id int) {

skipped := tcIDSet(p.Skipped)
for _, sub := range p.subTests[id] {
if _, isSkipped := skipped[sub]; !isSkipped {
delete(p.output, sub)
if _, isSkipped := skipped[sub.ID]; !isSkipped {
delete(p.output, sub.ID)
}
}
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func newPackage() *Package {
return &Package{
output: make(map[int][]string),
running: make(map[string]TestCase),
subTests: make(map[int][]int),
subTests: make(map[int][]TestCase),
}
}

Expand Down Expand Up @@ -321,7 +321,7 @@ func (p *Package) addTestEvent(event TestEvent) {
if tc.Test.IsSubTest() {
root, _ := TestName(event.Test).Split()
rootID := p.running[root].ID
p.subTests[rootID] = append(p.subTests[rootID], tc.ID)
p.subTests[rootID] = append(p.subTests[rootID], tc)
}
return
}
Expand All @@ -344,6 +344,10 @@ func (p *Package) addTestEvent(event TestEvent) {
}

// the event.Action must be one of the three test end events
p.addEndTestEvent(event, tc)
}

func (p *Package) addEndTestEvent(event TestEvent, tc TestCase) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gocyclo forced this change on me. I'm personally not a fan of gocyclo and how it can force small fixes to be bundled with refactors, but hey, it's not my codebase.

delete(p.running, event.Test)
tc.Elapsed = elapsedDuration(event.Elapsed)

Expand Down Expand Up @@ -371,6 +375,14 @@ func (p *Package) addTestEvent(event TestEvent) {
return
}

// If a test passed, that implies its subtests passed too.
for _, subtest := range p.subTests[tc.ID] {
if _, ok := p.running[string(subtest.Test)]; ok {
delete(p.running, string(subtest.Test))
p.Passed = append(p.Passed, subtest)
}
}

// Remove test output once a test passes, it wont be used.
p.removeOutput(tc.ID)
// Remove subtest mapping, it is only used when a test fails.
Expand Down
27 changes: 27 additions & 0 deletions testjson/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,30 @@ func TestParseEvent(t *testing.T) {
cmpTestEvent := cmp.AllowUnexported(TestEvent{})
assert.DeepEqual(t, event, expected, cmpTestEvent)
}

func TestPassageCascades(t *testing.T) {
exec := newExecution()
exec.add(TestEvent{
Package: "mytestpkg",
Action: ActionRun,
Test: "TestFoo",
})
exec.add(TestEvent{
Package: "mytestpkg",
Action: ActionRun,
Test: "TestFoo/child",
})
exec.add(TestEvent{
Package: "mytestpkg",
Action: ActionPass,
Test: "TestFoo",
})

passed := exec.packages["mytestpkg"].Passed
assert.Equal(t, 2, len(passed))
if len(passed) != 2 {
return
}
assert.Equal(t, "TestFoo", string(passed[0].Test))
assert.Equal(t, "TestFoo/child", string(passed[1].Test))
}