Skip to content

Commit

Permalink
chore: remove io/ioutil usage
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Jul 3, 2023
1 parent a7ab88b commit 619453a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
5 changes: 1 addition & 4 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 11 additions & 13 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -185,15 +184,16 @@ func TestUploadHandler(t *testing.T) {

assert.Nil(t, err)

fileContent, err := ioutil.ReadAll(f)
fileContent, err := io.ReadAll(f)

assert.Nil(t, err)

writer, err := w.CreateFormFile("data", "avatar.png")

assert.Nil(t, err)

writer.Write(fileContent)
_, err = writer.Write(fileContent)
assert.NoError(t, err)

if err := w.Close(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tests

import (
"context"
"io/ioutil"
"io"
"math/rand"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 619453a

Please sign in to comment.