summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Paul Duffin <paulduffin@google.com> 2021-03-06 08:45:54 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2021-03-06 08:45:54 +0000
commit059f7f1ff3a70c2d7d823a1aee5696b4ce3ef4dc (patch)
treeac346abe20e0fd74e114ce80b7d1ccbbe6f3f9e3
parent87d7dd918a7d8ffb0530c6e5c9803aec55eac69d (diff)
parentea8a38634653713f6fb4d6a78a2c589cebd3f33a (diff)
Merge "Make error handlers treat any failures as fatal"
-rw-r--r--android/fixture.go13
-rw-r--r--android/testing.go11
2 files changed, 20 insertions, 4 deletions
diff --git a/android/fixture.go b/android/fixture.go
index aaf8d46a2..110e27169 100644
--- a/android/fixture.go
+++ b/android/fixture.go
@@ -396,6 +396,9 @@ func (h simpleErrorHandler) CheckErrors(result *TestResult, errs []error) {
// The default fixture error handler.
//
// Will fail the test immediately if any errors are reported.
+//
+// If the test fails this handler will call `result.FailNow()` which will exit the goroutine within
+// which the test is being run which means that the RunTest() method will not return.
var FixtureExpectsNoErrors = FixtureCustomErrorHandler(
func(result *TestResult, errs []error) {
FailIfErrored(result.T, errs)
@@ -411,9 +414,14 @@ var FixtureExpectsNoErrors = FixtureCustomErrorHandler(
//
// The test will not fail if:
// * Multiple errors are reported that do not match the pattern as long as one does match.
+//
+// If the test fails this handler will call `result.FailNow()` which will exit the goroutine within
+// which the test is being run which means that the RunTest() method will not return.
func FixtureExpectsAtLeastOneErrorMatchingPattern(pattern string) FixtureErrorHandler {
return FixtureCustomErrorHandler(func(result *TestResult, errs []error) {
- FailIfNoMatchingErrors(result.T, pattern, errs)
+ if !FailIfNoMatchingErrors(result.T, pattern, errs) {
+ result.FailNow()
+ }
})
}
@@ -427,6 +435,9 @@ func FixtureExpectsAtLeastOneErrorMatchingPattern(pattern string) FixtureErrorHa
//
// The test will not fail if:
// * One or more of the patterns does not match an error.
+//
+// If the test fails this handler will call `result.FailNow()` which will exit the goroutine within
+// which the test is being run which means that the RunTest() method will not return.
func FixtureExpectsAllErrorsToMatchAPattern(patterns []string) FixtureErrorHandler {
return FixtureCustomErrorHandler(func(result *TestResult, errs []error) {
CheckErrorsAgainstExpectations(result.T, errs, patterns)
diff --git a/android/testing.go b/android/testing.go
index 691ebe1cd..d8c123bba 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -456,12 +456,15 @@ func FailIfErrored(t *testing.T, errs []error) {
}
}
-func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
+// Fail if no errors that matched the regular expression were found.
+//
+// Returns true if a matching error was found, false otherwise.
+func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) bool {
t.Helper()
matcher, err := regexp.Compile(pattern)
if err != nil {
- t.Errorf("failed to compile regular expression %q because %s", pattern, err)
+ t.Fatalf("failed to compile regular expression %q because %s", pattern, err)
}
found := false
@@ -477,6 +480,8 @@ func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
t.Errorf("errs[%d] = %q", i, err)
}
}
+
+ return found
}
func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
@@ -497,9 +502,9 @@ func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPat
for i, err := range errs {
t.Errorf("errs[%d] = %s", i, err)
}
+ t.FailNow()
}
}
-
}
func SetKatiEnabledForTests(config Config) {