Skip to content

Commit 804a4c0

Browse files
committed
test: make TestExit_Unwrap table-driven
Check each kind of Exit message (an error, an error chain and a non-error message) with errors.Unwrap, and with errors.Is and errors.As where they apply. Drop the errors.As check to ExitCoder, which also passes without Unwrap because Exit returns the ExitCoder itself. The error chain case checks errors.As to the wrapped *fs.PathError instead, which only passes when Unwrap is in place. Assisted-by: claude:claude-opus-5-5 Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
1 parent 6c85755 commit 804a4c0

1 file changed

Lines changed: 40 additions & 12 deletions

File tree

‎errors_test.go‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"io/fs"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -274,18 +275,45 @@ func TestHandleExitCoder_ExitCoderEmptyMessage(t *testing.T) {
274275
func TestExit_Unwrap(t *testing.T) {
275276
sentinel := errors.New("not found")
276277

277-
err := Exit(sentinel, 2)
278-
279278
// The error given to Exit must stay reachable through the standard
280279
// wrapping helpers (#1090).
281-
assert.ErrorIs(t, err, sentinel)
282-
assert.Equal(t, sentinel, errors.Unwrap(err))
283-
284-
var ec ExitCoder
285-
assert.True(t, errors.As(err, &ec))
286-
assert.Equal(t, 2, ec.ExitCode())
287-
288-
// A non-error message is wrapped into a fresh error and still unwraps.
289-
err = Exit("boom", 3)
290-
assert.EqualError(t, errors.Unwrap(err), "boom")
280+
tests := []struct {
281+
name string
282+
message any
283+
wantMsg string // message of errors.Unwrap(err)
284+
wantIs error // target for errors.Is, if any
285+
wantAs any // target for errors.As, if any
286+
}{
287+
{
288+
name: "error",
289+
message: sentinel,
290+
wantMsg: "not found",
291+
wantIs: sentinel,
292+
},
293+
{
294+
name: "error chain",
295+
message: &fs.PathError{Op: "open", Path: "x", Err: fs.ErrNotExist},
296+
wantMsg: "open x: file does not exist",
297+
wantIs: fs.ErrNotExist,
298+
wantAs: new(*fs.PathError),
299+
},
300+
{
301+
name: "non-error message",
302+
message: "boom",
303+
wantMsg: "boom",
304+
},
305+
}
306+
for _, tt := range tests {
307+
t.Run(tt.name, func(t *testing.T) {
308+
err := Exit(tt.message, 2)
309+
310+
assert.EqualError(t, errors.Unwrap(err), tt.wantMsg)
311+
if tt.wantIs != nil {
312+
assert.ErrorIs(t, err, tt.wantIs)
313+
}
314+
if tt.wantAs != nil {
315+
assert.ErrorAs(t, err, tt.wantAs)
316+
}
317+
})
318+
}
291319
}

0 commit comments

Comments
 (0)