|
4 | 4 | "bytes" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
| 7 | + "io/fs" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/stretchr/testify/assert" |
@@ -270,3 +271,49 @@ func TestHandleExitCoder_ExitCoderEmptyMessage(t *testing.T) { |
270 | 271 | assert.True(t, called) |
271 | 272 | assert.Empty(t, errBuf.String(), "expected no output to stderr for empty exit message") |
272 | 273 | } |
| 274 | + |
| 275 | +func TestExit_Unwrap(t *testing.T) { |
| 276 | + sentinel := errors.New("not found") |
| 277 | + |
| 278 | + // The error given to Exit must stay reachable through the standard |
| 279 | + // wrapping helpers (#1090). |
| 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 | + } |
| 319 | +} |
0 commit comments