add expected errors tests, fix ErrFolderNotFound not being sent on S3.

This commit is contained in:
Shane C. 2024-11-21 12:05:00 -05:00
parent ba32206f35
commit 0e3407c7cc
Signed by: Shane C.
GPG key ID: E46B5FEA35B22FF9
3 changed files with 71 additions and 4 deletions

37
local_errors_test.go Normal file
View file

@ -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)
}

29
s3_errors_test.go Normal file
View file

@ -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)
}

View file

@ -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 {