From 669940065fdcf271a94b611ddff99393564576b5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 14 Nov 2022 16:04:53 +0100 Subject: reference: use t.TempDir() for tests Signed-off-by: Sebastiaan van Stijn --- reference/store_test.go | 46 ++++++++++++---------------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) (limited to 'reference') diff --git a/reference/store_test.go b/reference/store_test.go index c3098ddf0b..0fd473e716 100644 --- a/reference/store_test.go +++ b/reference/store_test.go @@ -28,20 +28,11 @@ var ( ) func TestLoad(t *testing.T) { - jsonFile, err := os.CreateTemp("", "tag-store-test") - if err != nil { - t.Fatalf("error creating temp file: %v", err) - } - defer os.RemoveAll(jsonFile.Name()) - - // Write canned json to the temp file - _, err = jsonFile.Write(marshalledSaveLoadTestCases) - if err != nil { - t.Fatalf("error writing to temp file: %v", err) - } - jsonFile.Close() + jsonFile := filepath.Join(t.TempDir(), "repositories.json") + err := os.WriteFile(jsonFile, marshalledSaveLoadTestCases, 0o666) + assert.NilError(t, err) - store, err := NewReferenceStore(jsonFile.Name()) + store, err := NewReferenceStore(jsonFile) if err != nil { t.Fatalf("error creating tag store: %v", err) } @@ -62,15 +53,11 @@ func TestLoad(t *testing.T) { } func TestSave(t *testing.T) { - jsonFile, err := os.CreateTemp("", "tag-store-test") + jsonFile := filepath.Join(t.TempDir(), "repositories.json") + err := os.WriteFile(jsonFile, []byte(`{}`), 0o666) assert.NilError(t, err) - _, err = jsonFile.Write([]byte(`{}`)) - assert.NilError(t, err) - jsonFile.Close() - defer os.RemoveAll(jsonFile.Name()) - - store, err := NewReferenceStore(jsonFile.Name()) + store, err := NewReferenceStore(jsonFile) if err != nil { t.Fatalf("error creating tag store: %v", err) } @@ -93,7 +80,7 @@ func TestSave(t *testing.T) { } } - jsonBytes, err := os.ReadFile(jsonFile.Name()) + jsonBytes, err := os.ReadFile(jsonFile) if err != nil { t.Fatalf("could not read json file: %v", err) } @@ -104,16 +91,11 @@ func TestSave(t *testing.T) { } func TestAddDeleteGet(t *testing.T) { - jsonFile, err := os.CreateTemp("", "tag-store-test") - if err != nil { - t.Fatalf("error creating temp file: %v", err) - } - _, err = jsonFile.Write([]byte(`{}`)) + jsonFile := filepath.Join(t.TempDir(), "repositories.json") + err := os.WriteFile(jsonFile, []byte(`{}`), 0o666) assert.NilError(t, err) - _ = jsonFile.Close() - defer func() { _ = os.RemoveAll(jsonFile.Name()) }() - store, err := NewReferenceStore(jsonFile.Name()) + store, err := NewReferenceStore(jsonFile) if err != nil { t.Fatalf("error creating tag store: %v", err) } @@ -335,11 +317,7 @@ func TestAddDeleteGet(t *testing.T) { } func TestInvalidTags(t *testing.T) { - tmpDir, err := os.MkdirTemp("", "tag-store-test") - assert.NilError(t, err) - defer os.RemoveAll(tmpDir) - - store, err := NewReferenceStore(filepath.Join(tmpDir, "repositories.json")) + store, err := NewReferenceStore(filepath.Join(t.TempDir(), "repositories.json")) assert.NilError(t, err) id := digest.Digest("sha256:470022b8af682154f57a2163d030eb369549549cba00edc69e1b99b46bb924d6") -- cgit v1.2.1