@@ -18,6 +18,7 @@ package yaml
18
18
19
19
import (
20
20
"errors"
21
+ "fmt"
21
22
"os"
22
23
"testing"
23
24
@@ -91,31 +92,54 @@ layout: ""
91
92
It ("should fail if no file exists at the default path" , func () {
92
93
err := s .Load ()
93
94
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
+ }))
95
102
})
96
103
97
104
It ("should fail if unable to identify the version of the file at the default path" , func () {
98
105
Expect (afero .WriteFile (s .fs , DefaultPath , []byte (commentStr + unversionedFile ), os .ModePerm )).To (Succeed ())
99
106
100
107
err := s .Load ()
101
108
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
+ }))
103
116
})
104
117
105
118
It ("should fail if unable to create a Config for the version of the file at the default path" , func () {
106
119
Expect (afero .WriteFile (s .fs , DefaultPath , []byte (commentStr + nonexistentVersionFile ), os .ModePerm )).To (Succeed ())
107
120
108
121
err := s .Load ()
109
122
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
+ }))
111
128
})
112
129
113
130
It ("should fail if unable to unmarshal the file at the default path" , func () {
114
131
Expect (afero .WriteFile (s .fs , DefaultPath , []byte (commentStr + wrongFile ), os .ModePerm )).To (Succeed ())
115
132
116
133
err := s .Load ()
117
134
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
+ }))
119
143
})
120
144
})
121
145
@@ -133,31 +157,53 @@ layout: ""
133
157
It ("should fail if no file exists at the specified path" , func () {
134
158
err := s .LoadFrom (path )
135
159
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
+ }))
137
167
})
138
168
139
169
It ("should fail if unable to identify the version of the file at the specified path" , func () {
140
170
Expect (afero .WriteFile (s .fs , path , []byte (commentStr + unversionedFile ), os .ModePerm )).To (Succeed ())
141
171
142
172
err := s .LoadFrom (path )
143
173
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
+ }))
145
181
})
146
182
147
183
It ("should fail if unable to create a Config for the version of the file at the specified path" , func () {
148
184
Expect (afero .WriteFile (s .fs , path , []byte (commentStr + nonexistentVersionFile ), os .ModePerm )).To (Succeed ())
149
185
150
186
err := s .LoadFrom (path )
151
187
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
+ }))
153
193
})
154
194
155
195
It ("should fail if unable to unmarshal the file at the specified path" , func () {
156
196
Expect (afero .WriteFile (s .fs , path , []byte (commentStr + wrongFile ), os .ModePerm )).To (Succeed ())
157
197
158
198
err := s .LoadFrom (path )
159
199
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
+ }))
161
207
})
162
208
})
163
209
@@ -184,7 +230,9 @@ layout: ""
184
230
It ("should fail for an empty config" , func () {
185
231
err := s .Save ()
186
232
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
+ }))
188
236
})
189
237
190
238
It ("should fail for a pre-existent file that must not exist" , func () {
@@ -194,7 +242,9 @@ layout: ""
194
242
195
243
err := s .Save ()
196
244
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
+ }))
198
248
})
199
249
})
200
250
@@ -221,7 +271,9 @@ layout: ""
221
271
It ("should fail for an empty config" , func () {
222
272
err := s .SaveTo (path )
223
273
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
+ }))
225
277
})
226
278
227
279
It ("should fail for a pre-existent file that must not exist" , func () {
@@ -231,7 +283,9 @@ layout: ""
231
283
232
284
err := s .SaveTo (path )
233
285
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
+ }))
235
289
})
236
290
})
237
291
})
0 commit comments