Taken suggestions from code review and created test for ExtraRuleType.Regex instead of throwing exception there.

This commit is contained in:
Stepan 2020-11-12 13:16:33 +01:00
parent 496923719c
commit 3bca1181b3
5 changed files with 14 additions and 23 deletions

View file

@ -73,6 +73,7 @@ namespace Emby.Naming.AudioBook
var haveChaptersOrPages = stackFiles.Any(x => x.ChapterNumber != null || x.PartNumber != null); var haveChaptersOrPages = stackFiles.Any(x => x.ChapterNumber != null || x.PartNumber != null);
var groupedBy = stackFiles.GroupBy(file => new { file.ChapterNumber, file.PartNumber }); var groupedBy = stackFiles.GroupBy(file => new { file.ChapterNumber, file.PartNumber });
var nameWithReplacedDots = nameParserResult.Name.Replace(" ", ".");
foreach (var group in groupedBy) foreach (var group in groupedBy)
{ {
@ -86,9 +87,9 @@ namespace Emby.Naming.AudioBook
foreach (var audioFile in group) foreach (var audioFile in group)
{ {
var name = Path.GetFileNameWithoutExtension(audioFile.Path); var name = Path.GetFileNameWithoutExtension(audioFile.Path);
if (name == "audiobook" || if (name.Equals("audiobook") ||
name.Contains(nameParserResult.Name, StringComparison.OrdinalIgnoreCase) || name.Contains(nameParserResult.Name, StringComparison.OrdinalIgnoreCase) ||
name.Contains(nameParserResult.Name.Replace(" ", "."), StringComparison.OrdinalIgnoreCase)) name.Contains(nameWithReplacedDots, StringComparison.OrdinalIgnoreCase))
{ {
alt.Add(audioFile); alt.Add(audioFile);
} }

View file

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using Emby.Naming.Audio; using Emby.Naming.Audio;
using Emby.Naming.Common; using Emby.Naming.Common;
@ -52,11 +53,6 @@ namespace Emby.Naming.Video
return result; return result;
} }
} }
else
{
// Currently unreachable code if new rule.MediaType is desired add if clause with proper tests
throw new InvalidOperationException();
}
if (rule.RuleType == ExtraRuleType.Filename) if (rule.RuleType == ExtraRuleType.Filename)
{ {
@ -80,9 +76,6 @@ namespace Emby.Naming.Video
} }
else if (rule.RuleType == ExtraRuleType.Regex) else if (rule.RuleType == ExtraRuleType.Regex)
{ {
// Currently unreachable code if new rule.MediaType is desired add if clause with proper tests
throw new InvalidOperationException();
/*
var filename = Path.GetFileName(path); var filename = Path.GetFileName(path);
var regex = new Regex(rule.Token, RegexOptions.IgnoreCase); var regex = new Regex(rule.Token, RegexOptions.IgnoreCase);
@ -92,7 +85,6 @@ namespace Emby.Naming.Video
result.ExtraType = rule.ExtraType; result.ExtraType = rule.ExtraType;
result.Rule = rule; result.Rule = rule;
} }
*/
} }
else if (rule.RuleType == ExtraRuleType.DirectoryName) else if (rule.RuleType == ExtraRuleType.DirectoryName)
{ {

View file

@ -230,8 +230,8 @@ namespace Emby.Naming.Video
testFilename = testFilename.Substring(folderName.Length).Trim(); testFilename = testFilename.Substring(folderName.Length).Trim();
return string.IsNullOrEmpty(testFilename) return string.IsNullOrEmpty(testFilename)
|| testFilename[0] == '-' || testFilename[0].Equals('-')
|| testFilename[0].Equals( '_') || testFilename[0].Equals('_')
|| string.IsNullOrWhiteSpace(Regex.Replace(testFilename, @"\[([^]]*)\]", string.Empty)); || string.IsNullOrWhiteSpace(Regex.Replace(testFilename, @"\[([^]]*)\]", string.Empty));
} }

View file

@ -1,4 +1,4 @@
using System.IO; using System.IO;
using Emby.Naming.Common; using Emby.Naming.Common;
using Emby.Naming.Video; using Emby.Naming.Video;
using Xunit; using Xunit;
@ -51,6 +51,8 @@ namespace Jellyfin.Naming.Tests.Video
[InlineData("My Movie 2013-12-09", "My Movie 2013-12-09", null)] [InlineData("My Movie 2013-12-09", "My Movie 2013-12-09", null)]
[InlineData("My Movie 20131209", "My Movie 20131209", null)] [InlineData("My Movie 20131209", "My Movie 20131209", null)]
[InlineData("My Movie 2013-12-09 2013", "My Movie 2013-12-09", 2013)] [InlineData("My Movie 2013-12-09 2013", "My Movie 2013-12-09", 2013)]
[InlineData(null, null, null)]
[InlineData("", "", null)]
public void CleanDateTimeTest(string input, string expectedName, int? expectedYear) public void CleanDateTimeTest(string input, string expectedName, int? expectedYear)
{ {
input = Path.GetFileName(input); input = Path.GetFileName(input);

View file

@ -95,18 +95,14 @@ namespace Jellyfin.Naming.Tests.Video
} }
} }
[Fact]
public void TestExtraInfo_InvalidRuleMediaType()
{
var options = new NamingOptions { VideoExtraRules = new[] { new ExtraRule(ExtraType.Unknown, ExtraRuleType.DirectoryName, " ", MediaType.Photo) } };
Assert.Throws<InvalidOperationException>(() => GetExtraTypeParser(options).GetExtraInfo("sample.jpg"));
}
[Fact] [Fact]
public void TestExtraInfo_InvalidRuleType() public void TestExtraInfo_InvalidRuleType()
{ {
var options = new NamingOptions { VideoExtraRules = new[] { new ExtraRule(ExtraType.Unknown, ExtraRuleType.Regex, " ", MediaType.Video) } }; var rule = new ExtraRule(ExtraType.Unknown, ExtraRuleType.Regex, @"([eE]x(tra)?\.\w+)", MediaType.Video);
Assert.Throws<InvalidOperationException>(() => GetExtraTypeParser(options).GetExtraInfo("sample.mp4")); var options = new NamingOptions { VideoExtraRules = new[] { rule } };
var res = GetExtraTypeParser(options).GetExtraInfo("extra.mp4");
Assert.Equal(rule, res.Rule);
} }
[Fact] [Fact]