File tree 2 files changed +36
-11
lines changed
2 files changed +36
-11
lines changed Original file line number Diff line number Diff line change @@ -131,20 +131,16 @@ func (b Backend) validate() error {
131
131
return err
132
132
}
133
133
options := ExecuteOptions {Envs : env , Silent : true }
134
- // Check if already initialized
134
+
135
+ err = b .EnsureInit ()
136
+ if err != nil {
137
+ return err
138
+ }
139
+
135
140
cmd := []string {"check" }
136
141
cmd = append (cmd , combineBackendOptions ("check" , b )... )
137
142
_ , _ , err = ExecuteResticCommand (options , cmd ... )
138
- if err == nil {
139
- return nil
140
- } else {
141
- // If not initialize
142
- colors .Body .Printf ("Initializing backend \" %s\" ...\n " , b .name )
143
- cmd := []string {"init" }
144
- cmd = append (cmd , combineBackendOptions ("init" , b )... )
145
- _ , _ , err := ExecuteResticCommand (options , cmd ... )
146
- return err
147
- }
143
+ return err
148
144
}
149
145
150
146
// EnsureInit initializes the backend if it is not already initialized
Original file line number Diff line number Diff line change @@ -266,6 +266,35 @@ func TestValidate(t *testing.T) {
266
266
})
267
267
}
268
268
269
+ func TestValidateInitsRepo (t * testing.T ) {
270
+ // This is normally initialized by the cobra commands but they don't run in
271
+ // this test so we do it ourselves.
272
+ flags .RESTIC_BIN = "restic"
273
+
274
+ workDir := t .TempDir ()
275
+
276
+ b := Backend {
277
+ name : "test" ,
278
+ Type : "local" ,
279
+ Path : path .Join (workDir , "backend" ),
280
+ Key : "supersecret" ,
281
+ }
282
+
283
+ config = & Config {Backends : map [string ]Backend {"test" : b }}
284
+ defer func () { config = nil }()
285
+
286
+ // Check should fail because the repo doesn't exist
287
+ err := b .Exec ([]string {"check" })
288
+ assert .Error (t , err )
289
+
290
+ err = b .validate ()
291
+ assert .NoError (t , err )
292
+
293
+ // Check should pass now
294
+ err = b .Exec ([]string {"check" })
295
+ assert .NoError (t , err )
296
+ }
297
+
269
298
func TestEnsureInit (t * testing.T ) {
270
299
// This is normally initialized by the cobra commands but they don't run in
271
300
// this test so we do it ourselves.
You can’t perform that action at this time.
0 commit comments