From 015e726698c8c91c7a45acd3538a0eb3986e2360 Mon Sep 17 00:00:00 2001 From: Cian John Date: Fri, 9 Sep 2022 20:56:33 +0800 Subject: [PATCH] Fix: unexpected querying all files when deleting an empty folder --- models/file.go | 4 ++++ models/file_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/models/file.go b/models/file.go index 34dad78617..8171054b02 100644 --- a/models/file.go +++ b/models/file.go @@ -186,6 +186,10 @@ func RemoveFilesWithSoftLinks(files []File) ([]File, error) { // 结果值 filteredFiles := make([]File, 0) + if len(files) == 0 { + return filteredFiles, nil + } + // 查询软链接的文件 var filesWithSoftLinks []File tx := DB diff --git a/models/file_test.go b/models/file_test.go index b421763a90..9563521ae0 100644 --- a/models/file_test.go +++ b/models/file_test.go @@ -257,6 +257,19 @@ func TestFile_GetPolicy(t *testing.T) { } } +func TestRemoveFilesWithSoftLinks_EmptyArg(t *testing.T) { + asserts := assert.New(t) + // 传入空 + { + mock.ExpectQuery("SELECT(.+)files(.+)") + file, err := RemoveFilesWithSoftLinks([]File{}) + asserts.Error(mock.ExpectationsWereMet()) + asserts.NoError(err) + asserts.Equal(len(file), 0) + DB.Find(&File{}) + } +} + func TestRemoveFilesWithSoftLinks(t *testing.T) { asserts := assert.New(t) files := []File{