fix: Do not warn about empty configs when they aren't really empty

This was caused by a logic error that thought that a file was empty just
because it didn't find any configuration data relevant to the current
subcommand. For example, a config file that only has Radarr config in it
when you run `recyclarr sonarr`.
pull/151/head
Robert Dailey 1 year ago
parent ebb33f3328
commit ab352f6a4c

@ -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

@ -275,12 +275,37 @@ sonarr:
}
[Test, AutoMockData]
public void Yaml_file_with_only_comment_should_be_skipped(ConfigurationLoader<TestConfig> sut)
public void Throw_when_yaml_file_only_has_comment(ConfigurationLoader<TestConfig> 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<EmptyYamlException>();
}
[Test, AutoMockData]
public void Throw_when_yaml_file_is_empty(ConfigurationLoader<TestConfig> sut)
{
const string testYml = "";
var act = () => sut.LoadFromStream(new StringReader(testYml), "fubar");
act.Should().Throw<EmptyYamlException>();
}
[Test, AutoMockData]
public void Do_not_throw_when_file_not_empty_but_has_no_desired_sections(ConfigurationLoader<TestConfig> sut)
{
const string testYml = @"
not_wanted:
instance:
base_url: abc
api_key: xyz
";
var act = () => sut.LoadFromStream(new StringReader(testYml), "fubar");
act.Should().NotThrow();
}
}

@ -45,13 +45,12 @@ public class ConfigurationLoader<T> : IConfigurationLoader<T>
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<T>();
}
catch (YamlException e)
{
@ -81,14 +80,14 @@ public class ConfigurationLoader<T> : IConfigurationLoader<T>
if (parser.Current is StreamEnd)
{
_log.Debug("Skipping this config due to StreamEnd");
return Array.Empty<T>();
throw new EmptyYamlException();
}
parser.Consume<DocumentStart>();
if (parser.Current is DocumentEnd)
{
_log.Debug("Skipping this config due to DocumentEnd");
return Array.Empty<T>();
throw new EmptyYamlException();
}
return ParseAllSections(parser, requestedSection);

@ -0,0 +1,5 @@
namespace Recyclarr.Config;
public class EmptyYamlException : Exception
{
}
Loading…
Cancel
Save