From bd236e2964d9854f56c15825f8b1531ac83423b7 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Sun, 3 Nov 2024 10:59:48 +0700 Subject: [PATCH] Testing: Add FixtureToDataHandlerWithErrors --- internal/testing/exchange/exchange.go | 29 +++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/testing/exchange/exchange.go b/internal/testing/exchange/exchange.go index fe82b4f5435..0ed0cddcb36 100644 --- a/internal/testing/exchange/exchange.go +++ b/internal/testing/exchange/exchange.go @@ -122,23 +122,44 @@ func MockWsInstance[T any, PT interface { return e } -// FixtureToDataHandler squirts the contents of a file to a reader function (probably e.wsHandleData) +type FixtureError struct { + Err error + Msg []byte +} + +// FixtureToDataHandler squirts the contents of a file to a reader function (probably e.wsHandleData) and asserts no errors are returned func FixtureToDataHandler(tb testing.TB, fixturePath string, reader func([]byte) error) { tb.Helper() + for _, e := range FixtureToDataHandlerWithErrors(tb, fixturePath, reader) { + assert.NoErrorf(tb, e.Err, "Should not error handling message:\n%s", e.Msg) + } +} + +// FixtureToDataHandlerWithErrors squirts the contents of a file to a reader function (probably e.wsHandleData) and returns handler errors +// Any errors setting up the fixture will fail tests +func FixtureToDataHandlerWithErrors(tb testing.TB, fixturePath string, reader func([]byte) error) []FixtureError { + tb.Helper() + fixture, err := os.Open(fixturePath) - assert.NoError(tb, err, "Opening fixture '%s' should not error", fixturePath) + require.NoError(tb, err, "Opening fixture '%s' should not error", fixturePath) defer func() { assert.NoError(tb, fixture.Close(), "Closing the fixture file should not error") }() + errs := []FixtureError{} s := bufio.NewScanner(fixture) for s.Scan() { msg := s.Bytes() - err := reader(msg) - assert.NoErrorf(tb, err, "Fixture message should not error:\n%s", msg) + if err := reader(msg); err != nil { + errs = append(errs, FixtureError{ + Err: err, + Msg: msg, + }) + } } assert.NoError(tb, s.Err(), "Fixture Scanner should not error") + return errs } var setupWsMutex sync.Mutex