-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
104 lines (90 loc) · 2.66 KB
/
Copy patherrors_test.go
File metadata and controls
104 lines (90 loc) · 2.66 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
package sysproxy
import (
"errors"
"testing"
)
func TestIsNonCritical(t *testing.T) {
if IsNonCritical(nil) {
t.Fatal("nil error must not be non-critical")
}
plain := errors.New("plain")
if IsNonCritical(plain) {
t.Fatal("plain error must not be non-critical")
}
nc := &nonCriticalError{err: plain}
if !IsNonCritical(nc) {
t.Fatal("nonCriticalError must be detected")
}
wrapped := errors.New("wrapper: " + nc.Error())
if IsNonCritical(wrapped) {
t.Fatal("string-wrapped error must not be detected via errors.As")
}
}
func TestNonCriticalErrorMethods(t *testing.T) {
base := errors.New("permission denied")
nc := &nonCriticalError{err: base}
if got := nc.Error(); got != "permission denied" {
t.Fatalf("Error() = %q, want %q", got, "permission denied")
}
if !errors.Is(nc, base) {
t.Fatal("Unwrap should expose wrapped error")
}
var nilNC *nonCriticalError
if got := nilNC.Error(); got == "" {
t.Fatal("nil receiver Error() should return fallback message")
}
if nilNC.Unwrap() != nil {
t.Fatal("nil receiver Unwrap() should return nil")
}
}
func TestRequiresElevation(t *testing.T) {
if RequiresElevation(nil) {
t.Fatal("nil error must not require elevation")
}
if RequiresElevation(errors.New("plain")) {
t.Fatal("plain error must not require elevation")
}
base := errors.New("permission denied")
err := asElevationError(base)
if !RequiresElevation(err) {
t.Fatal("elevationError should be detected")
}
if !IsNonCritical(err) {
t.Fatal("elevationError should also be non-critical")
}
if !errors.Is(err, base) {
t.Fatal("elevationError should unwrap to underlying cause")
}
// asElevationError(nil) must be nil.
if asElevationError(nil) != nil {
t.Fatal("asElevationError(nil) should return nil")
}
}
func TestElevationErrorMethods(t *testing.T) {
base := errors.New("permission denied")
ee := &elevationError{err: base}
if got := ee.Error(); got != "permission denied" {
t.Fatalf("Error() = %q, want %q", got, "permission denied")
}
if !errors.Is(ee, base) {
t.Fatal("elevationError.Unwrap should expose wrapped error")
}
var nilEE *elevationError
if got := nilEE.Error(); got == "" {
t.Fatal("nil receiver Error() should return fallback message")
}
if nilEE.Unwrap() != nil {
t.Fatal("nil receiver Unwrap() should return nil")
}
}
func TestSentinelsAreDistinct(t *testing.T) {
// Distinct instances so callers can errors.Is precisely.
for _, e := range []error{ErrProxyNotSet, ErrProxyNotEnabled, ErrUnsupportedPlatform, ErrToolMissing} {
if e == nil {
t.Fatal("sentinel error unexpectedly nil")
}
}
if errors.Is(ErrProxyNotSet, ErrProxyNotEnabled) {
t.Fatal("distinct sentinels must not compare equal")
}
}