From 0a0de6708e0b900ca616b108dfdf94894a56a6e0 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Thu, 1 Aug 2024 17:17:10 +0200 Subject: [PATCH] Enable more analyser rules as errors Also deduplicates a bit of code inside of SeasonPathParser and adds some more tests --- Emby.Naming/TV/SeasonPathParser.cs | 15 +++----- MediaBrowser.Model/Dlna/StreamBuilder.cs | 2 +- jellyfin.ruleset | 22 +++++++++++ .../TV/SeasonFolderTests.cs | 35 ------------------ .../TV/SeasonPathParserTests.cs | 37 +++++++++++++++++++ 5 files changed, 66 insertions(+), 45 deletions(-) delete mode 100644 tests/Jellyfin.Naming.Tests/TV/SeasonFolderTests.cs create mode 100644 tests/Jellyfin.Naming.Tests/TV/SeasonPathParserTests.cs diff --git a/Emby.Naming/TV/SeasonPathParser.cs b/Emby.Naming/TV/SeasonPathParser.cs index fc9ee8e569..45b91971bf 100644 --- a/Emby.Naming/TV/SeasonPathParser.cs +++ b/Emby.Naming/TV/SeasonPathParser.cs @@ -24,6 +24,8 @@ namespace Emby.Naming.TV "stagione" }; + private static readonly char[] _splitChars = ['.', '_', ' ', '-']; + /// /// Attempts to parse season number from path. /// @@ -83,14 +85,9 @@ namespace Emby.Naming.TV } } - if (filename.StartsWith("s", StringComparison.OrdinalIgnoreCase)) + if (TryGetSeasonNumberFromPart(filename, out int seasonNumber)) { - var testFilename = filename.AsSpan().Slice(1); - - if (int.TryParse(testFilename, NumberStyles.Integer, CultureInfo.InvariantCulture, out var val)) - { - return (val, true); - } + return (seasonNumber, true); } // Look for one of the season folder names @@ -108,10 +105,10 @@ namespace Emby.Naming.TV } } - var parts = filename.Split(new[] { '.', '_', ' ', '-' }, StringSplitOptions.RemoveEmptyEntries); + var parts = filename.Split(_splitChars, StringSplitOptions.RemoveEmptyEntries); foreach (var part in parts) { - if (TryGetSeasonNumberFromPart(part, out int seasonNumber)) + if (TryGetSeasonNumberFromPart(part, out seasonNumber)) { return (seasonNumber, true); } diff --git a/MediaBrowser.Model/Dlna/StreamBuilder.cs b/MediaBrowser.Model/Dlna/StreamBuilder.cs index 4815dcc044..d2715e2ac7 100644 --- a/MediaBrowser.Model/Dlna/StreamBuilder.cs +++ b/MediaBrowser.Model/Dlna/StreamBuilder.cs @@ -1304,7 +1304,7 @@ namespace MediaBrowser.Model.Dlna // Check audio codec MediaStream? selectedAudioStream = null; - if (candidateAudioStreams.Any()) + if (candidateAudioStreams.Count != 0) { selectedAudioStream = candidateAudioStreams.FirstOrDefault(audioStream => directPlayProfile.SupportsAudioCodec(audioStream.Codec)); if (selectedAudioStream is null) diff --git a/jellyfin.ruleset b/jellyfin.ruleset index db116f46c8..67ffd9a37b 100644 --- a/jellyfin.ruleset +++ b/jellyfin.ruleset @@ -105,6 +105,28 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/Jellyfin.Naming.Tests/TV/SeasonFolderTests.cs b/tests/Jellyfin.Naming.Tests/TV/SeasonFolderTests.cs deleted file mode 100644 index 6773bbeb19..0000000000 --- a/tests/Jellyfin.Naming.Tests/TV/SeasonFolderTests.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Emby.Naming.TV; -using Xunit; - -namespace Jellyfin.Naming.Tests.TV -{ - public class SeasonFolderTests - { - [Theory] - [InlineData("/Drive/Season 1", 1, true)] - [InlineData("/Drive/Season 2", 2, true)] - [InlineData("/Drive/Season 02", 2, true)] - [InlineData("/Drive/Seinfeld/S02", 2, true)] - [InlineData("/Drive/Seinfeld/2", 2, true)] - [InlineData("/Drive/Season 2009", 2009, true)] - [InlineData("/Drive/Season1", 1, true)] - [InlineData("The Wonder Years/The.Wonder.Years.S04.PDTV.x264-JCH", 4, true)] - [InlineData("/Drive/Season 7 (2016)", 7, false)] - [InlineData("/Drive/Staffel 7 (2016)", 7, false)] - [InlineData("/Drive/Stagione 7 (2016)", 7, false)] - [InlineData("/Drive/Season (8)", null, false)] - [InlineData("/Drive/3.Staffel", 3, false)] - [InlineData("/Drive/s06e05", null, false)] - [InlineData("/Drive/The.Legend.of.Condor.Heroes.2017.V2.web-dl.1080p.h264.aac-hdctv", null, false)] - [InlineData("/Drive/extras", 0, true)] - [InlineData("/Drive/specials", 0, true)] - public void GetSeasonNumberFromPathTest(string path, int? seasonNumber, bool isSeasonDirectory) - { - var result = SeasonPathParser.Parse(path, true, true); - - Assert.Equal(result.SeasonNumber is not null, result.Success); - Assert.Equal(result.SeasonNumber, seasonNumber); - Assert.Equal(isSeasonDirectory, result.IsSeasonFolder); - } - } -} diff --git a/tests/Jellyfin.Naming.Tests/TV/SeasonPathParserTests.cs b/tests/Jellyfin.Naming.Tests/TV/SeasonPathParserTests.cs new file mode 100644 index 0000000000..3a042df683 --- /dev/null +++ b/tests/Jellyfin.Naming.Tests/TV/SeasonPathParserTests.cs @@ -0,0 +1,37 @@ +using Emby.Naming.TV; +using Xunit; + +namespace Jellyfin.Naming.Tests.TV; + +public class SeasonPathParserTests +{ + [Theory] + [InlineData("/Drive/Season 1", 1, true)] + [InlineData("/Drive/s1", 1, true)] + [InlineData("/Drive/S1", 1, true)] + [InlineData("/Drive/Season 2", 2, true)] + [InlineData("/Drive/Season 02", 2, true)] + [InlineData("/Drive/Seinfeld/S02", 2, true)] + [InlineData("/Drive/Seinfeld/2", 2, true)] + [InlineData("/Drive/Seinfeld - S02", 2, true)] + [InlineData("/Drive/Season 2009", 2009, true)] + [InlineData("/Drive/Season1", 1, true)] + [InlineData("The Wonder Years/The.Wonder.Years.S04.PDTV.x264-JCH", 4, true)] + [InlineData("/Drive/Season 7 (2016)", 7, false)] + [InlineData("/Drive/Staffel 7 (2016)", 7, false)] + [InlineData("/Drive/Stagione 7 (2016)", 7, false)] + [InlineData("/Drive/Season (8)", null, false)] + [InlineData("/Drive/3.Staffel", 3, false)] + [InlineData("/Drive/s06e05", null, false)] + [InlineData("/Drive/The.Legend.of.Condor.Heroes.2017.V2.web-dl.1080p.h264.aac-hdctv", null, false)] + [InlineData("/Drive/extras", 0, true)] + [InlineData("/Drive/specials", 0, true)] + public void GetSeasonNumberFromPathTest(string path, int? seasonNumber, bool isSeasonDirectory) + { + var result = SeasonPathParser.Parse(path, true, true); + + Assert.Equal(result.SeasonNumber is not null, result.Success); + Assert.Equal(result.SeasonNumber, seasonNumber); + Assert.Equal(isSeasonDirectory, result.IsSeasonFolder); + } +}