From 030f08a410992b863a303ccbc4be10b1473477c2 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 13 Jun 2015 12:35:19 +0200 Subject: [PATCH] Remove flags from tests --- backend/backend_test.go | 2 +- backend/local_test.go | 2 +- backend/sftp_test.go | 4 ++-- cmd/restic/integration_test.go | 8 +++---- node_test.go | 4 ++-- test/backend.go | 40 +++++++++++++++++++++++++++------- tree_test.go | 4 ++-- 7 files changed, 44 insertions(+), 20 deletions(-) diff --git a/backend/backend_test.go b/backend/backend_test.go index da5606551..862d85657 100644 --- a/backend/backend_test.go +++ b/backend/backend_test.go @@ -102,7 +102,7 @@ func testBackend(b backend.Backend, t *testing.T) { } // remove content if requested - if *TestCleanup { + if TestCleanup { for _, test := range TestStrings { id, err := backend.ParseID(test.id) OK(t, err) diff --git a/backend/local_test.go b/backend/local_test.go index 9bc903f0a..24c7091bd 100644 --- a/backend/local_test.go +++ b/backend/local_test.go @@ -22,7 +22,7 @@ func setupLocalBackend(t *testing.T) *local.Local { } func teardownLocalBackend(t *testing.T, b *local.Local) { - if !*TestCleanup { + if !TestCleanup { t.Logf("leaving local backend at %s\n", b.Location()) return } diff --git a/backend/sftp_test.go b/backend/sftp_test.go index 15ab04ead..aa262176d 100644 --- a/backend/sftp_test.go +++ b/backend/sftp_test.go @@ -25,7 +25,7 @@ func setupSFTPBackend(t *testing.T) *sftp.SFTP { } func teardownSFTPBackend(t *testing.T, b *sftp.SFTP) { - if !*TestCleanup { + if !TestCleanup { t.Logf("leaving backend at %s\n", b.Location()) return } @@ -35,7 +35,7 @@ func teardownSFTPBackend(t *testing.T, b *sftp.SFTP) { } func TestSFTPBackend(t *testing.T) { - if !*RunIntegrationTest { + if !RunIntegrationTest { t.Skip("integration tests disabled, use `-test.integration` to enable") } diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 63c74691e..e602b9113 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -17,7 +17,7 @@ import ( var TestDataFile = flag.String("test.datafile", "", `specify tar.gz file with test data to backup and restore (required for integration test)`) func setupTempdir(t testing.TB) (tempdir string) { - tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-") + tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") OK(t, err) return tempdir @@ -28,11 +28,11 @@ func configureRestic(t testing.TB, tempdir string) { opts.Repo = filepath.Join(tempdir, "repo") opts.Quiet = true - opts.password = *TestPassword + opts.password = TestPassword } func cleanupTempdir(t testing.TB, tempdir string) { - if !*TestCleanup { + if !TestCleanup { t.Logf("leaving temporary directory %v used for test", tempdir) return } @@ -116,7 +116,7 @@ func cmdFsck(t testing.TB) { } func TestBackup(t *testing.T) { - if !*RunIntegrationTest { + if !RunIntegrationTest { t.Skip("integration tests disabled, use `-test.integration` to enable") } diff --git a/node_test.go b/node_test.go index 33d222d78..ff4d4d721 100644 --- a/node_test.go +++ b/node_test.go @@ -103,11 +103,11 @@ var nodeTests = []restic.Node{ } func TestNodeRestoreAt(t *testing.T) { - tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-") + tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") OK(t, err) defer func() { - if *TestCleanup { + if TestCleanup { OK(t, os.RemoveAll(tempdir)) } else { t.Logf("leaving tempdir at %v", tempdir) diff --git a/test/backend.go b/test/backend.go index 8abd28582..9baa3f220 100644 --- a/test/backend.go +++ b/test/backend.go @@ -1,8 +1,9 @@ package test_helper import ( - "flag" + "fmt" "io/ioutil" + "os" "path/filepath" "testing" @@ -13,14 +14,37 @@ import ( ) var ( - TestPassword = flag.String("test.password", "geheim", `use this password for repositories created during tests (default: "geheim")`) - TestCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)") - TestTempDir = flag.String("test.tempdir", "", "use this directory for temporary storage (default: system temp dir)") - RunIntegrationTest = flag.Bool("test.integration", false, "run integration tests (default: false)") + TestPassword = getStringVar("RESTIC_TEST_PASSWORD", "geheim") + TestCleanup = getBoolVar("RESTIC_TEST_CLEANUP", true) + TestTempDir = getStringVar("RESTIC_TEST_TMPDIR", "") + RunIntegrationTest = getBoolVar("RESTIC_TEST_INTEGRATION", true) ) +func getStringVar(name, defaultValue string) string { + if e := os.Getenv(name); e != "" { + return e + } + + return defaultValue +} + +func getBoolVar(name string, defaultValue bool) bool { + if e := os.Getenv(name); e != "" { + switch e { + case "1": + return true + case "0": + return false + default: + fmt.Fprintf(os.Stderr, "invalid value for variable %q, using default\n", name) + } + } + + return defaultValue +} + func SetupRepo(t testing.TB) *repository.Repository { - tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-") + tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") OK(t, err) // create repository below temp dir @@ -28,12 +52,12 @@ func SetupRepo(t testing.TB) *repository.Repository { OK(t, err) repo := repository.New(b) - OK(t, repo.Init(*TestPassword)) + OK(t, repo.Init(TestPassword)) return repo } func TeardownRepo(t testing.TB, repo *repository.Repository) { - if !*TestCleanup { + if !TestCleanup { l := repo.Backend().(*local.Local) t.Logf("leaving local backend at %s\n", l.Location()) return diff --git a/tree_test.go b/tree_test.go index 6a1984d48..b46f01b0a 100644 --- a/tree_test.go +++ b/tree_test.go @@ -22,7 +22,7 @@ var testFiles = []struct { } func createTempDir(t *testing.T) string { - tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-") + tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") OK(t, err) for _, test := range testFiles { @@ -49,7 +49,7 @@ func createTempDir(t *testing.T) string { func TestTree(t *testing.T) { dir := createTempDir(t) defer func() { - if *TestCleanup { + if TestCleanup { OK(t, os.RemoveAll(dir)) } }()