From fac4d074d0f625daf2b99e074860cf87d2e3fcbf Mon Sep 17 00:00:00 2001 From: Andrew Keesler Date: Fri, 9 Oct 2020 08:00:33 -0400 Subject: [PATCH] internal/multierror: add tests Signed-off-by: Andrew Keesler --- internal/multierror/multierror.go | 34 +++++++++++++++++--------- internal/multierror/multierror_test.go | 24 ++++++++++++++++++ 2 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 internal/multierror/multierror_test.go diff --git a/internal/multierror/multierror.go b/internal/multierror/multierror.go index f9d9f2f8..c9eab6b9 100644 --- a/internal/multierror/multierror.go +++ b/internal/multierror/multierror.go @@ -1,6 +1,15 @@ // Copyright 2020 the Pinniped contributors. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +// Package multierror provides a type that can translate multiple errors into a Go error interface. +// +// A common use of this package is as follows. +// errs := multierror.New() +// for i := range stuff { +// err := doThing(i) +// errs.Add(err) +// } +// return errs.ErrOrNil() package multierror import ( @@ -8,31 +17,34 @@ import ( "strings" ) -type multiError []error +// MultiError holds a list of error's, that could potentially be empty. +// +// Use New() to create a MultiError. +type MultiError []error -func New() multiError { //nolint:golint // returning a private type for encapsulation purposes +// New returns an empty MultiError. +func New() MultiError { return make([]error, 0) } -func (m *multiError) Add(err error) { +// Add adds an error to the MultiError. The provided err must not be nil. +func (m *MultiError) Add(err error) { *m = append(*m, err) } -func (m multiError) len() int { - return len(m) -} - -func (m multiError) Error() string { +// Error implements the error.Error() interface method. +func (m MultiError) Error() string { sb := strings.Builder{} - _, _ = fmt.Fprintf(&sb, "%d errors:", m.len()) + _, _ = fmt.Fprintf(&sb, "%d error(s):", len(m)) for _, err := range m { _, _ = fmt.Fprintf(&sb, "\n- %s", err.Error()) } return sb.String() } -func (m multiError) ErrOrNil() error { - if m.len() > 0 { +// ErrOrNil returns either nil, if there are no errors in this MultiError, or an error, otherwise. +func (m MultiError) ErrOrNil() error { + if len(m) > 0 { return m } return nil diff --git a/internal/multierror/multierror_test.go b/internal/multierror/multierror_test.go new file mode 100644 index 00000000..cb96771e --- /dev/null +++ b/internal/multierror/multierror_test.go @@ -0,0 +1,24 @@ +// Copyright 2020 the Pinniped contributors. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package multierror + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestMultierror(t *testing.T) { + errs := New() + + require.Nil(t, errs.ErrOrNil()) + + errs.Add(errors.New("some error 1")) + require.EqualError(t, errs.ErrOrNil(), "1 error(s):\n- some error 1") + + errs.Add(errors.New("some error 2")) + errs.Add(errors.New("some error 3")) + require.EqualError(t, errs.ErrOrNil(), "3 error(s):\n- some error 1\n- some error 2\n- some error 3") +}