mirror of
https://github.com/octoleo/restic.git
synced 2024-12-22 10:58:55 +00:00
local: Add test for #1167
It was discovered that restic creates directories when a non-existing directory is specified as a local repository.
This commit is contained in:
parent
3559f9c776
commit
ea017a49c3
@ -35,7 +35,7 @@ func Open(cfg Config) (*Local, error) {
|
||||
|
||||
be := &Local{Config: cfg, Layout: l}
|
||||
|
||||
// create paths for data and refs. MkdirAll does nothing if the directory already exists.
|
||||
// if data dir exists, make sure that all subdirs also exist
|
||||
for _, d := range be.Paths() {
|
||||
err := fs.MkdirAll(d, backend.Modes.Dir)
|
||||
if err != nil {
|
||||
|
@ -2,6 +2,8 @@ package local_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/backend/local"
|
||||
@ -59,3 +61,57 @@ func TestBackend(t *testing.T) {
|
||||
func BenchmarkBackend(t *testing.B) {
|
||||
newTestSuite(t).RunBenchmarks(t)
|
||||
}
|
||||
|
||||
func readdirnames(t testing.TB, dir string) []string {
|
||||
f, err := os.Open(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
entries, err := f.Readdirnames(-1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return entries
|
||||
}
|
||||
|
||||
func empty(t testing.TB, dir string) {
|
||||
entries := readdirnames(t, dir)
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("directory %v is not empty, contains: %v", dir, entries)
|
||||
}
|
||||
}
|
||||
|
||||
func openclose(t testing.TB, dir string) {
|
||||
cfg := local.Config{Path: dir}
|
||||
|
||||
be, err := local.Open(cfg)
|
||||
if err != nil {
|
||||
t.Logf("Open returned error %v", err)
|
||||
}
|
||||
|
||||
if be != nil {
|
||||
err = be.Close()
|
||||
if err != nil {
|
||||
t.Logf("Close returned error %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenNotExistingDirectory(t *testing.T) {
|
||||
dir, cleanup := TempDir(t)
|
||||
defer cleanup()
|
||||
|
||||
// local.Open must not create any files dirs in the repo
|
||||
openclose(t, filepath.Join(dir, "repo"))
|
||||
empty(t, dir)
|
||||
|
||||
openclose(t, dir)
|
||||
empty(t, dir)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user