@@ -18,6 +18,7 @@ package yaml
1818
1919import (
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})
0 commit comments