From 02d19c609b558e790272dfebc45d014309da1890 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Mon, 16 Oct 2023 17:23:23 -0500 Subject: [PATCH] fix: Match categories by file without extension File name comparisons using the markdown table anchor link was broken because the extension (`.json`) was not stripped from the file name. --- CHANGELOG.md | 4 +++ .../CustomFormat/CustomFormatLoader.cs | 9 ++++-- .../CustomFormatLoaderIntegrationTest.cs | 28 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eed397d..81a536ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- CLI: Some custom formats were not properly categorized when running `list custom-formats`. + ## [6.0.1] - 2023-10-02 ### Fixed diff --git a/src/Recyclarr.TrashGuide/CustomFormat/CustomFormatLoader.cs b/src/Recyclarr.TrashGuide/CustomFormat/CustomFormatLoader.cs index 740efcc9..7abc2a88 100644 --- a/src/Recyclarr.TrashGuide/CustomFormat/CustomFormatLoader.cs +++ b/src/Recyclarr.TrashGuide/CustomFormat/CustomFormatLoader.cs @@ -9,11 +9,13 @@ public class CustomFormatLoader : ICustomFormatLoader { private readonly ServiceJsonLoader _loader; private readonly ICustomFormatCategoryParser _categoryParser; + private readonly IFileSystem _fs; - public CustomFormatLoader(ServiceJsonLoader loader, ICustomFormatCategoryParser categoryParser) + public CustomFormatLoader(ServiceJsonLoader loader, ICustomFormatCategoryParser categoryParser, IFileSystem fs) { _loader = loader; _categoryParser = categoryParser; + _fs = fs; } public ICollection LoadAllCustomFormatsAtPaths( @@ -23,8 +25,9 @@ public class CustomFormatLoader : ICustomFormatLoader var categories = _categoryParser.Parse(collectionOfCustomFormats).AsReadOnly(); return _loader.LoadAllFilesAtPaths(jsonPaths, x => x.Select(cf => { - var matchingCategory = categories.FirstOrDefault( - y => y.CfName.EqualsIgnoreCase(cf.Obj.Name) || y.CfAnchor.EqualsIgnoreCase(cf.File.Name)); + var matchingCategory = categories.FirstOrDefault(y => + y.CfName.EqualsIgnoreCase(cf.Obj.Name) || + y.CfAnchor.EqualsIgnoreCase(_fs.Path.GetFileNameWithoutExtension(cf.File.Name))); return cf.Obj with { diff --git a/src/tests/Recyclarr.IntegrationTests/CustomFormatLoaderIntegrationTest.cs b/src/tests/Recyclarr.IntegrationTests/CustomFormatLoaderIntegrationTest.cs index 206760a5..67b3c227 100644 --- a/src/tests/Recyclarr.IntegrationTests/CustomFormatLoaderIntegrationTest.cs +++ b/src/tests/Recyclarr.IntegrationTests/CustomFormatLoaderIntegrationTest.cs @@ -1,5 +1,7 @@ using System.IO.Abstractions; using System.Text.Json; +using Autofac; +using Recyclarr.TestLibrary.Autofac; using Recyclarr.Tests.TestLibrary; using Recyclarr.TrashGuide.CustomFormat; @@ -9,6 +11,12 @@ namespace Recyclarr.IntegrationTests; [Parallelizable(ParallelScope.All)] public class CustomFormatLoaderIntegrationTest : IntegrationTestFixture { + protected override void RegisterStubsAndMocks(ContainerBuilder builder) + { + base.RegisterStubsAndMocks(builder); + builder.RegisterMockFor(); + } + [Test] public void Get_custom_format_json_works() { @@ -26,4 +34,24 @@ public class CustomFormatLoaderIntegrationTest : IntegrationTestFixture NewCf.Data("second", "2") }, o => o.Excluding(x => x.Type == typeof(JsonElement))); } + + [Test] + public void Categorize_by_file_name() + { + var categoryParser = Resolve(); + categoryParser.Parse(default!).ReturnsForAnyArgs(new[] + { + new CustomFormatCategoryItem("Streaming Services", "iTunes", "iT") + }); + + Fs.AddFile("it.json", new MockFileData("""{"name":"iT"}""")); + Fs.AddEmptyFile("collection_of_cfs.md"); + + var sut = Resolve(); + + var dir = Fs.CurrentDirectory(); + var results = sut.LoadAllCustomFormatsAtPaths(new[] {dir}, dir.File("collection_of_cfs.md")); + + results.Should().ContainSingle().Which.Category.Should().Be("Streaming Services"); + } }