sq/libsq/core/cleanup/cleanup_test.go
2023-11-19 18:06:36 -07:00

55 lines
928 B
Go

package cleanup_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/libsq/core/cleanup"
"github.com/neilotoole/sq/libsq/core/errz"
)
func TestCleanup(t *testing.T) {
want := []int{4, 3, 2, 1, 0}
var got []int
clnup := cleanup.New()
for i := 0; i < 5; i++ {
i := i
clnup.AddE(func() error {
got = append(got, i)
return nil
})
}
err := clnup.Run()
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestCleanup_Error(t *testing.T) {
clnup := cleanup.New()
clnup.AddE(func() error {
return nil
})
clnup.AddE(func() error {
return errz.New("err1")
})
clnup.AddE(func() error {
return errz.New("err2")
})
err := clnup.Run()
require.Error(t, err)
require.Equal(t, "err2; err1", err.Error())
errs := errz.Errors(err)
require.Equal(t, 2, len(errs))
require.Equal(t, "err2", errs[0].Error())
require.Equal(t, "err1", errs[1].Error())
}