Make error handlers treat any failures as fatal
A lot of existing tests that expect errors to be reported do not check the returned result. This change treats failures of the error handler in that situation as fatal. The default error handler already behaved that way. The others did not. FixtureExpectsAllErrorsToMatchAPattern uses CheckErrorsAgainstExpectations and it was safe to just make that func treat any unmatching errors as fatal as all the existing usages are at the end of test functions. FixtureExpectsAtLeastOneErrorMatchingPattern uses the FailIfNoMatchingErrors function which is used in a number of places (including CheckErrorsAgainstExpectations) that do not want to treat a test failure as fatal. So, that was modified to return false if no matching error was found and the error handler treated that as fatal. Bug: 181070625 Test: m nothing Change-Id: I6e4df53f93250348bc050d4ff098134e6314ae30
This commit is contained in:
@@ -396,6 +396,9 @@ func (h simpleErrorHandler) CheckErrors(result *TestResult, errs []error) {
|
|||||||
// The default fixture error handler.
|
// The default fixture error handler.
|
||||||
//
|
//
|
||||||
// Will fail the test immediately if any errors are reported.
|
// 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(
|
var FixtureExpectsNoErrors = FixtureCustomErrorHandler(
|
||||||
func(result *TestResult, errs []error) {
|
func(result *TestResult, errs []error) {
|
||||||
FailIfErrored(result.T, errs)
|
FailIfErrored(result.T, errs)
|
||||||
@@ -411,9 +414,14 @@ var FixtureExpectsNoErrors = FixtureCustomErrorHandler(
|
|||||||
//
|
//
|
||||||
// The test will not fail if:
|
// The test will not fail if:
|
||||||
// * Multiple errors are reported that do not match the pattern as long as one does match.
|
// * 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 {
|
func FixtureExpectsAtLeastOneErrorMatchingPattern(pattern string) FixtureErrorHandler {
|
||||||
return FixtureCustomErrorHandler(func(result *TestResult, errs []error) {
|
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:
|
// The test will not fail if:
|
||||||
// * One or more of the patterns does not match an error.
|
// * 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 {
|
func FixtureExpectsAllErrorsToMatchAPattern(patterns []string) FixtureErrorHandler {
|
||||||
return FixtureCustomErrorHandler(func(result *TestResult, errs []error) {
|
return FixtureCustomErrorHandler(func(result *TestResult, errs []error) {
|
||||||
CheckErrorsAgainstExpectations(result.T, errs, patterns)
|
CheckErrorsAgainstExpectations(result.T, errs, patterns)
|
||||||
|
@@ -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()
|
t.Helper()
|
||||||
|
|
||||||
matcher, err := regexp.Compile(pattern)
|
matcher, err := regexp.Compile(pattern)
|
||||||
if err != nil {
|
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
|
found := false
|
||||||
@@ -477,6 +480,8 @@ func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
|
|||||||
t.Errorf("errs[%d] = %q", i, err)
|
t.Errorf("errs[%d] = %q", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
|
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 {
|
for i, err := range errs {
|
||||||
t.Errorf("errs[%d] = %s", i, err)
|
t.Errorf("errs[%d] = %s", i, err)
|
||||||
}
|
}
|
||||||
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetKatiEnabledForTests(config Config) {
|
func SetKatiEnabledForTests(config Config) {
|
||||||
|
Reference in New Issue
Block a user