From acc98d89b7e2bd16ed5abb111ff5d7e2c042f62c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20L=C3=B3pez?= Date: Thu, 10 Sep 2020 17:26:03 -0700 Subject: [PATCH] Trivial test nits (#602) * Ensure other repo is closed * Prefer testlogging.Context in tests * Prefer T.TempDir() in repotesting.Environment.Setup() --- internal/repotesting/repotesting.go | 23 ++++++----------------- snapshot/policy/policy_manager_test.go | 5 ++--- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/internal/repotesting/repotesting.go b/internal/repotesting/repotesting.go index 949d4c06a..c0d25956f 100644 --- a/internal/repotesting/repotesting.go +++ b/internal/repotesting/repotesting.go @@ -3,7 +3,6 @@ import ( "context" - "io/ioutil" "os" "path/filepath" "testing" @@ -30,19 +29,9 @@ type Environment struct { // Setup sets up a test environment. func (e *Environment) Setup(t *testing.T, opts ...func(*repo.NewRepositoryOptions)) *Environment { - var err error - ctx := testlogging.Context(t) - - e.configDir, err = ioutil.TempDir("", "") - if err != nil { - t.Fatalf("err: %v", err) - } - - e.storageDir, err = ioutil.TempDir("", "") - if err != nil { - t.Fatalf("err: %v", err) - } + e.configDir = t.TempDir() + e.storageDir = t.TempDir() opt := &repo.NewRepositoryOptions{ BlockFormat: content.FormattingOptions{ @@ -102,10 +91,6 @@ func (e *Environment) Close(ctx context.Context, t *testing.T) { // should be empty, assuming Disconnect was successful t.Errorf("error removing config directory: %v", err) } - - if err := os.RemoveAll(e.storageDir); err != nil { - t.Errorf("error removing storage directory: %v", err) - } } func (e *Environment) configFile() string { @@ -134,6 +119,10 @@ func (e *Environment) MustOpenAnother(t *testing.T) repo.Repository { t.Fatalf("err: %v", err) } + t.Cleanup(func() { + rep2.Close(testlogging.Context(t)) + }) + return rep2 } diff --git a/snapshot/policy/policy_manager_test.go b/snapshot/policy/policy_manager_test.go index 3064b1ba9..f89bbeb15 100644 --- a/snapshot/policy/policy_manager_test.go +++ b/snapshot/policy/policy_manager_test.go @@ -1,7 +1,6 @@ package policy import ( - "context" "encoding/json" "fmt" "sort" @@ -15,7 +14,7 @@ ) func TestPolicyManagerInheritanceTest(t *testing.T) { - ctx := context.Background() + ctx := testlogging.Context(t) var env repotesting.Environment @@ -166,7 +165,7 @@ func defaultPolicyWithKeepDaily(t *testing.T, keepDaily int) *Policy { } func TestPolicyManagerResolvesConflicts(t *testing.T) { - ctx := context.Background() + ctx := testlogging.Context(t) var env repotesting.Environment