Skip to content

Commit d68510b

Browse files
committed
fix(backend): reuse ensure init feature in backend validation
1 parent 3651c7c commit d68510b

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

internal/backend.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,16 @@ func (b Backend) validate() error {
131131
return err
132132
}
133133
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+
135140
cmd := []string{"check"}
136141
cmd = append(cmd, combineBackendOptions("check", b)...)
137142
_, _, 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
148144
}
149145

150146
// EnsureInit initializes the backend if it is not already initialized

internal/backend_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,35 @@ func TestValidate(t *testing.T) {
266266
})
267267
}
268268

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+
269298
func TestEnsureInit(t *testing.T) {
270299
// This is normally initialized by the cobra commands but they don't run in
271300
// this test so we do it ourselves.

0 commit comments

Comments
 (0)