Repository navigation
Expand file tree
/
Copy pathspec_test.go
More file actions
337 lines (320 loc) · 14.3 KB
/
Copy pathspec_test.go
File metadata and controls
337 lines (320 loc) · 14.3 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
package errors_test
import (
"context"
stderrors "errors"
"fmt"
"reflect"
"sync"
"testing"
"github.com/0xsj/n2f-go/pkg/errors"
)
func TestE01Vocabulary(t *testing.T) {
cases := []struct {
kind errors.Kind
name string
}{
{errors.Internal, "internal"}, {errors.Invalid, "invalid"},
{errors.NotFound, "not_found"}, {errors.Conflict, "conflict"},
{errors.Unauthenticated, "unauthenticated"}, {errors.Forbidden, "forbidden"},
{errors.RateLimited, "rate_limited"}, {errors.Unavailable, "unavailable"},
{errors.Timeout, "timeout"}, {errors.Canceled, "canceled"},
}
if got := errors.Kinds(); len(got) != len(cases) {
t.Fatalf("kinds: %v", got)
}
counts := make(map[errors.Kind]int)
for _, kind := range errors.Kinds() {
counts[kind]++
}
for _, c := range cases {
if counts[c.kind] != 1 || c.kind.String() != c.name {
t.Errorf("kind %d must render %q", c.kind, c.name)
}
if got, ok := errors.ParseKind(c.name); !ok || got != c.kind {
t.Errorf("parse %q: %v, %v", c.name, got, ok)
}
}
for _, name := range []string{"", "INTERNAL", "not-found", "other"} {
if _, ok := errors.ParseKind(name); ok {
t.Errorf("accepted unknown name %q", name)
}
}
listed := errors.Kinds()
first := listed[0]
listed[0] = errors.Kind(255)
if errors.Kinds()[0] != first {
t.Fatal("caller mutated vocabulary")
}
}
func TestE02Construction(t *testing.T) {
e := errors.New(errors.Invalid, "email is required").WithType("account.email_required").WithField("email", "required")
if e == nil || !errors.IsKind(e, errors.Invalid) {
t.Fatal("constructed failure lost its classification")
}
if errors.Message(e) != "email is required" || errors.TypeOf(e) != "account.email_required" || errors.FieldsOf(e)["email"] != "required" {
t.Fatal("public data was not preserved")
}
if errors.Message(errors.New(errors.Conflict, "")) != "request failed" {
t.Fatal("empty public message needs safe fallback")
}
if !errors.IsKind(&errors.Error{}, errors.Internal) {
t.Fatal("zero Error must classify Internal")
}
}
func TestE03AbsenceAndUnknown(t *testing.T) {
for _, e := range []error{nil, stderrors.New("PRIVATE"), errors.New(errors.Kind(255), "PRIVATE")} {
if _, ok := errors.KindOf(e); ok {
t.Errorf("unexpected classification: %v", e)
}
if errors.IsKind(e, errors.Internal) {
t.Fatal("absence/unknown matched explicit Internal")
}
}
if errors.Wrap(nil, errors.Internal, "context") != nil {
t.Fatal("wrap manufactured failure")
}
if errors.Message(nil) != "" || errors.TypeOf(nil) != "" || len(errors.FieldsOf(nil)) != 0 {
t.Fatal("nil has public failure data")
}
if errors.New(errors.Conflict, "exists").WithCause(nil) == nil {
t.Fatal("constructing a condition with no cause is still failure")
}
}
func TestE04PublicProjection(t *testing.T) {
for _, e := range []error{
errors.New(errors.Internal, "PRIVATE message").WithType("PRIVATE.type").WithField("PRIVATE", "field").WithDetail("sql", "PRIVATE SQL"),
stderrors.New("PRIVATE upstream"),
errors.New(errors.Kind(255), "PRIVATE invalid kind").WithType("PRIVATE.type"),
} {
if errors.Message(e) != "internal error" || errors.TypeOf(e) != "" || len(errors.FieldsOf(e)) != 0 {
t.Fatalf("unsafe public projection: %q %q %v", errors.Message(e), errors.TypeOf(e), errors.FieldsOf(e))
}
}
for _, k := range []errors.Kind{errors.Invalid, errors.NotFound, errors.Conflict, errors.Unauthenticated, errors.Forbidden, errors.RateLimited, errors.Unavailable, errors.Timeout, errors.Canceled} {
e := errors.New(k, "safe message").WithType("module.condition").WithField("input", "problem")
if errors.Message(e) != "safe message" || errors.TypeOf(e) != "module.condition" || errors.FieldsOf(e)["input"] != "problem" {
t.Errorf("safe data hidden for %v", k)
}
}
}
func TestE05MetadataIsolation(t *testing.T) {
fields, details := map[string]string{"email": "required"}, map[string]string{"operation": "insert"}
base := errors.New(errors.Invalid, "invalid input").WithFields(fields).WithDetails(details)
fields["email"], details["operation"] = "MUTATED", "MUTATED"
if errors.FieldsOf(base)["email"] != "required" || errors.DetailsOf(base)["operation"] != "insert" {
t.Fatal("supplied map aliases failure")
}
derived := base.WithField("email", "malformed").WithDetail("operation", "register")
if errors.FieldsOf(derived)["email"] != "malformed" || errors.DetailsOf(derived)["operation"] != "register" {
t.Fatal("derived metadata not updated")
}
public, private := errors.FieldsOf(derived), errors.DetailsOf(derived)
public["email"], private["operation"] = "MUTATED", "MUTATED"
if errors.FieldsOf(derived)["email"] != "malformed" || errors.DetailsOf(derived)["operation"] != "register" {
t.Fatal("getter exposes internal map")
}
if errors.FieldsOf(base)["email"] != "required" || errors.DetailsOf(base)["operation"] != "insert" {
t.Fatal("derivation changed base")
}
}
type driverFailure struct{}
func (*driverFailure) Error() string { return "PRIVATE driver detail" }
func TestE06AnnotationPreservesIdentityAndCause(t *testing.T) {
condition := errors.New(errors.Conflict, "email taken").WithType("account.email_taken")
cause := &driverFailure{}
occurrence := condition.WithCause(cause)
derived := occurrence.WithDetail("operation", "register").WithField("email", "taken")
annotated := fmt.Errorf("register account: %w", derived)
if !stderrors.Is(annotated, condition) || !stderrors.Is(annotated, cause) {
t.Fatal("annotation lost condition or cause identity")
}
var driver *driverFailure
if !stderrors.As(annotated, &driver) || driver != cause {
t.Fatal("typed cause no longer inspectable")
}
if !errors.IsKind(annotated, errors.Conflict) || errors.TypeOf(annotated) != "account.email_taken" {
t.Fatal("annotation changed public meaning")
}
other := errors.New(errors.Conflict, "email taken").WithType("account.email_taken")
if stderrors.Is(annotated, other) {
t.Fatal("matching text must not equate separate conditions")
}
if stderrors.Unwrap(condition) != nil {
t.Fatal("attaching source mutated condition")
}
}
func TestE07TranslationOwnsProjection(t *testing.T) {
inner := errors.New(errors.Conflict, "inner").WithType("inner.type").WithField("inner", "field").WithDetail("inner", "detail")
outer := errors.Wrap(inner, errors.Unavailable, "try later")
if !errors.IsKind(outer, errors.Unavailable) || errors.Message(outer) != "try later" {
t.Fatal("translation lost outer classification")
}
if errors.TypeOf(outer) != "" || len(errors.FieldsOf(outer)) != 0 || len(errors.DetailsOf(outer)) != 0 {
t.Fatal("inner metadata filled outer frame")
}
if !stderrors.Is(outer, inner) {
t.Fatal("translation discarded cause")
}
first, second := stderrors.New("first"), stderrors.New("second")
base := errors.New(errors.Conflict, "condition").WithCause(first)
changed := base.WithCause(second)
if stderrors.Unwrap(base) != first || stderrors.Unwrap(changed) != second {
t.Fatal("source replacement mutated an existing occurrence")
}
}
func TestE08ContextClassification(t *testing.T) {
for cause, kind := range map[error]errors.Kind{context.Canceled: errors.Canceled, context.DeadlineExceeded: errors.Timeout} {
e := fmt.Errorf("operation: %w", cause)
if !errors.IsKind(e, kind) {
t.Fatalf("wrapped context lost %v", kind)
}
if errors.TypeOf(e) != "" || len(errors.FieldsOf(e)) != 0 {
t.Fatal("invented context public metadata")
}
outer := errors.Wrap(e, errors.Unavailable, "capability unavailable")
if !errors.IsKind(outer, errors.Unavailable) {
t.Fatal("source overrode explicit outer classification")
}
}
if _, ok := errors.KindOf(stderrors.New("context canceled")); ok {
t.Fatal("text classified a foreign error")
}
}
func TestE09AggregatesHaveNoImplicitSummary(t *testing.T) {
known := errors.New(errors.Unavailable, "try later").WithType("dependency.offline").WithField("x", "y").WithDetail("branch", "known")
unknown := stderrors.New("PRIVATE unknown")
for _, joined := range []error{stderrors.Join(known, unknown), stderrors.Join(unknown, known), fmt.Errorf("multiple: %w; %w", known, unknown)} {
if _, ok := errors.KindOf(joined); ok {
t.Fatal("selected a branch as the aggregate kind")
}
if errors.Message(joined) != "internal error" || errors.TypeOf(joined) != "" || len(errors.FieldsOf(joined)) != 0 || len(errors.DetailsOf(joined)) != 0 {
t.Fatal("aggregate exposed a branch projection")
}
summary := errors.New(errors.Unavailable, "batch interrupted").WithCause(joined)
if !errors.IsKind(summary, errors.Unavailable) || !stderrors.Is(summary, unknown) || !stderrors.Is(summary, known) {
t.Fatal("explicit summary lost underlying outcomes")
}
}
if !reflect.DeepEqual(errors.FieldsOf(nil), map[string]string(nil)) {
t.Fatal("absent fields should be nil")
}
}
func ExampleError() {
condition := errors.New(errors.Conflict, "email already registered").
WithType("account.email_taken").WithField("email", "taken")
e := condition.WithCause(stderrors.New("PRIVATE database constraint")).WithDetail("operation", "register")
view, _ := errors.Public(e)
fmt.Println(view.Kind.String())
fmt.Println(view.Message)
fmt.Println(view.Type)
fmt.Println(view.Fields["email"])
// Output:
// conflict
// email already registered
// account.email_taken
// taken
}
func TestE05ConcurrentDerivations(t *testing.T) {
base := errors.New(errors.Invalid, "invalid input").WithField("email", "required")
var workers sync.WaitGroup
for i := range 32 {
workers.Add(1)
go func() {
defer workers.Done()
value := fmt.Sprint(i)
derived := base.WithField("email", value).WithDetail("attempt", value)
if errors.FieldsOf(derived)["email"] != value || errors.DetailsOf(derived)["attempt"] != value || !stderrors.Is(derived, base) {
t.Error("concurrent derivation lost its own metadata or condition")
}
}()
}
workers.Wait()
if errors.FieldsOf(base)["email"] != "required" || len(errors.DetailsOf(base)) != 0 {
t.Fatal("concurrent derivations changed the shared condition")
}
}
func TestE10MergeAndSourceReplacement(t *testing.T) {
first, second := stderrors.New("PRIVATE first"), stderrors.New("PRIVATE second")
base := errors.New(errors.Invalid, "invalid input").WithType("account.invalid").
WithFields(map[string]string{"email": "required", "name": "required"}).
WithDetails(map[string]string{"operation": "insert", "attempt": "1"}).
WithCause(first)
derived := base.WithFields(map[string]string{"email": "malformed", "age": "positive"}).
WithDetails(map[string]string{"operation": "register", "worker": "signup"}).
WithCause(second)
if !reflect.DeepEqual(errors.FieldsOf(derived), map[string]string{"email": "malformed", "name": "required", "age": "positive"}) {
t.Fatal("fields did not merge across source replacement")
}
if !reflect.DeepEqual(errors.DetailsOf(derived), map[string]string{"operation": "register", "attempt": "1", "worker": "signup"}) {
t.Fatal("diagnostic details did not merge across source replacement")
}
if errors.Message(derived) != "invalid input" || errors.TypeOf(derived) != "account.invalid" || !errors.IsKind(derived, errors.Invalid) {
t.Fatal("source replacement changed public meaning")
}
if !stderrors.Is(derived, base) || !stderrors.Is(derived, second) || stderrors.Is(derived, first) {
t.Fatal("source replacement lost condition identity or retained the old source")
}
if stderrors.Unwrap(base) != first || errors.FieldsOf(base)["email"] != "required" || errors.DetailsOf(base)["operation"] != "insert" {
t.Fatal("derivation changed the original occurrence")
}
emptyType := errors.New(errors.Conflict, "").WithType("account.old").WithType("").WithDetail("operation", "register")
if errors.TypeOf(emptyType) != "" || errors.Message(emptyType) != "request failed" {
t.Fatal("empty type or message changed through annotation")
}
}
func TestGo01CompletePublicSnapshot(t *testing.T) {
if view, present := errors.Public(nil); present || view.Message != "" || view.Type != "" || len(view.Fields) != 0 {
t.Fatal("nil must have no public failure snapshot")
}
var typedNil *errors.Error
for _, unknown := range []error{
typedNil, stderrors.New("PRIVATE foreign"),
errors.New(errors.Kind(255), "PRIVATE invalid kind"),
stderrors.Join(errors.New(errors.Unavailable, "PRIVATE branch"), stderrors.New("PRIVATE other")),
} {
view, present := errors.Public(unknown)
if !present || !reflect.DeepEqual(view, errors.PublicInfo{Kind: errors.Internal, Message: "internal error"}) {
t.Fatalf("non-nil unknown failure needs the complete safe fallback: %+v, %v", view, present)
}
if _, classified := errors.KindOf(unknown); classified {
t.Fatal("presentation fallback manufactured classification")
}
}
for _, kind := range errors.Kinds() {
t.Run(kind.String(), func(t *testing.T) {
e := errors.New(kind, "safe message").WithType("account.condition").
WithField("email", "taken").WithDetail("sql", "PRIVATE SQL").
WithCause(stderrors.New("PRIVATE cause"))
annotated := fmt.Errorf("PRIVATE operation: %w", e)
view, present := errors.Public(annotated)
want := errors.PublicInfo{Kind: kind, Message: "safe message", Type: "account.condition", Fields: map[string]string{"email": "taken"}}
if kind == errors.Internal {
want = errors.PublicInfo{Kind: errors.Internal, Message: "internal error"}
}
if !present || !reflect.DeepEqual(view, want) {
t.Fatalf("public snapshot = %+v, %v; want %+v", view, present, want)
}
if view.Message != errors.Message(annotated) || view.Type != errors.TypeOf(annotated) || !reflect.DeepEqual(view.Fields, errors.FieldsOf(annotated)) {
t.Fatal("complete and individual projections disagree")
}
if view.Fields != nil {
view.Fields["email"] = "MUTATED"
again, _ := errors.Public(annotated)
if again.Fields["email"] != "taken" {
t.Fatal("snapshot aliases the originating error")
}
}
})
}
for cause, kind := range map[error]errors.Kind{context.Canceled: errors.Canceled, context.DeadlineExceeded: errors.Timeout} {
view, present := errors.Public(fmt.Errorf("PRIVATE operation: %w", cause))
if !present || !reflect.DeepEqual(view, errors.PublicInfo{Kind: kind, Message: "request failed"}) {
t.Fatal("context failure needs a safe complete projection")
}
}
inner := errors.New(errors.Invalid, "inner").WithType("inner.type").WithField("inner", "field")
outer, present := errors.Public(errors.Wrap(inner, errors.Unavailable, ""))
if !present || !reflect.DeepEqual(outer, errors.PublicInfo{Kind: errors.Unavailable, Message: "request failed"}) {
t.Fatal("outer snapshot borrowed metadata from its source")
}
}