From 0e3407c7ccfa801be9abcee89247f79f1120b111 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 21 Nov 2024 12:05:00 -0500 Subject: [PATCH] add expected errors tests, fix ErrFolderNotFound not being sent on S3. --- local_errors_test.go | 37 +++++++++++++++++++++++++++++++++++++ s3_errors_test.go | 29 +++++++++++++++++++++++++++++ storage.go | 9 +++++---- 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 local_errors_test.go create mode 100644 s3_errors_test.go diff --git a/local_errors_test.go b/local_errors_test.go new file mode 100644 index 0000000..af5386b --- /dev/null +++ b/local_errors_test.go @@ -0,0 +1,37 @@ +package fsys + +import ( + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestStorageLocalErrors(t *testing.T) { + + err := os.WriteFile("./test/local/non_folder", []byte("hi"), 0640) + assert.NoError(t, err) + + t.Log("== InvalidPathErr ==") + _, err = New(Config{ + Type: "local", + Path: "./test/local/non_folder", + }) + assert.Error(t, err) + + stor, err := New(Config{ + Type: "local", + Path: "./test/local", + }) + assert.NoError(t, err) + + t.Log("== FileNotExistErr ==") + _, err = stor.Stat("nonexistant") + assert.Error(t, err) + assert.Equal(t, ErrFileNotFound, err) + + t.Log("== FolderNotExistErr ==") + _, err = stor.ReadDir("nonexistant") + assert.Error(t, err) + assert.Equal(t, ErrFolderNotFound, err) + +} diff --git a/s3_errors_test.go b/s3_errors_test.go new file mode 100644 index 0000000..cbd263f --- /dev/null +++ b/s3_errors_test.go @@ -0,0 +1,29 @@ +package fsys + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestStorageS3Errors(t *testing.T) { + + stor, err := New(Config{ + Type: "s3", + S3BucketName: "test", + S3AccessID: "root", + S3AccessKey: "password123", + S3Endpoint: "127.0.0.1:9000", + }) + assert.NoError(t, err) + + t.Log("== FileNotExistErr ==") + _, err = stor.Stat("nonexistant") + assert.Error(t, err) + assert.Equal(t, ErrFileNotFound, err) + + t.Log("== FolderNotExistErr ==") + _, err = stor.ReadDir("nonexistant") + assert.Error(t, err) + assert.Equal(t, ErrFolderNotFound, err) + +} diff --git a/storage.go b/storage.go index 1dbb3f7..d1c5357 100644 --- a/storage.go +++ b/storage.go @@ -326,14 +326,12 @@ func (s *Storage) Move(name string, dest string) error { func (s *Storage) ReadDir(name string) ([]FileInfo, error) { var fileInfo []FileInfo if s.s3Client != nil { + doesExist := false for object := range s.s3Client.ListObjects(s.ctx, s.config.S3BucketName, minio.ListObjectsOptions{ Prefix: name + "/", }) { + doesExist = true if object.Err != nil { - minioErr := minio.ToErrorResponse(object.Err) - if minioErr.Code == "NoSuchKey" { - return nil, ErrFolderNotFound - } return nil, object.Err } fileInfo = append(fileInfo, FileInfo{ @@ -344,6 +342,9 @@ func (s *Storage) ReadDir(name string) ([]FileInfo, error) { IsDir: object.Size == 0, }) } + if !doesExist { + return nil, ErrFolderNotFound + } } else { dirInfo, err := os.ReadDir(filepath.Join(s.config.Path, name)) if err != nil {