mirror of
https://github.com/containers/podman.git
synced 2025-05-17 06:59:07 +08:00

Many dependencies started using go 1.22 which means we have to follow in order to update. Disable the now depracted exportloopref linter as it was replaced by copyloopvar as go fixed the loop copy problem in 1.22[1] Another new chnage in go 1.22 is the for loop syntax over ints, the intrange linter chacks for this but there a lot of loops that have to be converted so I didn't do it here and disable th elinter for now, th eold syntax is still fine. [1] https://go.dev/blog/loopvar-preview Signed-off-by: Paul Holzinger <pholzing@redhat.com>
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package errorhandling
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCause(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
err func() error
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "nil error",
|
|
err: func() error { return nil },
|
|
expectedErr: nil,
|
|
},
|
|
{
|
|
name: "equal errors",
|
|
err: func() error { return errors.New("foo") },
|
|
expectedErr: errors.New("foo"),
|
|
},
|
|
{
|
|
name: "wrapped error",
|
|
err: func() error { return fmt.Errorf("baz: %w", fmt.Errorf("bar: %w", errors.New("foo"))) },
|
|
expectedErr: errors.New("foo"),
|
|
},
|
|
{
|
|
name: "max depth reached",
|
|
err: func() error {
|
|
err := errors.New("error")
|
|
for i := 0; i <= 101; i++ {
|
|
err = fmt.Errorf("%d: %w", i, err)
|
|
}
|
|
return err
|
|
},
|
|
expectedErr: fmt.Errorf("0: %w", errors.New("error")),
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
err := Cause(tc.err())
|
|
assert.Equal(t, tc.expectedErr, err)
|
|
})
|
|
}
|
|
}
|