From 98e6b4ce596bfd3abd9ef71862df94c5d6aebfef Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Fri, 26 Aug 2022 12:14:00 -0500 Subject: [PATCH] fix(radarr): Remove all trash properties from guide CFs Any property starting with `trash_` is now removed from guide CFs. Fixes #109. --- CHANGELOG.md | 4 +++ .../Guide/LocalRepoRadarrGuideServiceTest.cs | 34 +++++++++++++++++++ .../Guide/LocalRepoRadarrGuideService.cs | 13 ++++--- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3df2fa22..83575c5e 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 + +- Radarr: Custom formats were always showing up as changed in the logs (#109) + ## [2.4.0] - 2022-08-25 ### Added diff --git a/src/TrashLib.Tests/Radarr/CustomFormat/Guide/LocalRepoRadarrGuideServiceTest.cs b/src/TrashLib.Tests/Radarr/CustomFormat/Guide/LocalRepoRadarrGuideServiceTest.cs index eda19a96..16fdfa76 100644 --- a/src/TrashLib.Tests/Radarr/CustomFormat/Guide/LocalRepoRadarrGuideServiceTest.cs +++ b/src/TrashLib.Tests/Radarr/CustomFormat/Guide/LocalRepoRadarrGuideServiceTest.cs @@ -2,9 +2,11 @@ using System.IO.Abstractions; using System.IO.Abstractions.TestingHelpers; using AutoFixture.NUnit3; using FluentAssertions; +using Newtonsoft.Json.Linq; using NSubstitute; using NUnit.Framework; using TestLibrary.AutoFixture; +using TestLibrary.FluentAssertions; using TrashLib.Repo; using TrashLib.Services.Radarr.CustomFormat.Guide; using TrashLib.Startup; @@ -41,4 +43,36 @@ public class LocalRepoRadarrGuideServiceTest NewCf.Data("second", "2") }); } + + [Test, AutoMockData] + public void Trash_properties_are_removed( + [Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs, + [Frozen] IAppPaths appPaths, + [Frozen] IRepoPaths repoPaths, + LocalRepoRadarrGuideService sut) + { + var jsonDir = appPaths.RepoDirectory + .SubDirectory("docs") + .SubDirectory("json") + .SubDirectory("radarr"); + + fs.AddFile(jsonDir.File("first.json").FullName, new MockFileData(@" +{ + 'name':'first', + 'trash_id':'1', + 'trash_foo': 'foo', + 'trash_bar': 'bar', + 'extra': 'e1' +}")); + + repoPaths.RadarrCustomFormatPaths.Returns(new[] {jsonDir}); + + var results = sut.GetCustomFormatData(); + + const string expectedExtraJson = @"{'name':'first','extra': 'e1'}"; + + results.Should() + .ContainSingle().Which.ExtraJson.Should() + .BeEquivalentTo(JObject.Parse(expectedExtraJson), op => op.Using(new JsonEquivalencyStep())); + } } diff --git a/src/TrashLib/Services/Radarr/CustomFormat/Guide/LocalRepoRadarrGuideService.cs b/src/TrashLib/Services/Radarr/CustomFormat/Guide/LocalRepoRadarrGuideService.cs index 2d9e04da..8c138d8e 100644 --- a/src/TrashLib/Services/Radarr/CustomFormat/Guide/LocalRepoRadarrGuideService.cs +++ b/src/TrashLib/Services/Radarr/CustomFormat/Guide/LocalRepoRadarrGuideService.cs @@ -1,6 +1,7 @@ using System.IO.Abstractions; using System.Reactive.Linq; using System.Reactive.Threading.Tasks; +using System.Text.RegularExpressions; using Common.Extensions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -65,12 +66,16 @@ public class LocalRepoRadarrGuideService : IRadarrGuideService if (obj.TryGetValue("trash_score", out var score)) { finalScore = (int) score; - obj.Property("trash_score")?.Remove(); } - // Remove trash_id, it's metadata that is not meant for Radarr itself - // Radarr supposedly drops this anyway, but I prefer it to be removed. - obj.Property("trash_id")?.Remove(); + // Remove any properties starting with "trash_". Those are metadata that are not meant for Radarr itself Radarr + // supposedly drops this anyway, but I prefer it to be removed. ToList() is important here since removing the + // property itself modifies the collection, and we don't want the collection to get modified while still looping + // over it. + foreach (var trashProperty in obj.Properties().Where(x => Regex.IsMatch(x.Name, @"^trash_")).ToList()) + { + trashProperty.Remove(); + } return new CustomFormatData(name, trashId, finalScore, obj); }