-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
861 lines (721 loc) · 25.4 KB
/
Copy patherrors_test.go
File metadata and controls
861 lines (721 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
package core
// cspell:words Errno ENOENT
import (
"errors"
"fmt"
"io/fs"
"os"
"syscall"
"testing"
)
// Compile-time verification that test case types implement TestCase interface
var _ TestCase = quietWrapTestCase{}
var _ TestCase = coalesceErrorTestCase{}
var _ TestCase = isErrorTestCase{}
var _ TestCase = isErrorFnTestCase{}
var _ TestCase = isErrorFn2TestCase{}
var _ TestCase = checkErrorIsInTestCase{}
var _ TestCase = checkErrorIsIn2TestCase{}
var _ TestCase = temporaryErrorTestCase{}
var _ TestCase = checkIsTemporaryTestCase{}
var _ TestCase = isTemporaryTestCase{}
var _ TestCase = checkIsTimeoutTestCase{}
var _ TestCase = isTimeoutTestCase{}
var _ TestCase = wrappedErrorTestCase{}
var _ TestCase = unwrapBranchesTestCase{}
const emptyString = ""
// Test cases for IsErrorFn function
type isErrorFnTestCase struct {
checkFn func(error) bool
name string
errs []error
expected bool
}
func (tc isErrorFnTestCase) Name() string {
return tc.name
}
func (tc isErrorFnTestCase) Test(t *testing.T) {
t.Helper()
result := IsErrorFn(tc.checkFn, tc.errs...)
AssertEqual(t, tc.expected, result, "IsErrorFn result")
}
func newIsErrorFnTestCase(name string, checkFn func(error) bool, expected bool, errs ...error) isErrorFnTestCase {
return isErrorFnTestCase{
name: name,
checkFn: checkFn,
errs: errs,
expected: expected,
}
}
func TestIsErrorFn(t *testing.T) {
testErr := errors.New("test error")
differentErr := errors.New("different error")
wrappedErr := fmt.Errorf("wrapped: %w", testErr)
isTestErr := func(err error) bool {
return errors.Is(err, testErr)
}
testCases := []isErrorFnTestCase{
newIsErrorFnTestCase("matching error", isTestErr, true, testErr),
newIsErrorFnTestCase("non-matching error", isTestErr, false, differentErr),
newIsErrorFnTestCase("nil check function", nil, false, testErr),
newIsErrorFnTestCase("no errors", isTestErr, false),
newIsErrorFnTestCase("wrapped error", isTestErr, true, wrappedErr),
newIsErrorFnTestCase("nil error in slice", isTestErr, true, nil, testErr),
}
RunTestCases(t, testCases)
}
// Test cases for IsErrorFn2 function
type isErrorFn2TestCase struct {
checkFn func(error) (bool, bool)
name string
errs []error
expectedIs bool
expectedKnown bool
}
func (tc isErrorFn2TestCase) Name() string {
return tc.name
}
func (tc isErrorFn2TestCase) Test(t *testing.T) {
t.Helper()
is, known := IsErrorFn2(tc.checkFn, tc.errs...)
AssertEqual(t, tc.expectedIs, is, "IsErrorFn2 is result")
AssertEqual(t, tc.expectedKnown, known, "IsErrorFn2 known result")
}
func newIsErrorFn2TestCase(name string, checkFn func(error) (bool, bool),
expectedIs, expectedKnown bool, errs ...error) isErrorFn2TestCase {
return isErrorFn2TestCase{
name: name,
checkFn: checkFn,
errs: errs,
expectedIs: expectedIs,
expectedKnown: expectedKnown,
}
}
func TestIsErrorFn2(t *testing.T) {
testErr := errors.New("test error")
differentErr := errors.New("different error")
wrappedErr := fmt.Errorf("wrapped: %w", testErr)
isTestErr := func(err error) (bool, bool) {
if err == nil {
return false, false
}
return errors.Is(err, testErr), true
}
testCases := []isErrorFn2TestCase{
newIsErrorFn2TestCase("matching error", isTestErr, true, true, testErr),
newIsErrorFn2TestCase("non-matching error", isTestErr, false, true, differentErr),
newIsErrorFn2TestCase("nil check function", nil, false, true, testErr),
newIsErrorFn2TestCase("no errors", isTestErr, false, true),
newIsErrorFn2TestCase("wrapped error", isTestErr, true, true, wrappedErr),
newIsErrorFn2TestCase("unknown error type",
func(_ error) (bool, bool) { return false, false }, false, false, testErr),
}
RunTestCases(t, testCases)
}
// Test cases for QuietWrap function
type quietWrapTestCase struct {
err error
name string
format string
expected string
args []any
}
func newQuietWrapTestCase(name string, err error, format, expected string, args ...any) quietWrapTestCase {
return quietWrapTestCase{
name: name,
err: err,
format: format,
expected: expected,
args: args,
}
}
func (tc quietWrapTestCase) Name() string {
return tc.name
}
func (tc quietWrapTestCase) Test(t *testing.T) {
t.Helper()
result := QuietWrap(tc.err, tc.format, tc.args...)
if tc.expected == "" {
AssertEqual(t, tc.err, result, "QuietWrap original")
} else {
AssertEqual(t, tc.expected, result.Error(), "QuietWrap message")
// Test that it's wrappable
if wrapped, ok := result.(Unwrappable); ok {
AssertEqual(t, tc.err, wrapped.Unwrap(), "QuietWrap unwrap")
}
}
}
func TestQuietWrap(t *testing.T) {
baseErr := errors.New("base error")
testCases := S(
newQuietWrapTestCase("nil error", nil, "message", ""),
newQuietWrapTestCase("empty format", baseErr, "", ""),
newQuietWrapTestCase("simple message", baseErr, "wrapped", "wrapped"),
newQuietWrapTestCase("formatted message", baseErr, "wrapped: %s", "wrapped: test", "test"),
newQuietWrapTestCase("multiple args", baseErr, "error %d: %s", "error 42: test", 42, "test"),
newQuietWrapTestCase("nil error with format", nil, "message %s", "", "test"),
)
RunTestCases(t, testCases)
}
// Test cases for CoalesceError function
type coalesceErrorTestCase struct {
expected error
name string
errs []error
}
func newCoalesceErrorTestCase(name string, expected error, errs ...error) coalesceErrorTestCase {
return coalesceErrorTestCase{
name: name,
expected: expected,
errs: errs,
}
}
func (tc coalesceErrorTestCase) Name() string {
return tc.name
}
func (tc coalesceErrorTestCase) Test(t *testing.T) {
t.Helper()
result := CoalesceError(tc.errs...)
AssertEqual(t, tc.expected, result, "CoalesceError")
}
func TestCoalesceError(t *testing.T) {
err1 := errors.New("error 1")
err2 := errors.New("error 2")
err3 := errors.New("error 3")
testCases := S(
newCoalesceErrorTestCase("no errors", nil),
newCoalesceErrorTestCase("single nil error", nil, nil),
newCoalesceErrorTestCase("single non-nil error", err1, err1),
newCoalesceErrorTestCase("first non-nil wins", err1, nil, err1, err2),
newCoalesceErrorTestCase("first error wins", err1, err1, err2, err3),
newCoalesceErrorTestCase("all nil", nil, nil, nil, nil),
newCoalesceErrorTestCase("nil then non-nil", err2, nil, nil, err2),
newCoalesceErrorTestCase("empty slice", nil),
)
RunTestCases(t, testCases)
}
// errorWithIs matches a single target via its Is method instead of
// identity, exercising the Is(error) bool support in IsError.
type errorWithIs struct {
target error
}
func (*errorWithIs) Error() string { return "error with Is" }
func (e *errorWithIs) Is(target error) bool { return target == e.target }
// nonComparableError is a value-typed error carrying a slice, so its
// dynamic type does not support ==: comparing two of them panics
// rather than reporting false. It pins that IsError degrades the
// comparison to "no match" instead of panicking.
type nonComparableError struct {
codes []int
}
func (nonComparableError) Error() string { return "non-comparable error" }
// nonComparableErrorWithIs is non-comparable like nonComparableError,
// and its Is accepts anything: it pins that the Is(error) bool probe
// still runs after the identity comparison degrades, so a
// non-comparable error can match through its own Is method.
type nonComparableErrorWithIs struct {
codes []int
}
func (nonComparableErrorWithIs) Error() string {
return "non-comparable error with Is"
}
func (nonComparableErrorWithIs) Is(error) bool { return true }
// Test cases for IsError function
type isErrorTestCase struct {
name string
err error
errs []error
expected bool
}
func newIsErrorTestCase(name string, err error, expected bool, errs ...error) isErrorTestCase {
return isErrorTestCase{
name: name,
err: err,
errs: errs,
expected: expected,
}
}
func (tc isErrorTestCase) Name() string {
return tc.name
}
func (tc isErrorTestCase) Test(t *testing.T) {
t.Helper()
result := IsError(tc.err, tc.errs...)
AssertEqual(t, tc.expected, result, "IsError result")
}
func TestIsError(t *testing.T) {
err1 := errors.New("error 1")
err2 := errors.New("error 2")
err3 := errors.New("error 3")
wrappedErr := fmt.Errorf("wrapped: %w", err1)
errIs := &errorWithIs{target: err2}
pathErr := &fs.PathError{Op: "open", Path: "missing",
Err: syscall.ENOENT}
errNonComparable := nonComparableError{codes: []int{1}}
errNonComparableIs := nonComparableErrorWithIs{codes: []int{2}}
testCases := []isErrorTestCase{
newIsErrorTestCase("nil error", nil, false, err1),
newIsErrorTestCase("no target errors - non-nil", err1, true),
newIsErrorTestCase("no target errors - nil", nil, false),
newIsErrorTestCase("exact match", err1, true, err1, err2),
newIsErrorTestCase("no match", err3, false, err1, err2),
newIsErrorTestCase("wrapped error match", wrappedErr, true, err1),
newIsErrorTestCase("wrapped error no match", wrappedErr, false, err2),
newIsErrorTestCase("multiple targets", err2, true, err1, err2, err3),
newIsErrorTestCase("empty targets", err1, true),
newIsErrorTestCase("Is method match", errIs, true, err2),
newIsErrorTestCase("Is method mismatch", errIs, false, err3),
newIsErrorTestCase("wrapped Is method match",
fmt.Errorf("wrapped: %w", errIs), true, err2),
newIsErrorTestCase("target Is method not consulted",
err2, false, errIs),
newIsErrorTestCase("Errno match via Is method",
pathErr, true, fs.ErrNotExist),
newIsErrorTestCase("Is method self match", errIs, true, errIs),
newIsErrorTestCase("Is method match second target",
errIs, true, err3, err2),
newIsErrorTestCase("Errno self match",
syscall.ENOENT, true, syscall.ENOENT),
newIsErrorTestCase("non-comparable no match",
errNonComparable, false, errNonComparable),
newIsErrorTestCase("non-comparable Is method match",
errNonComparableIs, true, errNonComparableIs),
}
RunTestCases(t, testCases)
}
// Test cases for the NewCheckErrorIsIn per-node closure
type checkErrorIsInTestCase struct {
err error
name string
targets []error
want bool
}
func newCheckErrorIsInTestCase(name string, err error,
targets []error, want bool) checkErrorIsInTestCase {
return checkErrorIsInTestCase{
name: name,
err: err,
targets: targets,
want: want,
}
}
func (tc checkErrorIsInTestCase) Name() string { return tc.name }
func (tc checkErrorIsInTestCase) Test(t *testing.T) {
t.Helper()
check := NewCheckErrorIsIn(tc.targets)
AssertEqual(t, tc.want, check(tc.err), "check result")
}
func checkErrorIsInTestCases() []checkErrorIsInTestCase {
err1 := errors.New("error 1")
err2 := errors.New("error 2")
errIs := &errorWithIs{target: err2}
return []checkErrorIsInTestCase{
newCheckErrorIsInTestCase("identity match",
err1, []error{err1}, true),
newCheckErrorIsInTestCase("identity mismatch",
err1, []error{err2}, false),
newCheckErrorIsInTestCase("Is method match second target",
errIs, []error{err1, err2}, true),
newCheckErrorIsInTestCase("nil target never matches",
err1, []error{nil}, false),
newCheckErrorIsInTestCase("no targets",
err1, nil, false),
newCheckErrorIsInTestCase("no unwrapping",
fmt.Errorf("wrapped: %w", err1), []error{err1}, false),
}
}
// TestNewCheckErrorIsIn exercises the per-node closure directly:
// matching is identity-or-Is per node, without unwrapping — traversal
// belongs to IsErrorFn.
func TestNewCheckErrorIsIn(t *testing.T) {
RunTestCases(t, checkErrorIsInTestCases())
}
// Test cases for the NewCheckErrorIsIn2 per-node closure
type checkErrorIsIn2TestCase struct {
err error
name string
targets []error
wantIs bool
wantKnown bool
}
func newCheckErrorIsIn2TestCase(name string, err error,
targets []error, wantIs, wantKnown bool) checkErrorIsIn2TestCase {
return checkErrorIsIn2TestCase{
name: name,
err: err,
targets: targets,
wantIs: wantIs,
wantKnown: wantKnown,
}
}
func (tc checkErrorIsIn2TestCase) Name() string { return tc.name }
func (tc checkErrorIsIn2TestCase) Test(t *testing.T) {
t.Helper()
is, known := NewCheckErrorIsIn2(tc.targets)(tc.err)
AssertEqual(t, tc.wantIs, is, "is")
AssertEqual(t, tc.wantKnown, known, "known")
}
func checkErrorIsIn2TestCases() []checkErrorIsIn2TestCase {
err1 := errors.New("error 1")
err2 := errors.New("error 2")
errIs := &errorWithIs{target: err2}
return []checkErrorIsIn2TestCase{
newCheckErrorIsIn2TestCase("identity match",
err1, []error{err1}, true, true),
newCheckErrorIsIn2TestCase("miss is not final",
err1, []error{err2}, false, false),
newCheckErrorIsIn2TestCase("Is method match second target",
errIs, []error{err1, err2}, true, true),
newCheckErrorIsIn2TestCase("Is method miss not final",
errIs, []error{err1}, false, false),
newCheckErrorIsIn2TestCase("nil target final miss",
err1, []error{nil}, false, true),
newCheckErrorIsIn2TestCase("nil beside real target",
err1, []error{nil, err2}, false, false),
newCheckErrorIsIn2TestCase("no targets final miss",
err1, nil, false, true),
newCheckErrorIsIn2TestCase("no unwrapping",
fmt.Errorf("wrapped: %w", err1), []error{err1},
false, false),
}
}
// TestNewCheckErrorIsIn2 exercises the two-result per-node closure:
// a match is final, a miss is final only when no target could match
// deeper nodes either.
func TestNewCheckErrorIsIn2(t *testing.T) {
RunTestCases(t, checkErrorIsIn2TestCases())
}
// TestNewCheckErrorIsIn2Compose pins composition with IsErrorFn2: a
// per-node miss is not final, so traversal descends into wrapped
// errors and still finds the match.
func TestNewCheckErrorIsIn2Compose(t *testing.T) {
target := errors.New("target")
check := NewCheckErrorIsIn2([]error{target})
is, known := IsErrorFn2(check, fmt.Errorf("wrapped: %w", target))
AssertTrue(t, is, "is")
AssertTrue(t, known, "known")
}
// Test cases for TemporaryError constructors and methods
type temporaryErrorTestCase struct {
err error
testFunc func(error) error
name string
expected string
isTimeout bool
}
func newTemporaryErrorTestCase(name string, err error, expected string, isTimeout bool,
testFunc func(error) error) temporaryErrorTestCase {
return temporaryErrorTestCase{
name: name,
err: err,
expected: expected,
isTimeout: isTimeout,
testFunc: testFunc,
}
}
func (tc temporaryErrorTestCase) Name() string {
return tc.name
}
func (tc temporaryErrorTestCase) Test(t *testing.T) {
t.Helper()
result := tc.testFunc(tc.err)
// Test error message
AssertEqual(t, tc.expected, result.Error(), "message")
// Test interface methods
if tempErr, ok := result.(interface{ IsTemporary() bool }); ok {
AssertTrue(t, tempErr.IsTemporary(), "IsTemporary result")
}
if timeoutErr, ok := result.(interface{ IsTimeout() bool }); ok {
AssertEqual(t, tc.isTimeout, timeoutErr.IsTimeout(), "IsTimeout result")
}
// Test legacy methods
if tempErr, ok := result.(interface{ Temporary() bool }); ok {
AssertTrue(t, tempErr.Temporary(), "Temporary result")
}
if timeoutErr, ok := result.(interface{ Timeout() bool }); ok {
AssertEqual(t, tc.isTimeout, timeoutErr.Timeout(), "Timeout result")
}
}
func TestNewTimeoutError(t *testing.T) {
baseErr := errors.New("base error")
testCases := S(
newTemporaryErrorTestCase("nil error", nil, "time-out", true, NewTimeoutError),
newTemporaryErrorTestCase("with cause", baseErr, "time-out: base error", true, NewTimeoutError),
newTemporaryErrorTestCase("empty cause", errors.New(emptyString), "time-out", true, NewTimeoutError),
)
RunTestCases(t, testCases)
}
func TestNewTemporaryError(t *testing.T) {
baseErr := errors.New("base error")
testCases := S(
newTemporaryErrorTestCase("nil error", nil, "", false, NewTemporaryError),
newTemporaryErrorTestCase("with cause", baseErr, "base error", false, NewTemporaryError),
newTemporaryErrorTestCase("empty cause", errors.New(emptyString), "", false, NewTemporaryError),
)
RunTestCases(t, testCases)
}
// Test for TemporaryError with nil receiver
func TestTemporaryErrorNilReceiver(t *testing.T) {
var tempErr *TemporaryError
AssertEqual(t, "", tempErr.Error(), "nil TemporaryError should return empty string")
AssertFalse(t, tempErr.IsTimeout(), "nil TemporaryError IsTimeout")
AssertFalse(t, tempErr.Timeout(), "nil TemporaryError Timeout")
}
// Test case for CheckIsTemporary function
type checkIsTemporaryTestCase struct {
err error
name string
expectedIs bool
expectedKnown bool
}
func (tc checkIsTemporaryTestCase) Name() string {
return tc.name
}
func (tc checkIsTemporaryTestCase) Test(t *testing.T) {
t.Helper()
is, known := CheckIsTemporary(tc.err)
AssertEqual(t, tc.expectedIs, is, "CheckIsTemporary is result")
AssertEqual(t, tc.expectedKnown, known, "CheckIsTemporary known result")
}
func newCheckIsTemporaryTestCase(name string, err error, expectedIs, expectedKnown bool) checkIsTemporaryTestCase {
return checkIsTemporaryTestCase{
err: err,
name: name,
expectedIs: expectedIs,
expectedKnown: expectedKnown,
}
}
func checkIsTemporaryTestCases() []checkIsTemporaryTestCase {
tempErr := NewTemporaryError(errors.New("temp error"))
timeoutErr := NewTimeoutError(errors.New("timeout error"))
regularErr := errors.New("regular error")
return S(
newCheckIsTemporaryTestCase("nil error", nil, false, true),
newCheckIsTemporaryTestCase("temporary error", tempErr, true, true),
newCheckIsTemporaryTestCase("timeout error", timeoutErr, true, true),
newCheckIsTemporaryTestCase("regular error", regularErr, false, false),
newCheckIsTemporaryTestCase("IsTemporary-only interface",
&isTemporaryOnlyError{}, true, true),
)
}
// Test CheckIsTemporary function (0% coverage)
func TestCheckIsTemporary(t *testing.T) {
RunTestCases(t, checkIsTemporaryTestCases())
}
// Test case for IsTemporary function
type isTemporaryTestCase struct {
err error
name string
expected bool
}
func (tc isTemporaryTestCase) Name() string {
return tc.name
}
func (tc isTemporaryTestCase) Test(t *testing.T) {
t.Helper()
result := IsTemporary(tc.err)
AssertEqual(t, tc.expected, result, "IsTemporary result")
}
func newIsTemporaryTestCase(name string, err error, expected bool) isTemporaryTestCase {
return isTemporaryTestCase{
err: err,
name: name,
expected: expected,
}
}
func isTemporaryTestCases() []isTemporaryTestCase {
tempErr := NewTemporaryError(errors.New("temp error"))
timeoutErr := NewTimeoutError(errors.New("timeout error"))
wrappedTempErr := fmt.Errorf("wrapped: %w", tempErr)
regularErr := errors.New("regular error")
return S(
newIsTemporaryTestCase("nil error", nil, false),
newIsTemporaryTestCase("temporary error", tempErr, true),
newIsTemporaryTestCase("timeout error", timeoutErr, true),
newIsTemporaryTestCase("wrapped temporary error", wrappedTempErr, true),
newIsTemporaryTestCase("regular error", regularErr, false),
)
}
// Test IsTemporary function (0% coverage)
func TestIsTemporary(t *testing.T) {
RunTestCases(t, isTemporaryTestCases())
}
// Test case for CheckIsTimeout function
type checkIsTimeoutTestCase struct {
err error
name string
expectedIs bool
expectedKnown bool
}
func (tc checkIsTimeoutTestCase) Name() string {
return tc.name
}
func (tc checkIsTimeoutTestCase) Test(t *testing.T) {
t.Helper()
is, known := CheckIsTimeout(tc.err)
AssertEqual(t, tc.expectedIs, is, "CheckIsTimeout is result")
AssertEqual(t, tc.expectedKnown, known, "CheckIsTimeout known result")
}
func newCheckIsTimeoutTestCase(name string, err error, expectedIs, expectedKnown bool) checkIsTimeoutTestCase {
return checkIsTimeoutTestCase{
err: err,
name: name,
expectedIs: expectedIs,
expectedKnown: expectedKnown,
}
}
func checkIsTimeoutTestCases() []checkIsTimeoutTestCase {
tempErr := NewTemporaryError(errors.New("temp error"))
timeoutErr := NewTimeoutError(errors.New("timeout error"))
regularErr := errors.New("regular error")
return S(
newCheckIsTimeoutTestCase("nil error", nil, false, true),
newCheckIsTimeoutTestCase("temporary error", tempErr, false, true),
newCheckIsTimeoutTestCase("timeout error", timeoutErr, true, true),
newCheckIsTimeoutTestCase("regular error", regularErr, false, false),
newCheckIsTimeoutTestCase("IsTimeout-only interface",
&isTimeoutOnlyError{}, true, true),
)
}
// Test CheckIsTimeout function (0% coverage)
func TestCheckIsTimeout(t *testing.T) {
RunTestCases(t, checkIsTimeoutTestCases())
}
// Test case for IsTimeout function
type isTimeoutTestCase struct {
err error
name string
expected bool
}
func (tc isTimeoutTestCase) Name() string {
return tc.name
}
func (tc isTimeoutTestCase) Test(t *testing.T) {
t.Helper()
result := IsTimeout(tc.err)
AssertEqual(t, tc.expected, result, "IsTimeout result")
}
func newIsTimeoutTestCase(name string, err error, expected bool) isTimeoutTestCase {
return isTimeoutTestCase{
err: err,
name: name,
expected: expected,
}
}
func isTimeoutTestCases() []isTimeoutTestCase {
tempErr := NewTemporaryError(errors.New("temp error"))
timeoutErr := NewTimeoutError(errors.New("timeout error"))
wrappedTimeoutErr := fmt.Errorf("wrapped: %w", timeoutErr)
regularErr := errors.New("regular error")
return S(
newIsTimeoutTestCase("nil error", nil, false),
newIsTimeoutTestCase("temporary error", tempErr, false),
newIsTimeoutTestCase("timeout error", timeoutErr, true),
newIsTimeoutTestCase("wrapped timeout error", wrappedTimeoutErr, true),
newIsTimeoutTestCase("regular error", regularErr, false),
)
}
// Test IsTimeout function (0% coverage)
func TestIsTimeout(t *testing.T) {
RunTestCases(t, isTimeoutTestCases())
}
// emptyMessageError returns "" from Error() so that WrappedError.Error()
// takes the `s == ""` branch and falls back to the wrapper's note.
type emptyMessageError struct{}
func (emptyMessageError) Error() string { return "" }
// Types that implement only one of Unwrap/Errors/IsTemporary/IsTimeout
// to exercise the non-first branches of the type switches.
type errorsOnlyError struct{ errs []error }
func (*errorsOnlyError) Error() string { return "errors-only" }
func (e *errorsOnlyError) Errors() []error { return e.errs }
type singleUnwrapError struct{ inner error }
func (*singleUnwrapError) Error() string { return "single-unwrap" }
func (e *singleUnwrapError) Unwrap() error { return e.inner }
type isTemporaryOnlyError struct{}
func (*isTemporaryOnlyError) Error() string { return "is-temporary-only" }
func (*isTemporaryOnlyError) IsTemporary() bool { return true }
type isTimeoutOnlyError struct{}
func (*isTimeoutOnlyError) Error() string { return "is-timeout-only" }
func (*isTimeoutOnlyError) IsTimeout() bool { return true }
// wrappedErrorTestCase exercises WrappedError.Error and Unwrap,
// including the typed-nil receiver and empty-cause fallback branches.
type wrappedErrorTestCase struct {
err *WrappedError
wantUnwrap error
name string
wantError string
}
func newWrappedErrorTestCase(name string, err *WrappedError,
wantError string, wantUnwrap error) wrappedErrorTestCase {
return wrappedErrorTestCase{
err: err,
name: name,
wantError: wantError,
wantUnwrap: wantUnwrap,
}
}
func (tc wrappedErrorTestCase) Name() string { return tc.name }
func (tc wrappedErrorTestCase) Test(t *testing.T) {
t.Helper()
AssertEqual(t, tc.wantError, tc.err.Error(), "WrappedError.Error")
if tc.wantUnwrap == nil {
AssertNil(t, tc.err.Unwrap(), "WrappedError.Unwrap")
return
}
AssertErrorIs(t, tc.err.Unwrap(), tc.wantUnwrap, "WrappedError.Unwrap")
}
func wrappedErrorTestCases() []wrappedErrorTestCase {
empty := emptyMessageError{}
return S(
newWrappedErrorTestCase("nil receiver", nil, "", nil),
newWrappedErrorTestCase("empty cause",
&WrappedError{cause: empty, note: "note"}, "note", empty),
)
}
// Cover WrappedError method behaviour across its main branches.
func TestWrappedError(t *testing.T) {
RunTestCases(t, wrappedErrorTestCases())
}
// unwrapBranchesTestCase covers the three Unwrap type-switch arms plus
// the default branch, asserting that the returned slice contains the
// expected inner error(s).
type unwrapBranchesTestCase struct {
err error
name string
wantInner []error
}
func newUnwrapBranchesTestCase(name string, err error,
wantInner []error) unwrapBranchesTestCase {
return unwrapBranchesTestCase{err: err, name: name, wantInner: wantInner}
}
func (tc unwrapBranchesTestCase) Name() string { return tc.name }
func (tc unwrapBranchesTestCase) Test(t *testing.T) {
t.Helper()
got := Unwrap(tc.err)
AssertSliceEqual(t, tc.wantInner, got, "Unwrap")
}
func unwrapBranchesTestCases() []unwrapBranchesTestCase {
inner := errors.New("inner")
return S(
newUnwrapBranchesTestCase("Unwrap []error branch",
&CompoundError{Errs: []error{inner}}, []error{inner}),
newUnwrapBranchesTestCase("Errors() branch",
&errorsOnlyError{errs: []error{inner}}, []error{inner}),
newUnwrapBranchesTestCase("Unwrap error branch",
&singleUnwrapError{inner: inner}, []error{inner}),
newUnwrapBranchesTestCase("default branch",
errors.New("plain"), nil),
)
}
// Test Unwrap(err) hits all three type-switch branches and the default.
func TestUnwrapBranches(t *testing.T) {
RunTestCases(t, unwrapBranchesTestCases())
}
// Test ErrInvalid is an alias of the standard sentinel, so errors.Is
// matches across the boundary and the message text is unchanged.
func TestErrInvalidAlias(t *testing.T) {
AssertSame(t, fs.ErrInvalid, ErrInvalid, "fs.ErrInvalid identity")
AssertErrorIs(t, ErrInvalid, os.ErrInvalid, "os.ErrInvalid match")
AssertErrorIs(t, os.ErrInvalid, ErrInvalid, "reverse match")
AssertEqual(t, "invalid argument", ErrInvalid.Error(), "message")
}