Răsfoiți Sursa

1,修复,GetDebugFolderByName 的 bug;
2,修复部分测试用例的问题

Signed-off-by: allan716 <[email protected]>

allan716 3 ani în urmă
părinte
comite
62d61d1dc6

+ 9 - 7
internal/pkg/archive_helper/unarchiveFile_test.go

@@ -2,23 +2,25 @@ package archive_helper
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
 	"path/filepath"
+	"strings"
 	"testing"
 )
 
 func TestUnArchiveFile(t *testing.T) {
 
-	testDataPath := filepath.FromSlash("../../../TestData/misc")
-	// TODO: remove CopyTestData
-	testUnArchive(t, testDataPath, "zip.zip")
-	testUnArchive(t, testDataPath, "tar.tar")
-	testUnArchive(t, testDataPath, "rar.rar")
-	testUnArchive(t, testDataPath, "7z.7z")
+	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"zips"}, 4, true)
+
+	testUnArchive(t, testRootDir, "zip.zip")
+	testUnArchive(t, testRootDir, "tar.tar")
+	testUnArchive(t, testRootDir, "rar.rar")
+	testUnArchive(t, testRootDir, "7z.7z")
 }
 
 func testUnArchive(t *testing.T, testRootDir string, missionName string) {
 	fileFPath := filepath.Join(testRootDir, missionName)
-	desPath := t.TempDir()
+	desPath := filepath.Join(testRootDir, strings.ReplaceAll(filepath.Ext(missionName), ".", ""))
 	err := UnArchiveFile(fileFPath, desPath)
 	if err != nil {
 		t.Fatal(err)

+ 2 - 2
internal/pkg/my_util/folder.go

@@ -33,9 +33,9 @@ func GetDebugFolderByName(names []string) (string, error) {
 		return "", err
 	}
 
-	tmpFolderFullPath := ""
+	tmpFolderFullPath := rootPath
 	for _, name := range names {
-		tmpFolderFullPath = filepath.Join(rootPath, name)
+		tmpFolderFullPath = filepath.Join(tmpFolderFullPath, name)
 	}
 	err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
 	if err != nil {

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

@@ -258,26 +258,6 @@ func CopyDir(src string, dst string) error {
 	return nil
 }
 
-// CopyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
-func CopyTestData(srcDir string) (string, error) {
-	// 测试数据的文件夹
-	orgDir := filepath.Join(srcDir, "org")
-	testDir := filepath.Join(srcDir, "test")
-
-	if IsDir(testDir) == true {
-		err := ClearFolder(testDir)
-		if err != nil {
-			return "", err
-		}
-	}
-
-	err := CopyDir(orgDir, testDir)
-	if err != nil {
-		return "", err
-	}
-	return testDir, nil
-}
-
 // CloseChrome 强行结束没有关闭的 Chrome 进程
 func CloseChrome() {
 

+ 1 - 1
internal/pkg/sub_helper/dialogue_merger_test.go

@@ -59,7 +59,7 @@ func Test_isFirstLetterIsEngLower(t *testing.T) {
 
 func TestNewDialogueMerger(t *testing.T) {
 
-	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"FixTimeline", "test"}, 4)
+	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"FixTimeline", "org"}, 4, false)
 
 	subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
 	//bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(filepath.Join(testRootDir, "2line-The Card Counter (2021) WEBDL-1080p.chinese(inside).ass"))

+ 24 - 13
internal/pkg/sub_helper/sub_helper_test.go

@@ -6,23 +6,32 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
 	"path/filepath"
 	"testing"
 )
 
 func TestDeleteOneSeasonSubCacheFolder(t *testing.T) {
-
-
-	// ERROR: DeleteOneSeasonSubCacheFolder will create 
-	// CSF-DebugThings/sub_helper
-	// TODO: fix GetRootDebugFolder 
-
-	testRootDir := "../../../TestData/sub_helper"
-	err := DeleteOneSeasonSubCacheFolder(testRootDir)
+	const testSerName = "XXX"
+	const needDelFolderName = "Sub_S1E0"
+	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_helper", "test", needDelFolderName}, 4, false)
+	desSerFullPath, err := my_util.GetDebugFolderByName([]string{testSerName})
 	if err != nil {
 		t.Fatal(err)
 	}
-	if my_util.IsDir(filepath.Join(testRootDir, "Sub_S1E0")) == true {
+	desSeasonFullPath, err := my_util.GetDebugFolderByName([]string{testSerName, filepath.Base(testRootDir)})
+	if err != nil {
+		t.Fatal(err)
+	}
+	err = my_util.CopyDir(testRootDir, desSeasonFullPath)
+	if err != nil {
+		t.Fatal(err)
+	}
+	err = DeleteOneSeasonSubCacheFolder(desSerFullPath)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if my_util.IsDir(desSeasonFullPath) == true {
 		t.Fatal("Sub_S1E0 not delete")
 	}
 }
@@ -32,8 +41,10 @@ func TestGetVADInfosFromSub(t *testing.T) {
 	// 这两个字幕是一样的,只不过是格式不同而已
 	subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
 
-	baseSubFile := filepath.FromSlash("../../../TestData/sub_helper/R&M-S05E10/Rick and Morty - S05E10 - Rickmurai Jack WEBRip-1080p.chinese(简,zimuku).default.srt")
-	srcSubFile := filepath.FromSlash("../../../TestData/sub_helper/R&M-S05E10/Rick and Morty - S05E10 - Rickmurai Jack WEBRip-1080p.chinese(简英,zimuku).ass")
+	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_helper", "org", "R&M-S05E10"}, 4, false)
+
+	baseSubFile := filepath.Join(testRootDir, "Rick and Morty - S05E10 - Rickmurai Jack WEBRip-1080p.chinese(简,zimuku).default.srt")
+	srcSubFile := filepath.Join(testRootDir, "Rick and Morty - S05E10 - Rickmurai Jack WEBRip-1080p.chinese(简英,zimuku).ass")
 
 	bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(baseSubFile)
 	if err != nil {
@@ -50,9 +61,9 @@ func TestGetVADInfosFromSub(t *testing.T) {
 		t.Fatal("sub not match")
 	}
 
-	if len(infoBase.DialoguesFilterEx) != len(infoSrc.DialoguesFilterEx) {
+	if len(infoBase.Dialogues) != len(infoSrc.Dialogues) {
 		t.Fatal(fmt.Sprintf("info Base And Src Parse Error, infoBase.DialoguesFilterEx Len = %v, infoSrc.DialoguesFilterEx Len = %v",
-			len(infoBase.DialoguesFilterEx), len(infoSrc.DialoguesFilterEx)))
+			len(infoBase.Dialogues), len(infoSrc.Dialogues)))
 	}
 
 	baseSubUnit, err := GetVADInfoFeatureFromSubNew(infoBase, FrontAndEndPerBase)

+ 37 - 1
internal/pkg/unit_test_helper/unit_test_helper.go

@@ -1,11 +1,12 @@
 package unit_test_helper
 
 import (
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"path/filepath"
 )
 
 // GetTestDataResourceRootPath 向上返回几层就能够到 ChineseSubFinder-TestData 同级目录,然后进入其中的 resourceFolderName 资源文件夹中
-func GetTestDataResourceRootPath(resourceFolderNames []string, goBackTimes int) string {
+func GetTestDataResourceRootPath(resourceFolderNames []string, goBackTimes int, userCopyData bool) string {
 
 	times := ""
 	for i := 0; i < goBackTimes; i++ {
@@ -19,8 +20,43 @@ func GetTestDataResourceRootPath(resourceFolderNames []string, goBackTimes int)
 
 	outPath = filepath.FromSlash(outPath)
 
+	if userCopyData == true {
+		// 想要 copy org 中的数据到 test 中去处理
+		orgDir := filepath.Join(outPath, "org")
+		if my_util.IsDir(orgDir) == false {
+			// 如果没有发现 org 文件夹,就返回之前的路径即可
+			return outPath
+		}
+		// 如果发现有,那启动 copy 的操作
+		testDataPath, err := copyTestData(outPath)
+		if err != nil {
+			return outPath
+		}
+		return filepath.FromSlash(testDataPath)
+	}
+
 	return outPath
 }
 
+// copyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
+func copyTestData(srcDir string) (string, error) {
+	// 测试数据的文件夹
+	orgDir := filepath.Join(srcDir, "org")
+	testDir := filepath.Join(srcDir, "test")
+
+	if my_util.IsDir(testDir) == true {
+		err := my_util.ClearFolder(testDir)
+		if err != nil {
+			return "", err
+		}
+	}
+
+	err := my_util.CopyDir(orgDir, testDir)
+	if err != nil {
+		return "", err
+	}
+	return testDir, nil
+}
+
 const oneBackTime = "../"
 const testResourceProjectName = "ChineseSubFinder-TestData"

+ 4 - 1
internal/pkg/unit_test_helper/unit_test_helper_test.go

@@ -9,6 +9,7 @@ func TestGetTestDataResourceRootPath(t *testing.T) {
 	type args struct {
 		resourceFolderNames []string
 		goBackTimes         int
+		useCopyData         bool
 	}
 	tests := []struct {
 		name string
@@ -19,6 +20,7 @@ func TestGetTestDataResourceRootPath(t *testing.T) {
 			name: "loghelper", args: args{
 				resourceFolderNames: []string{"log_helper"},
 				goBackTimes:         1,
+				useCopyData:         false,
 			},
 			want: filepath.FromSlash("../ChineseSubFinder-TestData/log_helper"),
 		},
@@ -26,13 +28,14 @@ func TestGetTestDataResourceRootPath(t *testing.T) {
 			name: "language", args: args{
 				resourceFolderNames: []string{"language", "test"},
 				goBackTimes:         1,
+				useCopyData:         false,
 			},
 			want: filepath.FromSlash("../ChineseSubFinder-TestData/language/test"),
 		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			if got := GetTestDataResourceRootPath(tt.args.resourceFolderNames, tt.args.goBackTimes); got != tt.want {
+			if got := GetTestDataResourceRootPath(tt.args.resourceFolderNames, tt.args.goBackTimes, tt.args.useCopyData); got != tt.want {
 				t.Errorf("GetTestDataResourceRootPath() = %v, want %v", got, tt.want)
 			}
 		})