Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
91 lines (80 loc) · 2.37 KB
/
Copy patherrors_test.go
File metadata and controls
91 lines (80 loc) · 2.37 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
package torx
import (
"errors"
"testing"
)
func TestErrorIsMatchesKindAndCause(t *testing.T) {
cause := errors.New("no free port")
err := Wrap(ErrAllocation, "coordination: allocate port", cause)
if !errors.Is(err, ErrAllocation) {
t.Errorf("errors.Is(err, ErrAllocation) = false, want true")
}
if !errors.Is(err, cause) {
t.Errorf("errors.Is(err, cause) = false, want true")
}
if errors.Is(err, ErrBackend) {
t.Errorf("errors.Is(err, ErrBackend) = true, want false")
}
}
func TestErrorAsExtractsStructured(t *testing.T) {
err := Wrap(ErrService, "service: start", nil)
var te *Error
if !errors.As(err, &te) {
t.Fatalf("errors.As(err, *Error) = false, want true")
}
if te.Op != "service: start" {
t.Errorf("Op = %q, want %q", te.Op, "service: start")
}
if !errors.Is(te.Kind, ErrService) {
t.Errorf("Kind = %v, want ErrService", te.Kind)
}
}
func TestErrorMessage(t *testing.T) {
cause := errors.New("boom")
tests := []struct {
name string
err *Error
want string
}{
{"op and cause", Wrap(ErrBackend, "backend: exec", cause), "backend: exec: torx: backend operation failed: boom"},
{"op only", Wrap(ErrBackend, "backend: exec", nil), "backend: exec: torx: backend operation failed"},
{"cause only", Wrap(ErrBackend, "", cause), "torx: backend operation failed: boom"},
{"kind only", Wrap(ErrBackend, "", nil), "torx: backend operation failed"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := tc.err.Error(); got != tc.want {
t.Errorf("Error() = %q, want %q", got, tc.want)
}
})
}
}
func TestSentinelsAreDistinct(t *testing.T) {
all := []error{ErrAllocation, ErrBackend, ErrReadinessTimeout, ErrService}
for i, a := range all {
for j, b := range all {
if i != j && errors.Is(a, b) {
t.Errorf("errors.Is(%v, %v) = true, want false", a, b)
}
}
}
}
func TestMultiError(t *testing.T) {
var m MultiError
if err := m.Err(); err != nil {
t.Errorf("empty MultiError.Err() = %v, want nil", err)
}
m.Append(nil) // nil is ignored
if err := m.Err(); err != nil {
t.Errorf("after Append(nil), Err() = %v, want nil", err)
}
m.Append(Wrap(ErrService, "stop a", nil))
m.Append(Wrap(ErrBackend, "stop b", nil))
err := m.Err()
if err == nil {
t.Fatalf("Err() = nil, want joined error")
}
if !errors.Is(err, ErrService) || !errors.Is(err, ErrBackend) {
t.Errorf("joined error lost a cause: %v", err)
}
}