diff options
author | Sebastiaan van Stijn <github@gone.nl> | 2022-11-14 16:04:53 +0100 |
---|---|---|
committer | Sebastiaan van Stijn <github@gone.nl> | 2023-03-06 13:01:34 +0100 |
commit | 669940065fdcf271a94b611ddff99393564576b5 (patch) | |
tree | 6f51910d3c5b4b80842abb56ebe95889c77fd277 /reference | |
parent | 520dc12c0e8106fbfa61bcc01a3d09fa5e8c888a (diff) | |
download | docker-669940065fdcf271a94b611ddff99393564576b5.tar.gz |
reference: use t.TempDir() for tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Diffstat (limited to 'reference')
-rw-r--r-- | reference/store_test.go | 46 |
1 files changed, 12 insertions, 34 deletions
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") |