ソースを参照

修复测试用例的问题,比如 copy 测试用例的文件需要考虑批量可能会这边 copy 那边被清理的问题

Signed-off-by: 716 <[email protected]>
716 3 年 前
コミット
4c596012bb

+ 1 - 2
internal/downloader.go

@@ -24,7 +24,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/types/emby"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/series"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
-	"github.com/go-rod/rod/lib/utils"
 	"github.com/panjf2000/ants/v2"
 	"github.com/sirupsen/logrus"
 	"golang.org/x/net/context"
@@ -638,7 +637,7 @@ func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFil
 		}
 	}
 	// 最后写入字幕
-	err := utils.OutputFile(desSubFullPath, finalSubFile.Data)
+	err := my_util.WriteFile(desSubFullPath, finalSubFile.Data)
 	if err != nil {
 		return err
 	}

+ 22 - 22
internal/logic/sub_supplier/subhd/subhd_test.go

@@ -7,28 +7,28 @@ import (
 // 无需关注这个测试用例,这个方案暂时弃用
 func TestSupplier_GetSubListFromFile(t *testing.T) {
 
-	//movie1 := "X:\\电影\\The Devil All the Time (2020)\\The Devil All the Time (2020) WEBDL-1080p.mkv"
-	//movie1 := "X:\\电影\\Luca (2021)\\Luca (2021) WEBDL-1080p.mkv"
-	//movie1 := "X:\\电影\\The Boss Baby Family Business (2021)\\The Boss Baby Family Business (2021) WEBDL-1080p.mkv"
-	//movie1 := "X:\\电影\\Oslo (2021)\\Oslo (2021) WEBDL-1080p.mkv"
-	//movie1 := "X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv"
-	movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
-	//movie1 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
-
-	subhd := NewSupplier()
-	outList, err := subhd.getSubListFromFile4Movie(movie1)
-	if err != nil {
-		t.Error(err)
-	}
-	println(outList)
-
-	if len(outList) == 0 {
-		println("now sub found")
-	}
-
-	for i, sublist := range outList {
-		println(i, sublist.Name, sublist.Ext, sublist.Language.String(), sublist.Score, sublist.FileUrl, len(sublist.Data))
-	}
+	////movie1 := "X:\\电影\\The Devil All the Time (2020)\\The Devil All the Time (2020) WEBDL-1080p.mkv"
+	////movie1 := "X:\\电影\\Luca (2021)\\Luca (2021) WEBDL-1080p.mkv"
+	////movie1 := "X:\\电影\\The Boss Baby Family Business (2021)\\The Boss Baby Family Business (2021) WEBDL-1080p.mkv"
+	////movie1 := "X:\\电影\\Oslo (2021)\\Oslo (2021) WEBDL-1080p.mkv"
+	////movie1 := "X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv"
+	//movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
+	////movie1 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
+	//
+	//subhd := NewSupplier()
+	//outList, err := subhd.getSubListFromFile4Movie(movie1)
+	//if err != nil {
+	//	t.Error(err)
+	//}
+	//println(outList)
+	//
+	//if len(outList) == 0 {
+	//	println("now sub found")
+	//}
+	//
+	//for i, sublist := range outList {
+	//	println(i, sublist.Name, sublist.Ext, sublist.Language.String(), sublist.Score, sublist.FileUrl, len(sublist.Data))
+	//}
 }
 
 // 无需关注这个测试用例,这个方案暂时弃用

+ 3 - 3
internal/pkg/archive_helper/unarchiveFile.go

@@ -5,8 +5,8 @@ import (
 	"bytes"
 	"compress/flate"
 	"errors"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/bodgit/sevenzip"
-	"github.com/go-rod/rod/lib/utils"
 	"github.com/mholt/archiver/v3"
 	"golang.org/x/text/encoding/simplifiedchinese"
 	"golang.org/x/text/transform"
@@ -124,7 +124,7 @@ func processOneFile(f archiver.File, notUTF8 bool, desRootPath string) error {
 		//读取到最终的缓冲区中
 		chunk = append(chunk, buf[:n]...)
 	}
-	err := utils.OutputFile(filepath.Join(desRootPath, decodeName), chunk)
+	err := my_util.WriteFile(filepath.Join(desRootPath, decodeName), chunk)
 	if err != nil {
 		return err
 	}
@@ -162,7 +162,7 @@ func un7zOneFile(file *sevenzip.File, desRootPath string) error {
 	}
 	decodeName := file.Name
 	decodeName = filepath.Base(decodeName)
-	err = utils.OutputFile(filepath.Join(desRootPath, decodeName), data)
+	err = my_util.WriteFile(filepath.Join(desRootPath, decodeName), data)
 	if err != nil {
 		return err
 	}

+ 76 - 0
internal/pkg/my_util/folder.go

@@ -4,6 +4,8 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
 	"os"
 	"path/filepath"
+	"strconv"
+	"strings"
 )
 
 // --------------------------------------------------------------
@@ -202,6 +204,80 @@ func ClearFolder(folderFullPath string) error {
 	return nil
 }
 
+// ClearFolderEx 清空文件夹,文件夹名称有特殊之处,Hour-min-Nanosecond 的命名方式
+// 如果调用的时候,已存在的文件夹的时间 min < 5 那么则清理
+func ClearFolderEx(folderFullPath string, overtime int) error {
+
+	_, hour, minute, _ := GetNowTimeString()
+	pathSep := string(os.PathSeparator)
+	files, err := os.ReadDir(folderFullPath)
+	if err != nil {
+		return err
+	}
+	for _, curFile := range files {
+		fullPath := folderFullPath + pathSep + curFile.Name()
+		if curFile.IsDir() {
+
+			parts := strings.Split(curFile.Name(), "-")
+			if len(parts) == 3 {
+				// 基本是符合了,倒是还是需要额外的判断是否时间超过了
+				tmpHourStr := parts[0]
+				tmpMinuteStr := parts[1]
+				tmpHour, err := strconv.Atoi(tmpHourStr)
+				if err != nil {
+					// 如果不符合命名格式,直接删除
+					err = os.RemoveAll(fullPath)
+					if err != nil {
+						return err
+					}
+					continue
+				}
+				tmpMinute, err := strconv.Atoi(tmpMinuteStr)
+				if err != nil {
+					// 如果不符合命名格式,直接删除
+					err = os.RemoveAll(fullPath)
+					if err != nil {
+						return err
+					}
+					continue
+				}
+				// 判断时间
+				if tmpHour != hour {
+					// 如果不符合命名格式,直接删除
+					err = os.RemoveAll(fullPath)
+					if err != nil {
+						return err
+					}
+					continue
+				}
+				// 超过 5 min
+				if minute-overtime > tmpMinute {
+					// 如果不符合命名格式,直接删除
+					err = os.RemoveAll(fullPath)
+					if err != nil {
+						return err
+					}
+					continue
+				}
+			} else {
+				// 如果不符合命名格式,直接删除
+				err = os.RemoveAll(fullPath)
+				if err != nil {
+					return err
+				}
+			}
+		} else {
+			// 这里就是文件了
+			err = os.Remove(fullPath)
+			if err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}
+
 const (
 	DebugFolder       = "CSF-DebugThings" // 调试相关的文件夹
 	TmpFolder         = "CSF-TmpThings"   // 临时缓存的文件夹

+ 39 - 0
internal/pkg/my_util/util.go

@@ -486,3 +486,42 @@ func GetFileSHA1(srcFileFPath string) (string, error) {
 	}
 	return hex.EncodeToString(h.Sum(nil)), nil
 }
+
+// WriteFile 写文件
+func WriteFile(desFileFPath string, bytes []byte) error {
+	var err error
+	nowDesPath := desFileFPath
+	if filepath.IsAbs(nowDesPath) == false {
+		nowDesPath, err = filepath.Abs(nowDesPath)
+		if err != nil {
+			return err
+		}
+	}
+	// 创建对应的目录
+	nowDirPath := filepath.Dir(nowDesPath)
+	err = os.MkdirAll(nowDirPath, os.ModePerm)
+	if err != nil {
+		return err
+	}
+	file, err := os.Create(nowDesPath)
+	if err != nil {
+		return err
+	}
+	defer func() {
+		_ = file.Close()
+	}()
+
+	_, err = file.Write(bytes)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// GetNowTimeString 获取当前的时间,没有秒
+func GetNowTimeString() (string, int, int, int) {
+	nowTime := time.Now()
+	addString := fmt.Sprintf("%d-%d-%d", nowTime.Hour(), nowTime.Minute(), nowTime.Nanosecond())
+	return addString, nowTime.Hour(), nowTime.Minute(), nowTime.Nanosecond()
+}

+ 2 - 3
internal/pkg/sub_helper/sub_helper.go

@@ -13,7 +13,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/vad"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
-	"github.com/go-rod/rod/lib/utils"
 	"math"
 	"os"
 	"path/filepath"
@@ -42,9 +41,9 @@ func OrganizeDlSubFiles(tmpFolderName string, subInfos []supplier.SubInfo) (map[
 	for i := range subInfos {
 		// 先存下来,保存是时候需要前缀,前缀就是从那个网站下载来的
 		nowFileSaveFullPath := filepath.Join(tmpFolderFullPath, GetFrontNameAndOrgName(&subInfos[i]))
-		err = utils.OutputFile(nowFileSaveFullPath, subInfos[i].Data)
+		err = my_util.WriteFile(nowFileSaveFullPath, subInfos[i].Data)
 		if err != nil {
-			log_helper.GetLogger().Errorln("getFrontNameAndOrgName - OutputFile", subInfos[i].FromWhere, subInfos[i].Name, subInfos[i].TopN, err)
+			log_helper.GetLogger().Errorln("getFrontNameAndOrgName - WriteFile", subInfos[i].FromWhere, subInfos[i].Name, subInfos[i].TopN, err)
 			continue
 		}
 		nowExt := strings.ToLower(subInfos[i].Ext)

+ 9 - 3
internal/pkg/unit_test_helper/unit_test_helper.go

@@ -139,22 +139,28 @@ func GenerateXunleiVideoFile(videoPartsRootPath string) (string, error) {
 
 // copyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
 func copyTestData(srcDir string) (string, error) {
+
+	// 因为会出现,批量测试的需求,那么如果每次都进行一次清理,那么就会导致之前创建的被清理掉,测试用例失败
+	// 可以简单的按时间来判断,如果当前时间与以及存在文件夹名称相差在 5min,那么就清理掉
+	addString, _, _, _ := my_util.GetNowTimeString()
 	// 测试数据的文件夹
 	orgDir := filepath.Join(srcDir, "org")
 	testDir := filepath.Join(srcDir, "test")
 
 	if my_util.IsDir(testDir) == true {
-		err := my_util.ClearFolder(testDir)
+		err := my_util.ClearFolderEx(testDir, 2)
 		if err != nil {
 			return "", err
 		}
 	}
 
-	err := my_util.CopyDir(orgDir, testDir)
+	// 多加一层,这样在批量测试的时候才不会出错
+	testDirEx := filepath.Join(testDir, addString)
+	err := my_util.CopyDir(orgDir, testDirEx)
 	if err != nil {
 		return "", err
 	}
-	return testDir, nil
+	return testDirEx, nil
 }
 
 const oneBackTime = "../"

+ 1 - 1
internal/pkg/vad/vad_helper_test.go

@@ -10,7 +10,7 @@ func TestGetVADInfo(t *testing.T) {
 
 	var audioInfo = AudioInfo{
 
-		FileFullPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"ffmpeg"}, 4, false), "sampleAudio.wav"),
+		FileFullPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"ffmpeg", "org"}, 4, false), "sampleAudio.wav"),
 		// check below accordingly
 		SampleRate: 16000,
 		BitDepth:   16,