浏览代码

增加字幕名称修改测试用例,修复命名问题

Signed-off-by: allan716 <[email protected]>
allan716 2 年之前
父节点
当前提交
cd796a631f

+ 4 - 4
pkg/sub_formatter/common/common.go

@@ -8,9 +8,9 @@ const NoMatchFormatter = "No Match formatter"
 type FormatterName int
 
 const (
-	Emby              FormatterName = iota // Emby 格式 xxx.chinese.(简,shooter).ass
-	Normal                                 // 常规  xxx.zh.ass
-	SampleAsVideoName                      // 与视频文件名称相同
+	Emby            FormatterName = iota // Emby 格式 xxx.chinese.(简,shooter).ass
+	Normal                               // 常规  xxx.zh.ass
+	SameAsVideoName                      // 与视频文件名称相同
 )
 
 func (f FormatterName) String() string {
@@ -19,7 +19,7 @@ func (f FormatterName) String() string {
 		return FormatterNameString_Normal
 	case Emby:
 		return FormatterNameString_Emby
-	case SampleAsVideoName:
+	case SameAsVideoName:
 		return FormatterNameString_SampleAsVideoName
 	default:
 		return NoMatchFormatter

+ 12 - 1
pkg/sub_formatter/same_as_video_name/same_as_video_name.go

@@ -1,6 +1,7 @@
 package same_as_video_name
 
 import (
+	"github.com/ChineseSubFinder/ChineseSubFinder/pkg"
 	"github.com/ChineseSubFinder/ChineseSubFinder/pkg/logic/sub_parser/ass"
 	"github.com/ChineseSubFinder/ChineseSubFinder/pkg/logic/sub_parser/srt"
 	"github.com/ChineseSubFinder/ChineseSubFinder/pkg/sub_formatter/common"
@@ -27,7 +28,7 @@ func (f Formatter) GetFormatterName() string {
 }
 
 func (f Formatter) GetFormatterFormatterName() int {
-	return int(common.SampleAsVideoName)
+	return int(common.SameAsVideoName)
 }
 
 // IsMatchThisFormat 是否满足当前实现接口的字幕命名格式 - 是否符合规则、fileNameWithOutExt string, subExt string, subLang types.MyLanguage, extraSubPreName string
@@ -51,6 +52,16 @@ func (f Formatter) IsMatchThisFormat(subName string) (bool, string, string, lang
 	subExt := filepath.Ext(subNameBase)
 	fileNameWithOutExt := strings.ReplaceAll(subNameBase, subExt, "")
 
+	if pkg.IsFile(subName) == true {
+		bok, fInfo, err := f.subParser.DetermineFileTypeFromFile(subName)
+		if err != nil {
+			return false, "", "", language2.Unknown, ""
+		}
+		if bok == true {
+			return true, filepath.Join(subNameDir, fileNameWithOutExt), subExt, fInfo.Lang, ""
+		}
+	}
+
 	return true, filepath.Join(subNameDir, fileNameWithOutExt), subExt, language2.Unknown, ""
 }
 

+ 1 - 1
pkg/sub_formatter/sub_format_changer.go

@@ -240,7 +240,7 @@ func GetSubFormatter(log *logrus.Logger, subNameFormatter int) ifaces.ISubFormat
 			subFormatter = normal.NewFormatter(log)
 			break
 		}
-	case int(common.SampleAsVideoName):
+	case int(common.SameAsVideoName):
 		{
 			subFormatter = same_as_video_name.NewFormatter(log)
 		}

+ 105 - 11
pkg/sub_formatter/sub_format_changer_test.go

@@ -1,6 +1,9 @@
 package sub_formatter
 
 import (
+	"github.com/ChineseSubFinder/ChineseSubFinder/pkg"
+	"github.com/ChineseSubFinder/ChineseSubFinder/pkg/log_helper"
+	"github.com/ChineseSubFinder/ChineseSubFinder/pkg/settings"
 	"path/filepath"
 	"testing"
 
@@ -12,6 +15,8 @@ import (
 
 func TestSubFormatChanger_AutoDetectThenChangeTo(t *testing.T) {
 
+	settings.SetConfigRootPath(pkg.ConfigRootDirFPath())
+
 	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_format_changer"}, 4, true)
 	movie_name := "AAA"
 	series_name := "Loki"
@@ -80,7 +85,7 @@ func TestSubFormatChanger_AutoDetectThenChangeTo(t *testing.T) {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 
-			s := NewSubFormatChanger([]string{tt.fields.movieRootDir}, []string{tt.fields.seriesRootDir})
+			s := NewSubFormatChanger(log_helper.GetLogger4Tester(), []string{tt.fields.movieRootDir}, []string{tt.fields.seriesRootDir})
 
 			got, err := s.AutoDetectThenChangeTo(tt.args.desFormatter)
 			if (err != nil) != tt.wantErr {
@@ -114,13 +119,15 @@ func TestSubFormatChanger_AutoDetectThenChangeTo(t *testing.T) {
 
 func TestSubFormatChangerProcess(t *testing.T) {
 
+	settings.SetConfigRootPath(pkg.ConfigRootDirFPath())
+
 	// 先删除 db
 	err := dao.DeleteDbFile()
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_format_changer"}, 4, true)
+	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_format_changer"}, 3, true)
 	movie_name := "AAA"
 	series_name := "Loki"
 
@@ -134,6 +141,11 @@ func TestSubFormatChangerProcess(t *testing.T) {
 	seriesDir_org_normal := filepath.Join(testRootDir, "series_org_normal")
 	movieOneDir_org_normal := filepath.Join(movieDir_org_normal, movie_name)
 	seriesOneDir_org_normal := filepath.Join(seriesDir_org_normal, series_name, "Season 1")
+	// same 的信息
+	movieDir_org_same := filepath.Join(testRootDir, "movie_org_same")
+	seriesDir_org_same := filepath.Join(testRootDir, "series_org_same")
+	movieOneDir_org_same := filepath.Join(movieDir_org_same, movie_name)
+	seriesOneDir_org_same := filepath.Join(seriesDir_org_same, series_name, "Season 1")
 	// emby 转 emby 理论上不应该改文件
 	movieDir_emby_2_emby := filepath.Join(testRootDir, "movie_emby_2_emby")
 	seriesDir_emby_2_emby := filepath.Join(testRootDir, "series_emby_2_emby")
@@ -149,7 +161,7 @@ func TestSubFormatChangerProcess(t *testing.T) {
 		want    RenameResults
 		wantErr bool
 	}{
-		// 先从 emby 2 normal
+		// 轮次 0:先从 emby 2 normal
 		{name: "emby 2 normal",
 			args: args{movieRootDir: movieDir_org_emby, seriesRootDir: seriesDir_org_emby, nowDesFormatter: common.Normal},
 			want: RenameResults{
@@ -162,7 +174,7 @@ func TestSubFormatChangerProcess(t *testing.T) {
 					filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.zh.srt"):         1,
 				},
 			}, wantErr: false},
-		// 然后从上面一个测试用例的文件夹中,继续,转 normal 2 emby
+		// 轮次 1:然后从上面一个测试用例的文件夹中,继续,转 normal 2 emby
 		{name: "normal 2 emby",
 			args: args{movieRootDir: movieDir_org_emby, seriesRootDir: movieDir_org_emby, nowDesFormatter: common.Emby},
 			want: RenameResults{
@@ -175,13 +187,13 @@ func TestSubFormatChangerProcess(t *testing.T) {
 					filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.chinese(简英).srt"):         1,
 				},
 			}, wantErr: false},
-
+		// 轮次 2:
 		{name: "emby 2 emby",
 			args:    args{movieRootDir: movieDir_emby_2_emby, seriesRootDir: seriesDir_emby_2_emby, nowDesFormatter: common.Emby},
 			want:    RenameResults{},
 			wantErr: false},
 
-		// 重新评估 normal 2 emby ,需要清理数据库
+		// 轮次 3:重新评估 normal 2 emby ,需要清理数据库
 		{name: "normal 2 emby new",
 			args: args{movieRootDir: movieDir_org_normal, seriesRootDir: seriesDir_org_normal, nowDesFormatter: common.Emby},
 			want: RenameResults{
@@ -194,7 +206,7 @@ func TestSubFormatChangerProcess(t *testing.T) {
 					filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.chinese(简英).srt"):         1,
 				},
 			}, wantErr: false},
-		// 然后从上面一个测试用例的文件夹中,继续,转 emby 2 normal
+		// 轮次 4:然后从上面一个测试用例的文件夹中,继续,转 emby 2 normal
 		{name: "emby 2 normal new",
 			args: args{movieRootDir: movieDir_org_normal, seriesRootDir: seriesDir_org_normal, nowDesFormatter: common.Normal},
 			want: RenameResults{
@@ -207,11 +219,55 @@ func TestSubFormatChangerProcess(t *testing.T) {
 					filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.zh.srt"):         1,
 				},
 			}, wantErr: false},
+		// 轮次 5:
+		{name: "normal 2 same",
+			args: args{movieRootDir: movieDir_org_normal, seriesRootDir: seriesDir_org_normal, nowDesFormatter: common.SameAsVideoName},
+			want: RenameResults{
+				RenamedFiles: map[string]int{
+					filepath.Join(movieOneDir_org_normal, "AAA.ass"):            2,
+					filepath.Join(movieOneDir_org_normal, "AAA.srt"):            1,
+					filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.ass"): 2,
+					filepath.Join(seriesOneDir_org_normal, "Loki - S01E01.srt"): 1,
+				},
+			}, wantErr: false},
+		// 轮次 6:
+		{name: "same 2 emby",
+			args: args{movieRootDir: movieDir_org_same, seriesRootDir: seriesDir_org_same, nowDesFormatter: common.Emby},
+			want: RenameResults{
+				RenamedFiles: map[string]int{
+					filepath.Join(movieOneDir_org_same, "AAA.chinese(简英).ass"):            1,
+					filepath.Join(movieOneDir_org_same, "AAA.chinese(简英).srt"):            1,
+					filepath.Join(seriesOneDir_org_same, "Loki - S01E01.chinese(繁英).ass"): 1,
+					filepath.Join(seriesOneDir_org_same, "Loki - S01E01.chinese(简英).srt"): 1,
+				},
+			}, wantErr: false},
+		// 轮次 7:
+		{name: "emby 2 sample",
+			args: args{movieRootDir: movieDir_org_emby, seriesRootDir: seriesDir_org_emby, nowDesFormatter: common.SameAsVideoName},
+			want: RenameResults{
+				RenamedFiles: map[string]int{
+					filepath.Join(movieOneDir_org_emby, "AAA.ass"):            3,
+					filepath.Join(movieOneDir_org_emby, "AAA.srt"):            1,
+					filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.ass"): 6,
+					filepath.Join(seriesOneDir_org_emby, "Loki - S01E01.srt"): 1,
+				},
+			}, wantErr: false},
+		// 轮次 8:
+		{name: "same 2 normal",
+			args: args{movieRootDir: movieDir_org_same, seriesRootDir: seriesDir_org_same, nowDesFormatter: common.Normal},
+			want: RenameResults{
+				RenamedFiles: map[string]int{
+					filepath.Join(movieOneDir_org_same, "AAA.zh.ass"):            1,
+					filepath.Join(movieOneDir_org_same, "AAA.zh.srt"):            1,
+					filepath.Join(seriesOneDir_org_same, "Loki - S01E01.zh.ass"): 1,
+					filepath.Join(seriesOneDir_org_same, "Loki - S01E01.zh.srt"): 1,
+				},
+			}, wantErr: false},
 	}
 	for i, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 
-			if i == 0 || i == 2 || i == 3 {
+			if i == 0 || i == 2 || i == 3 || i == 5 || i == 6 || i == 7 || i == 8 {
 				// 0 - 1 轮次,测试的是 先从 emby 2 normal
 				// 然后从上面一个测试用例的文件夹中,继续,转 normal 2 emby
 				// 先删除 db
@@ -225,7 +281,7 @@ func TestSubFormatChangerProcess(t *testing.T) {
 				}
 			}
 
-			got, err := SubFormatChangerProcess([]string{tt.args.movieRootDir}, []string{tt.args.seriesRootDir}, tt.args.nowDesFormatter)
+			got, err := SubFormatChangerProcess(log_helper.GetLogger4Tester(), []string{tt.args.movieRootDir}, []string{tt.args.seriesRootDir}, tt.args.nowDesFormatter)
 			if err != nil != tt.wantErr {
 				t.Errorf("SubFormatChangerProcess() error = %v, wantErr %v", err, tt.wantErr)
 			}
@@ -244,10 +300,12 @@ func TestSubFormatChangerProcess(t *testing.T) {
 			}
 
 			for fileName, counter := range got.RenamedFiles {
-				if tt.want.RenamedFiles[filepath.FromSlash(fileName)] != counter {
+
+				nowName := filepath.FromSlash(fileName)
+				if tt.want.RenamedFiles[nowName] != counter {
 					//println(fileName)
 					//println(filepath.FromSlash(fileName))
-					t.Errorf("SubFormatChangerProcess() RenamedFiles %v got = %v, want %v", fileName, counter, tt.want.RenamedFiles[fileName])
+					t.Errorf("SubFormatChangerProcess() RenamedFiles %v got = %v, want %v", fileName, counter, tt.want.RenamedFiles[nowName])
 					return
 				}
 			}
@@ -286,6 +344,42 @@ func TestSubFormatChangerProcess(t *testing.T) {
 					t.Fatal(tt.name, "i == 4 check db result")
 				}
 			}
+
+			if i == 5 {
+				// 这里需要校验一次数据库的赋值是否正确
+				var subFormatRec models.SubFormatRec
+				dao.GetDb().First(&subFormatRec)
+				if subFormatRec.FormatName != int(common.SameAsVideoName) || subFormatRec.Done == false {
+					t.Fatal(tt.name, "i == 5 check db result")
+				}
+			}
+
+			if i == 6 {
+				// 这里需要校验一次数据库的赋值是否正确
+				var subFormatRec models.SubFormatRec
+				dao.GetDb().First(&subFormatRec)
+				if subFormatRec.FormatName != int(common.Emby) || subFormatRec.Done == false {
+					t.Fatal(tt.name, "i == 6 check db result")
+				}
+			}
+
+			if i == 7 {
+				// 这里需要校验一次数据库的赋值是否正确
+				var subFormatRec models.SubFormatRec
+				dao.GetDb().First(&subFormatRec)
+				if subFormatRec.FormatName != int(common.SameAsVideoName) || subFormatRec.Done == false {
+					t.Fatal(tt.name, "i == 6 check db result")
+				}
+			}
+
+			if i == 8 {
+				// 这里需要校验一次数据库的赋值是否正确
+				var subFormatRec models.SubFormatRec
+				dao.GetDb().First(&subFormatRec)
+				if subFormatRec.FormatName != int(common.Normal) || subFormatRec.Done == false {
+					t.Fatal(tt.name, "i == 6 check db result")
+				}
+			}
 		})
 	}
 }