diff --git a/CHANGELOG.md b/CHANGELOG.md index 72bcd790..4a990d61 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 + +- Do not warn about empty configuration YAML files when they aren't really empty. + ## [3.1.0] - 2022-12-10 ### Changed diff --git a/src/Recyclarr.Tests/Config/ConfigurationLoaderTest.cs b/src/Recyclarr.Tests/Config/ConfigurationLoaderTest.cs index dcd07165..88db5368 100644 --- a/src/Recyclarr.Tests/Config/ConfigurationLoaderTest.cs +++ b/src/Recyclarr.Tests/Config/ConfigurationLoaderTest.cs @@ -275,12 +275,37 @@ sonarr: } [Test, AutoMockData] - public void Yaml_file_with_only_comment_should_be_skipped(ConfigurationLoader sut) + public void Throw_when_yaml_file_only_has_comment(ConfigurationLoader sut) { const string testYml = "# YAML with nothing but this comment"; - var result = sut.LoadFromStream(new StringReader(testYml), "fubar"); + var act = () => sut.LoadFromStream(new StringReader(testYml), "fubar"); - result.Should().BeEmpty(); + act.Should().Throw(); + } + + [Test, AutoMockData] + public void Throw_when_yaml_file_is_empty(ConfigurationLoader sut) + { + const string testYml = ""; + + var act = () => sut.LoadFromStream(new StringReader(testYml), "fubar"); + + act.Should().Throw(); + } + + [Test, AutoMockData] + public void Do_not_throw_when_file_not_empty_but_has_no_desired_sections(ConfigurationLoader sut) + { + const string testYml = @" +not_wanted: + instance: + base_url: abc + api_key: xyz +"; + + var act = () => sut.LoadFromStream(new StringReader(testYml), "fubar"); + + act.Should().NotThrow(); } } diff --git a/src/Recyclarr/Config/ConfigurationLoader.cs b/src/Recyclarr/Config/ConfigurationLoader.cs index d8ec7644..2b5ce8c2 100644 --- a/src/Recyclarr/Config/ConfigurationLoader.cs +++ b/src/Recyclarr/Config/ConfigurationLoader.cs @@ -45,13 +45,12 @@ public class ConfigurationLoader : IConfigurationLoader try { using var stream = _fs.File.OpenText(file); - var configs = LoadFromStream(stream, configSection); - if (!configs.Any()) - { - _log.Warning("Configuration file yielded no usable configuration (is it empty?)"); - } - - return configs; + return LoadFromStream(stream, configSection); + } + catch (EmptyYamlException) + { + _log.Warning("Configuration file yielded no usable configuration (is it empty?)"); + return Array.Empty(); } catch (YamlException e) { @@ -81,14 +80,14 @@ public class ConfigurationLoader : IConfigurationLoader if (parser.Current is StreamEnd) { _log.Debug("Skipping this config due to StreamEnd"); - return Array.Empty(); + throw new EmptyYamlException(); } parser.Consume(); if (parser.Current is DocumentEnd) { _log.Debug("Skipping this config due to DocumentEnd"); - return Array.Empty(); + throw new EmptyYamlException(); } return ParseAllSections(parser, requestedSection); diff --git a/src/Recyclarr/Config/EmptyYamlException.cs b/src/Recyclarr/Config/EmptyYamlException.cs new file mode 100644 index 00000000..4c699149 --- /dev/null +++ b/src/Recyclarr/Config/EmptyYamlException.cs @@ -0,0 +1,5 @@ +namespace Recyclarr.Config; + +public class EmptyYamlException : Exception +{ +}