test: new changes in decompress method

pull/1232/head
HFO4 3 years ago
parent ac78e9db02
commit cb51046305

@ -165,7 +165,7 @@ func (fs *FileSystem) doCompress(ctx context.Context, file *model.File, folder *
}
// Decompress 解压缩给定压缩文件到dst目录
func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error {
func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string) error {
err := fs.ResetFileIfNotExist(ctx, src)
if err != nil {
return err
@ -219,7 +219,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error {
var isZip bool
switch extractor.(type) {
case archiver.Zip:
extractor = archiver.Zip{TextEncoding: "gb18030"}
extractor = archiver.Zip{TextEncoding: encoding}
isZip = true
}

@ -8,6 +8,8 @@ import (
testMock "github.com/stretchr/testify/mock"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
@ -147,12 +149,24 @@ func (m MockRSC) Close() error {
return nil
}
var basepath string
func init() {
_, currentFile, _, _ := runtime.Caller(0)
basepath = filepath.Dir(currentFile)
}
func Path(rel string) string {
return filepath.Join(basepath, rel)
}
func TestFileSystem_Decompress(t *testing.T) {
asserts := assert.New(t)
ctx := context.Background()
fs := FileSystem{
User: &model.User{Model: gorm.Model{ID: 1}},
}
os.RemoveAll(util.RelativePath("tests/decompress"))
// 压缩文件不存在
{
@ -162,7 +176,7 @@ func TestFileSystem_Decompress(t *testing.T) {
// 查找压缩文件,未找到
mock.ExpectQuery("SELECT(.+)files(.+)").
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
err := fs.Decompress(ctx, "/1.zip", "/")
err := fs.Decompress(ctx, "/1.zip", "/", "")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
}
@ -174,7 +188,7 @@ func TestFileSystem_Decompress(t *testing.T) {
testHandler := new(FileHeaderMock)
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{}, errors.New("error"))
fs.Handler = testHandler
err := fs.Decompress(ctx, "/1.zip", "/")
err := fs.Decompress(ctx, "/1.zip", "/", "")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.EqualError(err, "error")
@ -188,7 +202,7 @@ func TestFileSystem_Decompress(t *testing.T) {
testHandler := new(FileHeaderMock)
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{}, nil)
fs.Handler = testHandler
err := fs.Decompress(ctx, "/1.zip", "/")
err := fs.Decompress(ctx, "/1.zip", "/", "")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
}
@ -201,13 +215,13 @@ func TestFileSystem_Decompress(t *testing.T) {
testHandler := new(FileHeaderMock)
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockNopRSC("1"), nil)
fs.Handler = testHandler
err := fs.Decompress(ctx, "/1.zip", "/")
err := fs.Decompress(ctx, "/1.zip", "/", "")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.EqualError(err, "read error")
asserts.Contains(err.Error(), "read error")
}
// 无效zip文件
// 无法重设上传策略
{
cache.Set("setting_temp_path", "tests", 0)
fs.FileTarget = []model.File{{SourceName: "1.zip", Policy: model.Policy{Type: "mock"}}}
@ -215,22 +229,7 @@ func TestFileSystem_Decompress(t *testing.T) {
testHandler := new(FileHeaderMock)
testHandler.On("Get", testMock.Anything, "1.zip").Return(MockRSC{rs: strings.NewReader("read")}, nil)
fs.Handler = testHandler
err := fs.Decompress(ctx, "/1.zip", "/")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.EqualError(err, "zip: not a valid zip file")
}
// 无法重设上传策略
{
zipFile, _ := os.Open(util.RelativePath("filesystem/tests/test.zip"))
fs.FileTarget = []model.File{{SourceName: "1.zip", Policy: model.Policy{Type: "mock"}}}
fs.FileTarget[0].Policy.ID = 1
testHandler := new(FileHeaderMock)
testHandler.On("Get", testMock.Anything, "1.zip").Return(zipFile, nil)
fs.Handler = testHandler
err := fs.Decompress(ctx, "/1.zip", "/")
zipFile.Close()
err := fs.Decompress(ctx, "/1.zip", "/", "")
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.True(util.IsEmpty(util.RelativePath("tests/decompress")))
@ -239,7 +238,7 @@ func TestFileSystem_Decompress(t *testing.T) {
// 无法上传,容量不足
{
cache.Set("setting_max_parallel_transfer", "1", 0)
zipFile, _ := os.Open(util.RelativePath("filesystem/tests/test.zip"))
zipFile, _ := os.Open(Path("tests/test.zip"))
fs.FileTarget = []model.File{{SourceName: "1.zip", Policy: model.Policy{Type: "mock"}}}
fs.FileTarget[0].Policy.ID = 1
fs.User.Policy.Type = "mock"
@ -247,7 +246,7 @@ func TestFileSystem_Decompress(t *testing.T) {
testHandler.On("Get", testMock.Anything, "1.zip").Return(zipFile, nil)
fs.Handler = testHandler
fs.Decompress(ctx, "/1.zip", "/")
fs.Decompress(ctx, "/1.zip", "/", "")
zipFile.Close()

@ -82,7 +82,7 @@ func (job *DecompressTask) Do() {
job.TaskModel.SetProgress(DecompressingProgress)
err = fs.Decompress(context.Background(), job.TaskProps.Src, job.TaskProps.Dst)
err = fs.Decompress(context.Background(), job.TaskProps.Src, job.TaskProps.Dst, "")
if err != nil {
job.SetErrorMsg("解压缩失败", err)
return

Loading…
Cancel
Save