Skip to content

Commit

Permalink
Merge pull request #155 from dnephin/add-testname
Browse files Browse the repository at this point in the history
testjson: create TestName
  • Loading branch information
dnephin committed Oct 22, 2020
2 parents d52d240 + ca01c79 commit a94234d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 32 deletions.
12 changes: 6 additions & 6 deletions cmd/rerunfails.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func (r *failureRecorder) count() int {
return len(r.failures)
}

func goTestRunFlagForTestCase(name string) string {
root, sub := testjson.SplitTestName(name)
if sub == "" {
return "-test.run=^" + name + "$"
func goTestRunFlagForTestCase(test testjson.TestName) string {
if test.IsSubTest() {
root, sub := test.Split()
return "-test.run=^" + root + "$/^" + sub + "$"
}
return "-test.run=^" + root + "$/^" + sub + "$"
return "-test.run=^" + test.Name() + "$"
}

func writeRerunFailsReport(opts *options, exec *testjson.Execution) error {
Expand All @@ -144,7 +144,7 @@ func writeRerunFailsReport(opts *options, exec *testjson.Execution) error {
names := []string{}
results := map[string]testCaseCounts{}
for _, failure := range exec.Failed() {
name := failure.Package + "." + failure.Test
name := failure.Package + "." + failure.Test.Name()
if _, ok := results[name]; ok {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rerunfails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGoTestRunFlagFromTestCases(t *testing.T) {
expected string
}
fn := func(t *testing.T, tc testCase) {
actual := goTestRunFlagForTestCase(tc.input)
actual := goTestRunFlagForTestCase(testjson.TestName(tc.input))
assert.Equal(t, actual, tc.expected)
}

Expand Down
8 changes: 2 additions & 6 deletions cmd/tool/slowest/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,18 @@ func testNamesByPkgName(tcs []testjson.TestCase) ([]string, map[string]set) {
var pkgs []string
index := make(map[string]set)
for _, tc := range tcs {
if isSubTest(tc.Test) {
if tc.Test.IsSubTest() {
continue
}
if len(index[tc.Package]) == 0 {
pkgs = append(pkgs, tc.Package)
index[tc.Package] = make(map[string]struct{})
}
index[tc.Package][tc.Test] = struct{}{}
index[tc.Package][tc.Test.Name()] = struct{}{}
}
return pkgs, index
}

func isSubTest(name string) bool {
return strings.Contains(name, "/")
}

func errPkgLoad(pkg *packages.Package) error {
buf := new(strings.Builder)
for _, err := range pkg.Errors {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tool/slowest/slowest.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func aggregateTestCases(cases []testjson.TestCase) []testjson.TestCase {
}
pkg := cases[0].Package
// nolint: prealloc // size is not predictable
m := make(map[string][]time.Duration)
m := make(map[testjson.TestName][]time.Duration)
for _, tc := range cases {
m[tc.Test] = append(m[tc.Test], tc.Elapsed)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/tool/slowest/slowest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestAggregateTestCases(t *testing.T) {
}
assert.DeepEqual(t, actual, expected,
cmpopts.SortSlices(func(x, y testjson.TestCase) bool {
return strings.Compare(x.Test, y.Test) == -1
return strings.Compare(x.Test.Name(), y.Test.Name()) == -1
}),
cmpopts.IgnoreUnexported(testjson.TestCase{}))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/junitxml/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func packageTestCases(pkg *testjson.Package, formatClassname FormatFunc) []JUnit
func newJUnitTestCase(tc testjson.TestCase, formatClassname FormatFunc) JUnitTestCase {
return JUnitTestCase{
Classname: formatClassname(tc.Package),
Name: tc.Test,
Name: tc.Test.Name(),
Time: formatDurationAsSeconds(tc.Elapsed),
}
}
Expand Down
42 changes: 27 additions & 15 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (p *Package) TestCases() []TestCase {
// provides a little more safety if that ever changes.
func (p *Package) LastFailedByName(name string) TestCase {
for i := len(p.Failed) - 1; i >= 0; i-- {
if p.Failed[i].Test == name {
if p.Failed[i].Test.Name() == name {
return p.Failed[i]
}
}
Expand All @@ -143,13 +143,12 @@ func (p *Package) Output(id int) string {
// then all output for every subtest under the root test is returned.
// See https://github.com/golang/go/issues/29755.
func (p *Package) OutputLines(tc TestCase) []string {
_, sub := SplitTestName(tc.Test)
lines := p.output[tc.ID]

// If this is a subtest, or a root test case with subtest failures the
// subtest failure output should contain the relevant lines, so we don't need
// extra lines.
if sub != "" || tc.hasSubTestFailed {
if tc.Test.IsSubTest() || tc.hasSubTestFailed {
return lines
}

Expand All @@ -166,15 +165,26 @@ func (p *Package) addOutput(id int, output string) {
p.output[id] = append(p.output[id], output)
}

// SplitTestName into root test name and any subtest names.
func SplitTestName(name string) (root, sub string) {
parts := strings.SplitN(name, "/", 2)
type TestName string

func (n TestName) Split() (root string, sub string) {
parts := strings.SplitN(string(n), "/", 2)
if len(parts) < 2 {
return name, ""
return string(n), ""
}
return parts[0], parts[1]
}

// IsSubTest returns true if the name indicates the test is a subtest run using
// t.Run().
func (n TestName) IsSubTest() bool {
return strings.Contains(string(n), "/")
}

func (n TestName) Name() string {
return string(n)
}

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

Expand Down Expand Up @@ -216,7 +226,7 @@ func (p *Package) end() []TestEvent {
result = append(result, TestEvent{
Action: ActionFail,
Package: tc.Package,
Test: tc.Test,
Test: tc.Test.Name(),
Elapsed: float64(neverFinished),
})
delete(p.running, k)
Expand All @@ -232,7 +242,7 @@ type TestCase struct {
// test case.
ID int
Package string
Test string
Test TestName
Elapsed time.Duration
// RunID from the ScanConfig which produced this test case.
RunID int
Expand Down Expand Up @@ -287,8 +297,7 @@ func (e *Execution) addPackageEvent(pkg *Package, event TestEvent) {
}

func (p *Package) addTestEvent(event TestEvent) {
tc := p.running[event.Test]
root, subTest := SplitTestName(event.Test)
root, _ := TestName(event.Test).Split()

switch event.Action {
case ActionRun:
Expand All @@ -297,24 +306,27 @@ func (p *Package) addTestEvent(event TestEvent) {
p.Total++
tc := TestCase{
Package: event.Package,
Test: event.Test,
Test: TestName(event.Test),
ID: p.Total,
RunID: event.RunID,
}
p.running[event.Test] = tc

if subTest != "" {
if tc.Test.IsSubTest() {
rootID := p.running[root].ID
p.subTests[rootID] = append(p.subTests[rootID], tc.ID)
}
return
case ActionOutput, ActionBench:
tc := p.running[event.Test]
p.addOutput(tc.ID, event.Output)
return
case ActionPause, ActionCont:
return
}

// the event.Action must be one of the three test end events
tc := p.running[event.Test]
delete(p.running, event.Test)
tc.Elapsed = elapsedDuration(event.Elapsed)

Expand All @@ -323,7 +335,7 @@ func (p *Package) addTestEvent(event TestEvent) {
p.Failed = append(p.Failed, tc)

// If this is a subtest, mark the root test as having a failed subtest
if subTest != "" {
if tc.Test.IsSubTest() {
rootTestCase := p.running[root]
rootTestCase.hasSubTestFailed = true
p.running[root] = rootTestCase
Expand All @@ -337,7 +349,7 @@ func (p *Package) addTestEvent(event TestEvent) {
// Do not immediately remove output for subtests, to work around a bug
// in 'go test' where output is attributed to the wrong sub test.
// github.com/golang/go/issues/29755.
if subTest != "" {
if tc.Test.IsSubTest() {
return
}

Expand Down
2 changes: 1 addition & 1 deletion testjson/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func writeTestCaseSummary(out io.Writer, execution executionSummary, conf testCa
formatRunID(tc.RunID),
FormatDurationAsSeconds(tc.Elapsed, 2))
for _, line := range execution.OutputLines(tc) {
if isFramingLine(line) || conf.filter(tc.Test, line) {
if isFramingLine(line) || conf.filter(tc.Test.Name(), line) {
continue
}
fmt.Fprint(out, line)
Expand Down

0 comments on commit a94234d

Please sign in to comment.