From adeb478777d37531a3578c9d1c0b5292683a9084 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 22 Jan 2023 14:44:13 -0600 Subject: [PATCH] fix: Exception when either Sonarr or Radarr configs are empty --- CHANGELOG.md | 4 ++ .../Config/Parsing/ConfigRegistryTest.cs | 42 +++++++++++++++++++ .../Config/Parsing/ConfigRegistry.cs | 7 +++- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/Recyclarr.TrashLib.Tests/Config/Parsing/ConfigRegistryTest.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 332acdb5..d1e2e240 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Environment variables may now be used in YAML configuration (#145). +### Fixed + +- Exception when there's not configuration for both Sonarr and Radarr together. + ## [4.2.0] - 2023-01-13 ### Added diff --git a/src/Recyclarr.TrashLib.Tests/Config/Parsing/ConfigRegistryTest.cs b/src/Recyclarr.TrashLib.Tests/Config/Parsing/ConfigRegistryTest.cs new file mode 100644 index 00000000..f80f5fc7 --- /dev/null +++ b/src/Recyclarr.TrashLib.Tests/Config/Parsing/ConfigRegistryTest.cs @@ -0,0 +1,42 @@ +using FluentAssertions; +using NUnit.Framework; +using Recyclarr.TrashLib.Config; +using Recyclarr.TrashLib.Config.Parsing; +using Recyclarr.TrashLib.Services.Radarr.Config; +using Recyclarr.TrashLib.Services.Sonarr.Config; + +namespace Recyclarr.TrashLib.Tests.Config.Parsing; + +[TestFixture] +[Parallelizable(ParallelScope.All)] +public class ConfigRegistryTest +{ + [Test] + public void Get_configs_by_type() + { + var configs = new[] + { + new SonarrConfiguration {InstanceName = "one"}, + new SonarrConfiguration {InstanceName = "two"} + }; + + var sut = new ConfigRegistry(); + sut.Add(SupportedServices.Sonarr, configs[0]); + sut.Add(SupportedServices.Sonarr, configs[1]); + + var result = sut.GetConfigsOfType(SupportedServices.Sonarr); + + result.Should().Equal(configs); + } + + [Test] + public void Get_empty_collection_when_no_configs_of_type() + { + var sut = new ConfigRegistry(); + sut.Add(SupportedServices.Sonarr, new SonarrConfiguration()); + + var result = sut.GetConfigsOfType(SupportedServices.Radarr); + + result.Should().BeEmpty(); + } +} diff --git a/src/Recyclarr.TrashLib/Config/Parsing/ConfigRegistry.cs b/src/Recyclarr.TrashLib/Config/Parsing/ConfigRegistry.cs index 3be726f8..504d2b62 100644 --- a/src/Recyclarr.TrashLib/Config/Parsing/ConfigRegistry.cs +++ b/src/Recyclarr.TrashLib/Config/Parsing/ConfigRegistry.cs @@ -14,7 +14,12 @@ public class ConfigRegistry : IConfigRegistry public IReadOnlyCollection GetConfigsOfType(SupportedServices serviceType) where T : ServiceConfiguration { - return _configs[serviceType].Cast().ToList(); + if (_configs.TryGetValue(serviceType, out var configs)) + { + return configs.Cast().ToList(); + } + + return Array.Empty(); } public bool DoesConfigExist(string name)