From 619453a7bd944601b57c0902ac2a48041b3c2c4f Mon Sep 17 00:00:00 2001 From: Florent Messa Date: Mon, 3 Jul 2023 18:15:15 +0200 Subject: [PATCH] chore: remove io/ioutil usage --- Makefile | 3 +++ processor.go | 5 +---- processor_test.go | 24 +++++++++++------------- tests/utils.go | 4 ++-- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 769fe148..6ed236f9 100644 --- a/Makefile +++ b/Makefile @@ -67,3 +67,6 @@ docker-build: @(docker build -t picfit-builder -f Dockerfile.build .) @(mkdir -p $(BIN_DIR)) @(docker run --rm -v $(BIN_DIR):$(APP_DIR)/bin picfit-builder) + +lint: + golangci-lint run . diff --git a/processor.go b/processor.go index 7223fecc..348ba098 100644 --- a/processor.go +++ b/processor.go @@ -413,10 +413,7 @@ func (p *Processor) KeyExists(ctx context.Context, key string) (bool, error) { func (p *Processor) FileExists(ctx context.Context, name string) bool { _, err := p.sourceStorage.Stat(ctx, name) - if errors.Is(err, gostorages.ErrNotExist) { - return false - } - return true + return !errors.Is(err, gostorages.ErrNotExist) } func (p *Processor) OpenFile(ctx context.Context, name string) (io.ReadCloser, error) { diff --git a/processor_test.go b/processor_test.go index 5a122ec1..4150a365 100644 --- a/processor_test.go +++ b/processor_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "mime" "mime/multipart" "net/http" @@ -185,7 +184,7 @@ func TestUploadHandler(t *testing.T) { assert.Nil(t, err) - fileContent, err := ioutil.ReadAll(f) + fileContent, err := io.ReadAll(f) assert.Nil(t, err) @@ -193,7 +192,8 @@ func TestUploadHandler(t *testing.T) { assert.Nil(t, err) - writer.Write(fileContent) + _, err = writer.Write(fileContent) + assert.NoError(t, err) if err := w.Close(); err != nil { t.Fatal(err) @@ -223,28 +223,26 @@ func TestUploadHandler(t *testing.T) { } func TestDeleteHandler(t *testing.T) { - tmp, err := ioutil.TempDir("", tests.RandString(10)) - - assert.Nil(t, err) + tmp := os.TempDir() tmpSrcStorage := filepath.Join(tmp, "src") tmpDstStorage := filepath.Join(tmp, "dst") - os.MkdirAll(tmpSrcStorage, 0755) - os.MkdirAll(tmpDstStorage, 0755) + assert.NoError(t, os.MkdirAll(tmpSrcStorage, 0755)) + assert.NoError(t, os.MkdirAll(tmpDstStorage, 0755)) - img, err := ioutil.ReadFile("tests/fixtures/schwarzy.jpg") + img, err := os.ReadFile("tests/fixtures/schwarzy.jpg") assert.Nil(t, err) // copy 5 images to src storage for i := 0; i < 5; i++ { fn := fmt.Sprintf("image%d.jpg", i+1) - err = ioutil.WriteFile(filepath.Join(tmpSrcStorage, fn), img, 0644) + err = os.WriteFile(filepath.Join(tmpSrcStorage, fn), img, 0644) assert.Nil(t, err) } checkDirCount := func(dir string, count int, context string) { - dircontents, err := ioutil.ReadDir(dir) + dircontents, err := os.ReadDir(dir) assert.Nil(t, err) assert.Equal(t, count, len(dircontents), "%s (%s)", context, dir) } @@ -360,12 +358,12 @@ func TestStorageApplicationWithPath(t *testing.T) { assert.Nil(t, err) defer f.Close() - body, err := ioutil.ReadAll(f) + body, err := io.ReadAll(f) assert.Nil(t, err) // We store the image at the tmp location to access it // with the SourceStorage - ioutil.WriteFile(path.Join(tmp, "avatar.png"), body, 0755) + assert.NoError(t, os.WriteFile(path.Join(tmp, "avatar.png"), body, 0755)) var ( redisHost = os.Getenv("REDIS_HOST") diff --git a/tests/utils.go b/tests/utils.go index 15ccbc90..e9fb2da6 100644 --- a/tests/utils.go +++ b/tests/utils.go @@ -2,7 +2,7 @@ package tests import ( "context" - "io/ioutil" + "io" "math/rand" "net/http" "net/http/httptest" @@ -114,7 +114,7 @@ func NewImageServer() *httptest.Server { if err != nil { w.WriteHeader(500) } else { - bytes, _ := ioutil.ReadAll(f) + bytes, _ := io.ReadAll(f) contentType, _ := image.MimetypeDetectorExtension(r.URL)