forked from GoogleCloudPlatform/compute-image-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
149 lines (134 loc) · 5.33 KB
/
error_test.go
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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package daisy
import (
"errors"
"strings"
"testing"
)
func TestAddErrs(t *testing.T) {
tests := []struct {
desc string
base DError
errs []error
want DError
}{
{"add nil to nil case", nil, nil, nil},
{"add error to nil case", nil, []error{errors.New("foo")}, &dErrImpl{errs: []error{errors.New("foo")}, errsType: []string{""}}},
{"add nil to DError case", &dErrImpl{errs: []error{errors.New("foo")}, errsType: []string{""}}, nil, &dErrImpl{errs: []error{errors.New("foo")}, errsType: []string{""}}},
{"add errors to DError case", &dErrImpl{errs: []error{errors.New("foo")}, errsType: []string{""}}, []error{errors.New("bar"), errors.New("baz")}, &dErrImpl{errs: []error{errors.New("foo"), errors.New("bar"), errors.New("baz")}, errsType: []string{"", "", ""}}},
}
for _, tt := range tests {
got := addErrs(tt.base, tt.errs...)
if diffRes := diff(got, tt.want, 0); diffRes != "" {
t.Errorf("%s: (-got,+want)\n%s", tt.desc, diffRes)
}
if diffRes := diff(tt.base, tt.want, 0); tt.base != nil && diffRes != "" {
t.Errorf("%s: base DError not modified as expected: (-got,+want)\n%s", tt.desc, diffRes)
}
}
}
func TestDErrError(t *testing.T) {
tests := []struct {
desc string
err DError
want string
}{
{"no error type case", &dErrImpl{errs: []error{errors.New("foo")}, errsType: []string{""}}, "foo"},
{"error type case", &dErrImpl{errs: []error{errors.New("foo")}, errsType: []string{"FOO"}}, "FOO: foo"},
{
"multierror case",
&dErrImpl{errs: []error{errors.New("foo"), errors.New("bar")}, errsType: []string{"", ""}},
"Multiple errors:\n* foo\n* bar",
},
}
for _, tt := range tests {
got := tt.err.Error()
if got != tt.want {
t.Errorf("%s: got %q, want %q", tt.desc, got, tt.want)
}
}
}
func TestErrf(t *testing.T) {
got := Errf("%s %s", "hello", "world")
want := &dErrImpl{errs: []error{errors.New("hello world")}, errsType: []string{""}}
if diffRes := diff(got, want, 0); diffRes != "" {
t.Errorf("Error not created as expected: (-got,+want)\n%s", diffRes)
}
}
func TestTypedErrf(t *testing.T) {
got := typedErrf("FOO", "%s %s", "hello", "world")
want := &dErrImpl{errs: []error{errors.New("hello world")}, errsType: []string{"FOO"}}
if diffRes := diff(got, want, 0); diffRes != "" {
t.Errorf("Error not created as expected: (-got,+want)\n%s", diffRes)
}
}
func TestDErrImplAdd(t *testing.T) {
tests := []struct {
desc string
base *dErrImpl
add error
want *dErrImpl
wantErrType string
}{
{
"add error case",
&dErrImpl{errs: []error{errors.New("foo")}, errsType: []string{"FOO"}},
errors.New("bar"),
&dErrImpl{errs: []error{errors.New("foo"), errors.New("bar")}, errsType: []string{"FOO", ""}},
multiError,
},
{
"add dErrImpl case",
&dErrImpl{errs: []error{errors.New("foo")}, errsType: []string{"FOO"}},
&dErrImpl{errs: []error{errors.New("bar")}, errsType: []string{"FOO"}},
&dErrImpl{errs: []error{errors.New("foo"), errors.New("bar")}, errsType: []string{"FOO", "BAR"}},
multiError,
},
{
"add " + multiError + " case",
&dErrImpl{errs: []error{errors.New("foo"), errors.New("bar")}, errsType: []string{"FOO", "BAR"}},
&dErrImpl{errs: []error{errors.New("baz"), errors.New("gaz")}, errsType: []string{"FOO", "BAR"}},
&dErrImpl{errs: []error{errors.New("foo"), errors.New("bar"), errors.New("baz"), errors.New("gaz")}, errsType: []string{"FOO", "BAR", "FOO", "BAR"}},
multiError,
},
}
for _, tt := range tests {
tt.base.add(tt.add)
if diffRes := diff(tt.base, tt.want, 0); diffRes != "" {
t.Errorf("%s: base dErrImpl not modified as expected: (-got,+want)\n%s", tt.desc, diffRes)
}
if diffRes := diff(tt.base.etype(), tt.wantErrType, 0); diffRes != "" {
t.Errorf("%s: base dErrImpl not modified as expected: (-got,+want)\n%s", tt.desc, diffRes)
}
}
}
func TestNestedAnonymizedDErrorMessage(t *testing.T) {
innerDErr1 := Errf("inner error 1: %v %v", "root cause 1", "root cause 2")
innerDErr2 := Errf("inner error 2: %v %v", "root cause 3", "root cause 4")
innerDErr1.add(innerDErr2)
outerDErr := wrapErrf(innerDErr1, "outer error: %v", "bad news")
gotAnonymizedMsg := strings.Join(outerDErr.AnonymizedErrs(), ",")
wantAnonymizedMsg := "outer error: %v: inner error 1: %v %v; inner error 2: %v %v"
if diffRes := diff(wantAnonymizedMsg, gotAnonymizedMsg, 0); diffRes != "" {
t.Errorf("nested DError doesn't have correct anonymized error message: (-got,+want)\n%s", diffRes)
}
gotMsg := outerDErr.Error()
wantMsg := "outer error: bad news: Multiple errors:\n" +
"* inner error 1: root cause 1 root cause 2\n" +
"* inner error 2: root cause 3 root cause 4"
if diffRes := diff(wantMsg, gotMsg, 0); diffRes != "" {
t.Errorf("nested DError doesn't have correct error message: (-got,+want)\n%s", diffRes)
}
}