-
Notifications
You must be signed in to change notification settings - Fork 207
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
Swap to go-junit-report for parsing go test output #982
Merged
peterebden
merged 4 commits into
thought-machine:master
from
bli-0:go-junit-report-for-go-test-parsing
Jun 4, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,68 @@ | ||
// Parser for output from Go's testing package. | ||
// | ||
// This is a fairly straightforward microformat so pretty easy to parse ourselves. | ||
// There's at least one package out there to convert it to JUnit XML but not worth | ||
// the complexity of getting that installed as a standalone tool. | ||
|
||
package test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
parser "github.com/jstemmer/go-junit-report/parser" | ||
|
||
"github.com/thought-machine/please/src/core" | ||
) | ||
|
||
// Not sure what the -6 suffixes are about. | ||
var testStart = regexp.MustCompile(`^=== RUN (.*)(?:-6)?$`) | ||
var testResult = regexp.MustCompile(`^ *--- (PASS|FAIL|SKIP): (.*)(?:-6)? \(([0-9]+\.[0-9]+)s\)$`) | ||
|
||
func parseGoTestResults(data []byte) (core.TestSuite, error) { | ||
|
||
testPackage, err := parser.Parse(bytes.NewReader(data), "") | ||
|
||
if err != nil { | ||
return core.TestSuite{}, fmt.Errorf("Failed to parse go test output: %w", err) | ||
} | ||
|
||
results := fromGoJunitReport(testPackage) | ||
|
||
return results, nil | ||
} | ||
|
||
// Conversion between a go-junit-report Report into a core.TestSuite. | ||
// A Package is mapped to TestSuite & Tests mapped onto testCases. | ||
func fromGoJunitReport(report *parser.Report) core.TestSuite { | ||
results := core.TestSuite{} | ||
lines := bytes.Split(data, []byte{'\n'}) | ||
testsStarted := map[string]bool{} | ||
var suiteDuration time.Duration | ||
testOutput := make([]string, 0) | ||
for i, line := range lines { | ||
testStartMatches := testStart.FindSubmatch(line) | ||
testResultMatches := testResult.FindSubmatch(line) | ||
if testStartMatches != nil { | ||
testsStarted[strings.TrimSpace(string(testStartMatches[1]))] = true | ||
} else if testResultMatches != nil { | ||
testName := strings.TrimSpace(string(testResultMatches[2])) | ||
if !testsStarted[testName] { | ||
continue | ||
} | ||
f, _ := strconv.ParseFloat(string(testResultMatches[3]), 64) | ||
duration := time.Duration(f * float64(time.Second)) | ||
suiteDuration += duration | ||
testCase := core.TestCase{ | ||
Name: testName, | ||
} | ||
if bytes.Equal(testResultMatches[1], []byte("PASS")) { | ||
testCase.Executions = append(testCase.Executions, core.TestExecution{ | ||
Duration: &duration, | ||
Stderr: strings.Join(testOutput, ""), | ||
}) | ||
} else if bytes.Equal(testResultMatches[1], []byte("SKIP")) { | ||
// The skip message is found at the bottom of the test output segment. | ||
// Prior to Go 1.14 the test output segment follows the results line. | ||
// In Go 1.14 the test output segment sits between the start line and the results line. | ||
outputLines := getTestOutputLines(i, lines) | ||
skipMessage := "" | ||
if len(outputLines) > 0 { | ||
skipMessage = strings.TrimSpace(outputLines[len(outputLines)-1]) | ||
} | ||
|
||
testCase.Executions = append(testCase.Executions, core.TestExecution{ | ||
for _, pkg := range report.Packages { | ||
for _, test := range pkg.Tests { | ||
coreTestCase := core.TestCase{Name: test.Name} | ||
testOutput := strings.Join(test.Output, "\n") | ||
|
||
if test.Result == parser.PASS { | ||
coreTestCase.Executions = append(coreTestCase.Executions, core.TestExecution{ | ||
Stderr: testOutput, | ||
Duration: &test.Duration, | ||
}) | ||
} else if test.Result == parser.SKIP { | ||
coreTestCase.Executions = append(coreTestCase.Executions, core.TestExecution{ | ||
Skip: &core.TestResultSkip{ | ||
Message: skipMessage, | ||
// Given the possibility of test setup, teardowns & custom logging, we can't do anything | ||
// more targeted than using the whole test output as the skip message. | ||
Message: testOutput, | ||
}, | ||
Stderr: strings.Join(testOutput, ""), | ||
Duration: &duration, | ||
Stderr: testOutput, | ||
Duration: &test.Duration, | ||
}) | ||
} else { | ||
|
||
outputLines := getTestOutputLines(i, lines) | ||
|
||
output := strings.Join(outputLines, "\n") | ||
testCase.Executions = append(testCase.Executions, core.TestExecution{ | ||
// A "FAIL" result | ||
coreTestCase.Executions = append(coreTestCase.Executions, core.TestExecution{ | ||
Failure: &core.TestResultFailure{ | ||
Traceback: output, | ||
Traceback: testOutput, | ||
}, | ||
Stderr: strings.Join(testOutput, ""), | ||
Duration: &duration, | ||
Stderr: testOutput, | ||
Duration: &test.Duration, | ||
}) | ||
} | ||
results.TestCases = append(results.TestCases, testCase) | ||
testOutput = make([]string, 0) | ||
} else if bytes.Equal(line, []byte("PASS")) { | ||
// Do nothing, all's well. | ||
} else if bytes.Equal(line, []byte("FAIL")) { | ||
if results.Failures() == 0 { | ||
return results, fmt.Errorf("Test indicated final failure but no failures found yet") | ||
} | ||
} else { | ||
testOutput = append(testOutput, string(line), "\n") | ||
results.TestCases = append(results.TestCases, coreTestCase) | ||
} | ||
results.Duration += pkg.Duration | ||
} | ||
results.Duration = suiteDuration | ||
return results, nil | ||
} | ||
|
||
func getTestOutputLines(currentIndex int, lines [][]byte) []string { | ||
if resultLooksPriorGo114(currentIndex, lines) { | ||
return getPostResultOutput(currentIndex, lines) | ||
} | ||
return getPreResultOutput(currentIndex, lines) | ||
} | ||
|
||
// Go test output looks prior to 114 if the previous line matches against a start test block. | ||
// Only fully applicable for failing and skipped tests as a message may not | ||
// appear for passed tests. | ||
func resultLooksPriorGo114(currentIndex int, lines [][]byte) bool { | ||
if currentIndex == 0 { | ||
return false | ||
} | ||
|
||
prevLine := lines[currentIndex-1] | ||
prevLineMatchesStart := testStart.FindSubmatch(prevLine) | ||
|
||
return prevLineMatchesStart != nil | ||
} | ||
|
||
// Get the output for Go test output prior to Go 1.14 | ||
func getPostResultOutput(resultsIndex int, lines [][]byte) []string { | ||
output := []string{} | ||
for j := resultsIndex + 1; j < len(lines) && !lineMatchesRunOrResultsLine(lines[j]); j++ { | ||
output = append(output, string(lines[j])) | ||
} | ||
|
||
return output | ||
} | ||
|
||
// Get output for Go tests output after Go 1.14 | ||
func getPreResultOutput(resultsIndex int, lines [][]byte) []string { | ||
output := []string{} | ||
for j := resultsIndex - 1; j > 0 && !lineMatchesRunOrResultsLine(lines[j]); j-- { | ||
output = append([]string{string(lines[j])}, output...) | ||
} | ||
return output | ||
} | ||
|
||
func lineMatchesRunOrResultsLine(line []byte) bool { | ||
|
||
testStartMatches := testStart.FindSubmatch(line) | ||
matchesRunLine := (testStartMatches != nil) | ||
|
||
return matchesRunLine || bytes.Equal(line, []byte("PASS")) || bytes.Equal(line, []byte("FAIL")) | ||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how often we expect this particular case to come up. Go Junit report doesn't error out in this scenario - but it does 'misattribute' a failure to a Test case, so I'm not sure it's 100% correct in that regard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That dates back to literally before the dawn of time (initial commit). I don't think I've ever seen it actually happen though; not terribly bothered about losing this one case if everything else is still passing.