Skip to content

Commit 66d3ab3

Browse files
authored
Merge pull request #4723 from kersten/chore/matcherror-in-tests
🌱 (chore): replace errors.As() with MatchError() in error assertions
2 parents 2488bf4 + 3cb1e5e commit 66d3ab3

File tree

3 files changed

+83
-31
lines changed

3 files changed

+83
-31
lines changed

pkg/config/store/yaml/store_test.go

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package yaml
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"os"
2223
"testing"
2324

@@ -91,31 +92,54 @@ layout: ""
9192
It("should fail if no file exists at the default path", func() {
9293
err := s.Load()
9394
Expect(err).To(HaveOccurred())
94-
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
95+
Expect(err).To(MatchError(store.LoadError{
96+
Err: fmt.Errorf("unable to read %q file: %w", DefaultPath, &os.PathError{
97+
Err: os.ErrNotExist,
98+
Path: DefaultPath,
99+
Op: "open",
100+
}),
101+
}))
95102
})
96103

97104
It("should fail if unable to identify the version of the file at the default path", func() {
98105
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+unversionedFile), os.ModePerm)).To(Succeed())
99106

100107
err := s.Load()
101108
Expect(err).To(HaveOccurred())
102-
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
109+
Expect(err).To(MatchError(store.LoadError{
110+
Err: fmt.Errorf("unable to determine config version: %w",
111+
fmt.Errorf("error unmarshaling JSON: %w",
112+
errors.New("while decoding JSON: project version is empty"),
113+
),
114+
),
115+
}))
103116
})
104117

105118
It("should fail if unable to create a Config for the version of the file at the default path", func() {
106119
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+nonexistentVersionFile), os.ModePerm)).To(Succeed())
107120

108121
err := s.Load()
109122
Expect(err).To(HaveOccurred())
110-
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
123+
Expect(err).To(MatchError(store.LoadError{
124+
Err: fmt.Errorf("unable to create config for version %q: %w", "1-alpha", config.UnsupportedVersionError{
125+
Version: config.Version{Number: 1, Stage: 2},
126+
}),
127+
}))
111128
})
112129

113130
It("should fail if unable to unmarshal the file at the default path", func() {
114131
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+wrongFile), os.ModePerm)).To(Succeed())
115132

116133
err := s.Load()
117134
Expect(err).To(HaveOccurred())
118-
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
135+
Expect(err).To(MatchError(store.LoadError{
136+
Err: fmt.Errorf("unable to create config for version %q: %w", "2", config.UnsupportedVersionError{
137+
Version: config.Version{
138+
Number: 2,
139+
Stage: 0,
140+
},
141+
}),
142+
}))
119143
})
120144
})
121145

@@ -133,31 +157,53 @@ layout: ""
133157
It("should fail if no file exists at the specified path", func() {
134158
err := s.LoadFrom(path)
135159
Expect(err).To(HaveOccurred())
136-
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
160+
Expect(err).To(MatchError(store.LoadError{
161+
Err: fmt.Errorf("unable to read %q file: %w", path, &os.PathError{
162+
Err: os.ErrNotExist,
163+
Path: path,
164+
Op: "open",
165+
}),
166+
}))
137167
})
138168

139169
It("should fail if unable to identify the version of the file at the specified path", func() {
140170
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+unversionedFile), os.ModePerm)).To(Succeed())
141171

142172
err := s.LoadFrom(path)
143173
Expect(err).To(HaveOccurred())
144-
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
174+
Expect(err).To(MatchError(store.LoadError{
175+
Err: fmt.Errorf("unable to determine config version: %w",
176+
fmt.Errorf("error unmarshaling JSON: %w",
177+
errors.New("while decoding JSON: project version is empty"),
178+
),
179+
),
180+
}))
145181
})
146182

147183
It("should fail if unable to create a Config for the version of the file at the specified path", func() {
148184
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+nonexistentVersionFile), os.ModePerm)).To(Succeed())
149185

150186
err := s.LoadFrom(path)
151187
Expect(err).To(HaveOccurred())
152-
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
188+
Expect(err).To(MatchError(store.LoadError{
189+
Err: fmt.Errorf("unable to create config for version %q: %w", "1-alpha", config.UnsupportedVersionError{
190+
Version: config.Version{Number: 1, Stage: 2},
191+
}),
192+
}))
153193
})
154194

155195
It("should fail if unable to unmarshal the file at the specified path", func() {
156196
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+wrongFile), os.ModePerm)).To(Succeed())
157197

158198
err := s.LoadFrom(path)
159199
Expect(err).To(HaveOccurred())
160-
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
200+
Expect(err).To(MatchError(store.LoadError{
201+
Err: fmt.Errorf("unable to create config for version %q: %w", "2", config.UnsupportedVersionError{
202+
Version: config.Version{
203+
Number: 2,
204+
},
205+
}),
206+
}))
161207
})
162208
})
163209

@@ -184,7 +230,9 @@ layout: ""
184230
It("should fail for an empty config", func() {
185231
err := s.Save()
186232
Expect(err).To(HaveOccurred())
187-
Expect(errors.As(err, &store.SaveError{})).To(BeTrue())
233+
Expect(err).To(MatchError(store.SaveError{
234+
Err: errors.New("undefined config, use one of the initializers: New, Load, LoadFrom"),
235+
}))
188236
})
189237

190238
It("should fail for a pre-existent file that must not exist", func() {
@@ -194,7 +242,9 @@ layout: ""
194242

195243
err := s.Save()
196244
Expect(err).To(HaveOccurred())
197-
Expect(errors.As(err, &store.SaveError{})).To(BeTrue())
245+
Expect(err).To(MatchError(store.SaveError{
246+
Err: fmt.Errorf("configuration already exists in %q", DefaultPath),
247+
}))
198248
})
199249
})
200250

@@ -221,7 +271,9 @@ layout: ""
221271
It("should fail for an empty config", func() {
222272
err := s.SaveTo(path)
223273
Expect(err).To(HaveOccurred())
224-
Expect(errors.As(err, &store.SaveError{})).To(BeTrue())
274+
Expect(err).To(MatchError(store.SaveError{
275+
Err: errors.New("undefined config, use one of the initializers: New, Load, LoadFrom"),
276+
}))
225277
})
226278

227279
It("should fail for a pre-existent file that must not exist", func() {
@@ -231,7 +283,9 @@ layout: ""
231283

232284
err := s.SaveTo(path)
233285
Expect(err).To(HaveOccurred())
234-
Expect(errors.As(err, &store.SaveError{})).To(BeTrue())
286+
Expect(err).To(MatchError(store.SaveError{
287+
Err: fmt.Errorf("configuration already exists in %q", path),
288+
}))
235289
})
236290
})
237291
})

pkg/config/v3/config_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package v3
1818

1919
import (
20-
"errors"
2120
"sort"
2221
"testing"
2322

@@ -407,13 +406,13 @@ var _ = Describe("Cfg", func() {
407406
It("DecodePluginConfig should fail for no plugin config object", func() {
408407
err := c0.DecodePluginConfig(key, &pluginCfg)
409408
Expect(err).To(HaveOccurred())
410-
Expect(errors.As(err, &config.PluginKeyNotFoundError{})).To(BeTrue())
409+
Expect(err).To(MatchError(config.PluginKeyNotFoundError{Key: key}))
411410
})
412411

413412
It("DecodePluginConfig should fail to retrieve data from a non-existent plugin", func() {
414413
err := c1.DecodePluginConfig("plugin-y", &pluginCfg)
415414
Expect(err).To(HaveOccurred())
416-
Expect(errors.As(err, &config.PluginKeyNotFoundError{})).To(BeTrue())
415+
Expect(err).To(MatchError(config.PluginKeyNotFoundError{Key: "plugin-y"}))
417416
})
418417

419418
DescribeTable("DecodePluginConfig should retrieve the plugin data correctly",

pkg/machinery/scaffold_test.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
. "github.com/onsi/ginkgo/v2"
2222
. "github.com/onsi/gomega"
2323
"github.com/spf13/afero"
24-
2524
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
2625
"sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
2726
)
@@ -168,32 +167,32 @@ var _ = Describe("Scaffold", func() {
168167
)
169168

170169
DescribeTable("file builders related errors",
171-
func(setup func() (interface{}, []Builder)) {
170+
func(setup func() (error, []Builder)) {
172171
errType, files := setup()
173172

174173
err := s.Execute(files...)
175174

176175
Expect(err).To(HaveOccurred())
177-
Expect(errors.As(err, &errType)).To(BeTrue())
176+
Expect(err).To(MatchError(errType))
178177
},
179-
Entry("should fail if unable to validate a file builder", func() (interface{}, []Builder) {
180-
return &ValidateError{}, []Builder{
178+
Entry("should fail if unable to validate a file builder", func() (error, []Builder) {
179+
return ValidateError{testErr}, []Builder{
181180
fakeRequiresValidation{validateErr: testErr},
182181
}
183182
}),
184-
Entry("should fail if unable to set default values for a template", func() (interface{}, []Builder) {
185-
return &SetTemplateDefaultsError{}, []Builder{
183+
Entry("should fail if unable to set default values for a template", func() (error, []Builder) {
184+
return SetTemplateDefaultsError{testErr}, []Builder{
186185
&fakeTemplate{err: testErr},
187186
}
188187
}),
189-
Entry("should fail if an unexpected previous model is found", func() (interface{}, []Builder) {
190-
return &ModelAlreadyExistsError{}, []Builder{
188+
Entry("should fail if an unexpected previous model is found", func() (error, []Builder) {
189+
return ModelAlreadyExistsError{path: path}, []Builder{
191190
&fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
192191
&fakeTemplate{fakeBuilder: fakeBuilder{path: path, ifExistsAction: Error}},
193192
}
194193
}),
195-
Entry("should fail if behavior if-exists-action is not defined", func() (interface{}, []Builder) {
196-
return &UnknownIfExistsActionError{}, []Builder{
194+
Entry("should fail if behavior if-exists-action is not defined", func() (error, []Builder) {
195+
return UnknownIfExistsActionError{path: path, ifExistsAction: -1}, []Builder{
197196
&fakeTemplate{fakeBuilder: fakeBuilder{path: path}},
198197
&fakeTemplate{fakeBuilder: fakeBuilder{path: path, ifExistsAction: -1}},
199198
}
@@ -415,20 +414,20 @@ func init() {
415414
)
416415

417416
DescribeTable("insert strings related errors",
418-
func(errType interface{}, files ...Builder) {
417+
func(errType error, files ...Builder) {
419418
Expect(afero.WriteFile(s.fs, path, []byte{}, 0o666)).To(Succeed())
420419

421420
err := s.Execute(files...)
422421
Expect(err).To(HaveOccurred())
423-
Expect(errors.As(err, &errType)).To(BeTrue())
422+
Expect(err).To(MatchError(errType))
424423
},
425424
Entry("should fail if inserting into a model that fails when a file exists and it does exist",
426-
&FileAlreadyExistsError{},
425+
FileAlreadyExistsError{path: "filename"},
427426
&fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: Error}},
428427
fakeInserter{fakeBuilder: fakeBuilder{path: "filename"}},
429428
),
430429
Entry("should fail if inserting into a model with unknown behavior if the file exists and it does exist",
431-
&UnknownIfExistsActionError{},
430+
UnknownIfExistsActionError{path: "filename", ifExistsAction: -1},
432431
&fakeTemplate{fakeBuilder: fakeBuilder{path: "filename", ifExistsAction: -1}},
433432
fakeInserter{fakeBuilder: fakeBuilder{path: "filename"}},
434433
),
@@ -467,7 +466,7 @@ func init() {
467466
body: content,
468467
})
469468
Expect(err).To(HaveOccurred())
470-
Expect(errors.As(err, &FileAlreadyExistsError{})).To(BeTrue())
469+
Expect(err).To(MatchError(FileAlreadyExistsError{path: path}))
471470
})
472471
})
473472
})

0 commit comments

Comments
 (0)