-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #511 from restic/cleanups
Cleanups and test functions
- Loading branch information
Showing
13 changed files
with
397 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package repository | ||
|
||
import ( | ||
"os" | ||
"restic/backend" | ||
"restic/backend/local" | ||
"restic/backend/mem" | ||
"testing" | ||
) | ||
|
||
// TestBackend returns a fully configured in-memory backend. | ||
func TestBackend(t testing.TB) (be backend.Backend, cleanup func()) { | ||
return mem.New(), func() {} | ||
} | ||
|
||
// TestPassword is used for all repositories created by the Test* functions. | ||
const TestPassword = "geheim" | ||
|
||
// TestRepositoryWithBackend returns a repository initialized with a test | ||
// password. If be is nil, an in-memory backend is used. | ||
func TestRepositoryWithBackend(t testing.TB, be backend.Backend) (r *Repository, cleanup func()) { | ||
var beCleanup func() | ||
if be == nil { | ||
be, beCleanup = TestBackend(t) | ||
} | ||
|
||
r = New(be) | ||
|
||
err := r.Init(TestPassword) | ||
if err != nil { | ||
t.Fatalf("TestRepopository(): initialize repo failed: %v", err) | ||
} | ||
|
||
return r, func() { | ||
if beCleanup != nil { | ||
beCleanup() | ||
} | ||
} | ||
} | ||
|
||
// TestRepository returns a repository initialized with a test password on an | ||
// in-memory backend. When the environment variable RESTIC_TEST_REPO is set to | ||
// a non-existing directory, a local backend is created there and this is used | ||
// instead. The directory is not removed. | ||
func TestRepository(t testing.TB) (r *Repository, cleanup func()) { | ||
dir := os.Getenv("RESTIC_TEST_REPO") | ||
if dir != "" { | ||
_, err := os.Stat(dir) | ||
if err != nil { | ||
be, err := local.Create(dir) | ||
if err != nil { | ||
t.Fatalf("error creating local backend at %v: %v", dir, err) | ||
} | ||
return TestRepositoryWithBackend(t, be) | ||
} | ||
|
||
if err == nil { | ||
t.Logf("directory at %v already exists, using mem backend", dir) | ||
} | ||
} | ||
|
||
return TestRepositoryWithBackend(t, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.