-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
198 lines (164 loc) · 4.72 KB
/
Copy patherrors_test.go
File metadata and controls
198 lines (164 loc) · 4.72 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
package lsremote
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Each sentinel exists with the documented message. The fixture is a
// table so a missing or mis-worded message is obvious at a glance.
func TestSentinels(t *testing.T) {
t.Parallel()
cases := []struct {
name string
err error
want string
}{
{"ErrNotFound", ErrNotFound, "lsremote: repository not found"},
{"ErrAuthRequired", ErrAuthRequired, "lsremote: authentication required"},
{"ErrAuthFailed", ErrAuthFailed, "lsremote: authentication failed"},
{"ErrUnsupportedProtocol", ErrUnsupportedProtocol, "lsremote: protocol/operation not supported by server"},
{"ErrServerRefused", ErrServerRefused, "lsremote: server refused"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
require.Error(t, tc.err)
assert.Equal(t, tc.want, tc.err.Error())
})
}
}
func TestProtocolError_Unwrap(t *testing.T) {
t.Parallel()
t.Run("returns the wrapped Err", func(t *testing.T) {
t.Parallel()
want := errors.New("boom")
pe := &ProtocolError{Op: "dial", Err: want}
assert.Same(t, want, pe.Unwrap())
})
t.Run("nil Err unwraps to nil", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{Op: "dial"}
assert.NoError(t, pe.Unwrap())
})
}
func TestProtocolError_ErrorsIs(t *testing.T) {
t.Parallel()
t.Run("direct sentinel match", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{Op: "ls-refs", Err: ErrNotFound}
require.ErrorIs(t, pe, ErrNotFound)
assert.NotErrorIs(t, pe, ErrAuthFailed)
})
t.Run("transitively wrapped sentinel match", func(t *testing.T) {
t.Parallel()
inner := fmt.Errorf("transport says: %w", ErrAuthRequired)
pe := &ProtocolError{Op: "advertisement", Err: inner}
assert.ErrorIs(t, pe, ErrAuthRequired)
})
t.Run("non-matching sentinel returns false", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{Op: "probe", Err: ErrNotFound}
assert.NotErrorIs(t, pe, ErrServerRefused)
})
}
func TestProtocolError_Error(t *testing.T) {
t.Parallel()
t.Run("includes op and wrapped error", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{
Op: "ls-refs",
URL: "https://example.test/repo.git",
Err: ErrNotFound,
}
s := pe.Error()
assert.True(t, strings.HasPrefix(s, "lsremote:"),
"Error() output should share the sentinel prefix, got %q", s)
assert.Contains(t, s, "ls-refs")
assert.Contains(t, s, ErrNotFound.Error())
assert.Contains(t, s, "https://example.test/repo.git")
})
t.Run("redacts credentials in URL", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{
Op: "dial",
URL: "https://alice:secret@example.test/repo.git",
Err: ErrAuthFailed,
}
s := pe.Error()
assert.NotContains(t, s, "secret",
"the URL password must never leak into Error() output")
assert.Contains(t, s, "***",
"transport.RedactURL replaces the password with ***")
assert.Contains(t, s, "alice",
"the username should be preserved in the redacted form")
})
t.Run("omits status when zero", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{
Op: "dial",
URL: "https://example.test/repo.git",
Err: ErrNotFound,
}
s := pe.Error()
assert.NotContains(t, strings.ToLower(s), "status 0")
assert.NotContains(t, strings.ToLower(s), "status")
})
t.Run("includes status when non-zero", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{
Op: "probe",
URL: "https://example.test/repo.git",
Status: 500,
Err: ErrServerRefused,
}
s := pe.Error()
assert.Contains(t, s, "500")
assert.Contains(t, strings.ToLower(s), "status")
})
t.Run("omits server section when empty", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{
Op: "ls-refs",
URL: "https://example.test/repo.git",
Err: ErrNotFound,
}
assert.NotContains(t, pe.Error(), "server:")
})
t.Run("includes server section when present", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{
Op: "ls-refs",
URL: "https://example.test/repo.git",
Err: ErrServerRefused,
Server: "upload-pack: not our ref",
}
s := pe.Error()
assert.Contains(t, s, "server:")
assert.Contains(t, s, "upload-pack: not our ref")
})
t.Run("does not panic with nil Err", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{
Op: "dial",
URL: "https://example.test/repo.git",
}
assert.NotPanics(t, func() {
_ = pe.Error()
})
})
t.Run("does not panic with a long Server payload", func(t *testing.T) {
t.Parallel()
pe := &ProtocolError{
Op: "ls-refs",
URL: "https://example.test/repo.git",
Err: ErrServerRefused,
Server: strings.Repeat("x", 1024),
}
assert.NotPanics(t, func() {
_ = pe.Error()
})
})
}