Simplify the factory pattern so that it creates settings more directly instead of using a silly setter method. Also implemented `IntegrationFixture` for easier integration testing.pull/124/head
parent
1f70e7fb61
commit
f01edc5700
@ -0,0 +1,37 @@
|
||||
using System.IO.Abstractions;
|
||||
using System.IO.Abstractions.TestingHelpers;
|
||||
using Autofac;
|
||||
using CliFx.Infrastructure;
|
||||
using NUnit.Framework;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace Recyclarr.TestLibrary;
|
||||
|
||||
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
|
||||
public abstract class IntegrationFixture
|
||||
{
|
||||
private readonly ILifetimeScope _container;
|
||||
|
||||
protected IntegrationFixture()
|
||||
{
|
||||
var compRoot = new CompositionRoot();
|
||||
_container = compRoot.Setup(default, new FakeConsole(), LogEventLevel.Debug).Container
|
||||
.BeginLifetimeScope(builder =>
|
||||
{
|
||||
builder.RegisterInstance(Fs).As<IFileSystem>();
|
||||
});
|
||||
}
|
||||
|
||||
protected MockFileSystem Fs { get; } = new();
|
||||
|
||||
protected T Resolve<T>(Action<ContainerBuilder> customRegistrations) where T : notnull
|
||||
{
|
||||
var childScope = _container.BeginLifetimeScope(customRegistrations);
|
||||
return childScope.Resolve<T>();
|
||||
}
|
||||
|
||||
protected T Resolve<T>() where T : notnull
|
||||
{
|
||||
return _container.Resolve<T>();
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
using System.IO.Abstractions.TestingHelpers;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using Recyclarr.TestLibrary;
|
||||
using TrashLib.Config.Settings;
|
||||
using TrashLib.Startup;
|
||||
|
||||
namespace Recyclarr.Tests;
|
||||
|
||||
[TestFixture]
|
||||
[Parallelizable(ParallelScope.All)]
|
||||
public class ServiceCompatibilityIntegrationTest : IntegrationFixture
|
||||
{
|
||||
[Test]
|
||||
public void Load_data_correctly_when_file_exists()
|
||||
{
|
||||
var sut = Resolve<ISettingsProvider>();
|
||||
var paths = Resolve<IAppPaths>();
|
||||
|
||||
// For this test, it doesn't really matter if the YAML data matches what SettingsValue expects;
|
||||
// this test only ensures that the data deserialized is from the actual correct file.
|
||||
const string yamlData = @"
|
||||
repository:
|
||||
clone_url: http://the_url.com
|
||||
";
|
||||
|
||||
Fs.AddFile(paths.SettingsPath.FullName, new MockFileData(yamlData));
|
||||
|
||||
var settings = sut.Settings;
|
||||
|
||||
settings.Repository.CloneUrl.Should().Be("http://the_url.com");
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using TestLibrary.AutoFixture;
|
||||
using TrashLib.Config.Settings;
|
||||
|
||||
namespace TrashLib.Tests.Config.Settings;
|
||||
|
||||
[TestFixture]
|
||||
[Parallelizable(ParallelScope.All)]
|
||||
public class SettingsProviderTest
|
||||
{
|
||||
[Test, AutoMockData]
|
||||
public void Property_returns_same_value_from_set_method(SettingsProvider sut)
|
||||
{
|
||||
var settings = new SettingsValues();
|
||||
sut.UseSettings(settings);
|
||||
sut.Settings.Should().Be(settings);
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
namespace TrashLib.Config.Settings;
|
||||
|
||||
public interface ISettingsPersister
|
||||
{
|
||||
void Load();
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
using TrashLib.Startup;
|
||||
|
||||
namespace TrashLib.Config.Settings;
|
||||
|
||||
public class SettingsPersister : ISettingsPersister
|
||||
{
|
||||
private readonly IAppPaths _paths;
|
||||
private readonly ISettingsProvider _settingsProvider;
|
||||
private readonly IYamlSerializerFactory _serializerFactory;
|
||||
|
||||
public SettingsPersister(
|
||||
IAppPaths paths,
|
||||
ISettingsProvider settingsProvider,
|
||||
IYamlSerializerFactory serializerFactory)
|
||||
{
|
||||
_paths = paths;
|
||||
_settingsProvider = settingsProvider;
|
||||
_serializerFactory = serializerFactory;
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
var deserializer = _serializerFactory.CreateDeserializer();
|
||||
var settings = deserializer.Deserialize<SettingsValues?>(LoadOrCreateSettingsFile()) ?? new SettingsValues();
|
||||
_settingsProvider.UseSettings(settings);
|
||||
}
|
||||
|
||||
private string LoadOrCreateSettingsFile()
|
||||
{
|
||||
if (!_paths.SettingsPath.Exists)
|
||||
{
|
||||
CreateDefaultSettingsFile();
|
||||
}
|
||||
|
||||
using var stream = _paths.SettingsPath.OpenText();
|
||||
return stream.ReadToEnd();
|
||||
}
|
||||
|
||||
private void CreateDefaultSettingsFile()
|
||||
{
|
||||
const string fileData =
|
||||
"# yaml-language-server: $schema=https://raw.githubusercontent.com/recyclarr/recyclarr/master/schemas/settings-schema.json\n" +
|
||||
"\n" +
|
||||
"# Edit this file to customize the behavior of Recyclarr beyond its defaults\n" +
|
||||
"# For the settings file reference guide, visit the link to the wiki below:\n" +
|
||||
"# https://github.com/recyclarr/recyclarr/wiki/Settings-Reference\n";
|
||||
|
||||
_paths.SettingsPath.Directory.Create();
|
||||
using var stream = _paths.SettingsPath.CreateText();
|
||||
stream.Write(fileData);
|
||||
}
|
||||
}
|
@ -1,11 +1,43 @@
|
||||
using TrashLib.Startup;
|
||||
|
||||
namespace TrashLib.Config.Settings;
|
||||
|
||||
public class SettingsProvider : ISettingsProvider
|
||||
{
|
||||
public SettingsValues Settings { get; private set; } = new();
|
||||
public SettingsValues Settings => _settings.Value;
|
||||
|
||||
private readonly IAppPaths _paths;
|
||||
private readonly Lazy<SettingsValues> _settings;
|
||||
|
||||
public SettingsProvider(IAppPaths paths, IYamlSerializerFactory serializerFactory)
|
||||
{
|
||||
_paths = paths;
|
||||
_settings = new Lazy<SettingsValues>(() => LoadOrCreateSettingsFile(serializerFactory));
|
||||
}
|
||||
|
||||
public void UseSettings(SettingsValues settings)
|
||||
private SettingsValues LoadOrCreateSettingsFile(IYamlSerializerFactory serializerFactory)
|
||||
{
|
||||
Settings = settings;
|
||||
if (!_paths.SettingsPath.Exists)
|
||||
{
|
||||
CreateDefaultSettingsFile();
|
||||
}
|
||||
|
||||
using var stream = _paths.SettingsPath.OpenText();
|
||||
var deserializer = serializerFactory.CreateDeserializer();
|
||||
return deserializer.Deserialize<SettingsValues?>(stream.ReadToEnd()) ?? new SettingsValues();
|
||||
}
|
||||
|
||||
private void CreateDefaultSettingsFile()
|
||||
{
|
||||
const string fileData =
|
||||
"# yaml-language-server: $schema=https://raw.githubusercontent.com/recyclarr/recyclarr/master/schemas/settings-schema.json\n" +
|
||||
"\n" +
|
||||
"# Edit this file to customize the behavior of Recyclarr beyond its defaults\n" +
|
||||
"# For the settings file reference guide, visit the link to the wiki below:\n" +
|
||||
"# https://github.com/recyclarr/recyclarr/wiki/Settings-Reference\n";
|
||||
|
||||
_paths.SettingsPath.Directory.Create();
|
||||
using var stream = _paths.SettingsPath.CreateText();
|
||||
stream.Write(fileData);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue