Skip to content

Commit

Permalink
Fix FilterFailedUnique for prefix matched test names
Browse files Browse the repository at this point in the history
Add failing cases to the test
  • Loading branch information
dnephin committed Jul 16, 2023
1 parent 98a8f68 commit 62e0504
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
48 changes: 37 additions & 11 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ func (n TestName) Name() string {
return string(n)
}

func (n TestName) Parent() string {
idx := strings.LastIndex(string(n), "/")
if idx < 0 {
return ""
}
return string(n)[:idx]
}

func (p *Package) removeOutput(id int) {
delete(p.output, id)

Expand Down Expand Up @@ -538,23 +546,41 @@ func (e *Execution) Failed() []TestCase {
return failed
}

// FilterFailedUnique filters a slice of failed TestCases by removing root test
// case that have failed subtests.
// FilterFailedUnique filters a slice of failed TestCases to remove any parent
// tests that have failed subtests. The parent test will always be run when
// running any of its subtests.
func FilterFailedUnique(tcs []TestCase) []TestCase {
sort.Slice(tcs, func(i, j int) bool {
a, b := tcs[i], tcs[j]
if a.Package != b.Package {
return a.Package < b.Package
}
return len(a.Test.Name()) > len(b.Test.Name())
})

var result []TestCase
TestCaseLoop:
var parents = make(map[string]map[string]bool)
for _, tc := range tcs {
for _, ntc := range tcs {
if ntc == tc || ntc.Package != tc.Package || !ntc.Test.IsSubTest() {
continue
}
if strings.HasPrefix(string(ntc.Test), string(tc.Test)) {
// test name is a prefix of another test, so ignore
continue TestCaseLoop
}
if _, exists := parents[tc.Package]; !exists {
parents[tc.Package] = make(map[string]bool)
}
if parent := tc.Test.Parent(); parent != "" {
parents[tc.Package][parent] = true
}
if _, exists := parents[tc.Package][tc.Test.Name()]; exists {
continue // tc is a parent of a failing subtest
}
result = append(result, tc)
}

// Restore the original order of test cases
sort.Slice(result, func(i, j int) bool {
a, b := result[i], result[j]
if a.Package != b.Package {
return a.Package < b.Package
}
return a.ID < b.ID
})
return result
}

Expand Down
23 changes: 16 additions & 7 deletions testjson/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,24 @@ func TestFilterFailedUnique_MultipleNested(t *testing.T) {
{"Package": "pkg", "Test": "TestParent/TestNested", "Action": "run"}
{"Package": "pkg", "Test": "TestParent/TestNested/TestOne", "Action": "run"}
{"Package": "pkg", "Test": "TestParent/TestNested/TestOne", "Action": "fail"}
{"Package": "pkg", "Test": "TestParent/TestNested/TestOnePrefix", "Action": "run"}
{"Package": "pkg", "Test": "TestParent/TestNested/TestOnePrefix", "Action": "fail"}
{"Package": "pkg", "Test": "TestParent/TestNested", "Action": "fail"}
{"Package": "pkg", "Test": "TestParent", "Action": "fail"}
{"Package": "pkg", "Test": "TestTop", "Action": "run"}
{"Package": "pkg", "Test": "TestTop", "Action": "fail"}
{"Package": "pkg", "Test": "TestTop2", "Action": "run"}
{"Package": "pkg", "Test": "TestTop2", "Action": "fail"}
{"Package": "pkg", "Test": "TestTopPrefix", "Action": "run"}
{"Package": "pkg", "Test": "TestTopPrefix", "Action": "fail"}
{"Package": "pkg", "Action": "fail"}
{"Package": "pkg2", "Action": "run"}
{"Package": "pkg2", "Test": "TestParent", "Action": "run"}
{"Package": "pkg2", "Test": "TestParent/TestNested", "Action": "run"}
{"Package": "pkg2", "Test": "TestParent/TestNested", "Action": "fail"}
{"Package": "pkg2", "Test": "TestParent/TestNestedPrefix", "Action": "run"}
{"Package": "pkg2", "Test": "TestParent/TestNestedPrefix", "Action": "fail"}
{"Package": "pkg2", "Test": "TestParent", "Action": "fail"}
{"Package": "pkg2", "Test": "TestParentPrefix", "Action": "run"}
{"Package": "pkg2", "Test": "TestParentPrefix", "Action": "fail"}
{"Package": "pkg2", "Action": "fail"}`)

handler := &captureHandler{}
Expand All @@ -278,14 +284,17 @@ func TestFilterFailedUnique_MultipleNested(t *testing.T) {
}
exec, err := ScanTestOutput(cfg)
assert.NilError(t, err)
res := FilterFailedUnique(exec.Failed())
actual := FilterFailedUnique(exec.Failed())

exp := []TestCase{
expected := []TestCase{
{ID: 3, Package: "pkg", Test: TestName("TestParent/TestNested/TestOne")},
{ID: 4, Package: "pkg", Test: TestName("TestTop")},
{ID: 5, Package: "pkg", Test: TestName("TestTop2")},
{ID: 4, Package: "pkg", Test: TestName("TestParent/TestNested/TestOnePrefix")},
{ID: 5, Package: "pkg", Test: TestName("TestTop")},
{ID: 6, Package: "pkg", Test: TestName("TestTopPrefix")},
{ID: 2, Package: "pkg2", Test: TestName("TestParent/TestNested")},
{ID: 3, Package: "pkg2", Test: TestName("TestParent/TestNestedPrefix")},
{ID: 4, Package: "pkg2", Test: TestName("TestParentPrefix")},
}
cmpTestCase := cmp.AllowUnexported(TestCase{})
assert.DeepEqual(t, exp, res, cmpTestCase)
assert.DeepEqual(t, expected, actual, cmpTestCase)
}

0 comments on commit 62e0504

Please sign in to comment.