Skip to content

Commit

Permalink
1,修复,GetDebugFolderByName 的 bug;
Browse files Browse the repository at this point in the history
2,修复部分测试用例的问题

Signed-off-by: allan716 <525223688@qq.com>
  • Loading branch information
allanpk716 committed Jan 5, 2022
1 parent 21cfa0e commit 62d61d1
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 45 deletions.
16 changes: 9 additions & 7 deletions internal/pkg/archive_helper/unarchiveFile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/my_util/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 0 additions & 20 deletions internal/pkg/my_util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/sub_helper/dialogue_merger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
37 changes: 24 additions & 13 deletions internal/pkg/sub_helper/sub_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
38 changes: 37 additions & 1 deletion internal/pkg/unit_test_helper/unit_test_helper.go
Original file line number Diff line number Diff line change
@@ -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++ {
Expand All @@ -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"
5 changes: 4 additions & 1 deletion internal/pkg/unit_test_helper/unit_test_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func TestGetTestDataResourceRootPath(t *testing.T) {
type args struct {
resourceFolderNames []string
goBackTimes int
useCopyData bool
}
tests := []struct {
name string
Expand All @@ -19,20 +20,22 @@ func TestGetTestDataResourceRootPath(t *testing.T) {
name: "loghelper", args: args{
resourceFolderNames: []string{"log_helper"},
goBackTimes: 1,
useCopyData: false,
},
want: filepath.FromSlash("../ChineseSubFinder-TestData/log_helper"),
},
{
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)
}
})
Expand Down

0 comments on commit 62d61d1

Please sign in to comment.