From 58cf16da45082f0b013c79bf055a3d6b3c99eab9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 25 Feb 2022 15:29:30 +0100 Subject: [PATCH] cli/manifest: remove deprecated io/ioutil and use t.TempDir() Signed-off-by: Sebastiaan van Stijn --- cli/manifest/store/store.go | 9 ++++--- cli/manifest/store/store_test.go | 41 ++++++++++---------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/cli/manifest/store/store.go b/cli/manifest/store/store.go index 1fd0207b34..dd0c86e526 100644 --- a/cli/manifest/store/store.go +++ b/cli/manifest/store/store.go @@ -3,7 +3,6 @@ package store import ( "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -47,7 +46,7 @@ func (s *fsStore) Get(listRef reference.Reference, manifest reference.Reference) } func (s *fsStore) getFromFilename(ref reference.Reference, filename string) (types.ImageManifest, error) { - bytes, err := ioutil.ReadFile(filename) + bytes, err := os.ReadFile(filename) switch { case os.IsNotExist(err): return types.ImageManifest{}, newNotFoundError(ref.String()) @@ -112,7 +111,7 @@ func (s *fsStore) GetList(listRef reference.Reference) ([]types.ImageManifest, e // listManifests stored in a transaction func (s *fsStore) listManifests(transaction string) ([]string, error) { transactionDir := filepath.Join(s.root, makeFilesafeName(transaction)) - fileInfos, err := ioutil.ReadDir(transactionDir) + fileInfos, err := os.ReadDir(transactionDir) switch { case os.IsNotExist(err): return nil, nil @@ -120,7 +119,7 @@ func (s *fsStore) listManifests(transaction string) ([]string, error) { return nil, err } - filenames := []string{} + filenames := make([]string, 0, len(fileInfos)) for _, info := range fileInfos { filenames = append(filenames, info.Name()) } @@ -137,7 +136,7 @@ func (s *fsStore) Save(listRef reference.Reference, manifest reference.Reference if err != nil { return err } - return ioutil.WriteFile(filename, bytes, 0644) + return os.WriteFile(filename, bytes, 0644) } func (s *fsStore) createManifestListDirectory(transaction string) error { diff --git a/cli/manifest/store/store_test.go b/cli/manifest/store/store_test.go index a89f3980ba..5fc05a6031 100644 --- a/cli/manifest/store/store_test.go +++ b/cli/manifest/store/store_test.go @@ -1,7 +1,6 @@ package store import ( - "io/ioutil" "os" "testing" @@ -29,41 +28,31 @@ func ref(name string) fakeRef { } func sref(t *testing.T, name string) *types.SerializableNamed { + t.Helper() named, err := reference.ParseNamed("example.com/" + name) assert.NilError(t, err) return &types.SerializableNamed{Named: named} } -func newTestStore(t *testing.T) (Store, func()) { - tmpdir, err := ioutil.TempDir("", "manifest-store-test") - assert.NilError(t, err) - - return NewStore(tmpdir), func() { os.RemoveAll(tmpdir) } -} - -func getFiles(t *testing.T, store Store) []os.FileInfo { - infos, err := ioutil.ReadDir(store.(*fsStore).root) - assert.NilError(t, err) - return infos -} - func TestStoreRemove(t *testing.T) { - store, cleanup := newTestStore(t) - defer cleanup() - + tmpDir := t.TempDir() + store := NewStore(tmpDir) listRef := ref("list") data := types.ImageManifest{Ref: sref(t, "abcdef")} assert.NilError(t, store.Save(listRef, ref("manifest"), data)) - assert.Assert(t, is.Len(getFiles(t, store), 1)) + + files, err := os.ReadDir(tmpDir) + assert.NilError(t, err) + assert.Assert(t, is.Len(files, 1)) assert.Check(t, store.Remove(listRef)) - assert.Check(t, is.Len(getFiles(t, store), 0)) + files, err = os.ReadDir(tmpDir) + assert.NilError(t, err) + assert.Check(t, is.Len(files, 0)) } func TestStoreSaveAndGet(t *testing.T) { - store, cleanup := newTestStore(t) - defer cleanup() - + store := NewStore(t.TempDir()) listRef := ref("list") data := types.ImageManifest{Ref: sref(t, "abcdef")} err := store.Save(listRef, ref("exists"), data) @@ -112,9 +101,7 @@ var cmpReferenceNamed = cmp.Transformer("namedref", func(r reference.Named) stri }) func TestStoreGetList(t *testing.T) { - store, cleanup := newTestStore(t) - defer cleanup() - + store := NewStore(t.TempDir()) listRef := ref("list") first := types.ImageManifest{Ref: sref(t, "first")} assert.NilError(t, store.Save(listRef, ref("first"), first)) @@ -127,9 +114,7 @@ func TestStoreGetList(t *testing.T) { } func TestStoreGetListDoesNotExist(t *testing.T) { - store, cleanup := newTestStore(t) - defer cleanup() - + store := NewStore(t.TempDir()) listRef := ref("list") _, err := store.GetList(listRef) assert.Error(t, err, "No such manifest: list")