Skip to content

Commit c51b4cd

Browse files
committed
Better regex matching for panics
1 parent c34c00b commit c51b4cd

File tree

2 files changed

+153
-12
lines changed

2 files changed

+153
-12
lines changed

tools/flakeguard/runner/runner.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -598,22 +598,25 @@ func (r *Runner) transformTestOutputFiles(filePaths []string) error {
598598

599599
// attributePanicToTest properly attributes panics to the test that caused them.
600600
func attributePanicToTest(outputs []string) (test string, timeout bool, err error) {
601-
// Regex to extract a valid test function name.
602-
testNameRe := regexp.MustCompile(`(?:.*\.)?(Test[A-Z]\w+)(?:\.[^(]+)?\s*\(`)
603-
// Regex to detect timeout messages (accepting "timeout", "timedout", or "timed out", case-insensitive).
604-
timeoutRe := regexp.MustCompile(`(?i)(timeout|timedout|timed\s*out)`)
601+
// Regex to extract a valid test function name from a panic message, e.g.
602+
// github.com/smartcontractkit/chainlink/deployment/keystone/changeset_test.TestDeployBalanceReader(0xc000583c00)
603+
nestedTestNameRe := regexp.MustCompile(`\.(Test[A-Z]\w+)(?:\.[^(]+)?\s*\(`)
604+
// Regex to extract a valid test function name from a panic message if the panic is a timeout, e.g.
605+
// TestTimedOut (10m0s)
606+
timedOutTestNamRe := regexp.MustCompile(`^(Test\w+)\W+\(.*\)$`)
607+
605608
for _, o := range outputs {
606609
outputs = append(outputs, o)
607-
if matches := testNameRe.FindStringSubmatch(o); len(matches) > 1 {
608-
testName := strings.TrimSpace(matches[1])
609-
if timeoutRe.MatchString(o) {
610-
return testName, true, nil
611-
}
612-
return testName, false, nil
610+
matchNestedTestName := nestedTestNameRe.FindStringSubmatch(o)
611+
matchTimedOutTestName := timedOutTestNamRe.FindStringSubmatch(o)
612+
if len(matchNestedTestName) > 1 {
613+
return strings.TrimSpace(matchNestedTestName[1]), false, nil
614+
} else if len(matchTimedOutTestName) > 1 {
615+
return strings.TrimSpace(matchTimedOutTestName[1]), true, nil
613616
}
614617
}
615-
return "", false, fmt.Errorf("failed to attribute panic to test, using regex '%s' on these strings:\n\n%s",
616-
testNameRe.String(), strings.Join(outputs, ""))
618+
return "", false, fmt.Errorf("failed to attribute panic to test, using regex '%s' and '%s' on these strings:\n\n%s",
619+
nestedTestNameRe.String(), timedOutTestNamRe.String(), strings.Join(outputs, ""))
617620
}
618621

619622
// attributeRaceToTest properly attributes races to the test that caused them.

0 commit comments

Comments
 (0)