Avoid SetErrorHandler mutating FixtureFactory

A FixtureFactory is supposed to be immutable to allow them to be safely
shared but unfortunately the implementation of SetErrorHandler broke
that constraint. That made it very easy to mistakenly add an error
handler specific to a test to a shared factory breaking other tests
that use that factory.

This change causes SetErrorHandler to create a new instance of the
factory to avoid that.

Bug: 181070625
Test: m nothing
Change-Id: Ia5356a04189099c88880a2a521af29ab72560f30
This commit is contained in:
Paul Duffin 2021-03-04 19:15:47 +00:00
parent d6ceb8600c
commit 52323b5113
1 changed files with 6 additions and 3 deletions

View File

@ -182,7 +182,8 @@ type FixtureFactory interface {
// Create a Fixture.
Fixture(t *testing.T, preparers ...FixturePreparer) Fixture
// Set the error handler that will be used to check any errors reported by the test.
// SetErrorHandler creates a new FixtureFactory that will use the supplied error handler to check
// the errors (may be 0) reported by the test.
//
// The default handlers is FixtureExpectsNoErrors which will fail the go test immediately if any
// errors are reported.
@ -578,8 +579,10 @@ func (f *fixtureFactory) Fixture(t *testing.T, preparers ...FixturePreparer) Fix
}
func (f *fixtureFactory) SetErrorHandler(errorHandler FixtureErrorHandler) FixtureFactory {
f.errorHandler = errorHandler
return f
newFactory := &fixtureFactory{}
*newFactory = *f
newFactory.errorHandler = errorHandler
return newFactory
}
func (f *fixtureFactory) RunTest(t *testing.T, preparers ...FixturePreparer) *TestResult {