A settings file, `settings.yml` is now available which stores global configuration such as the clone URL for the Github Trash repository.pull/47/head
parent
b6b6ebda9e
commit
2fe29f3b16
@ -0,0 +1,69 @@
|
|||||||
|
using System.IO.Abstractions.TestingHelpers;
|
||||||
|
using AutoFixture.NUnit3;
|
||||||
|
using FluentAssertions;
|
||||||
|
using NSubstitute;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using TestLibrary.AutoFixture;
|
||||||
|
using TrashLib.Config;
|
||||||
|
using TrashLib.Config.Settings;
|
||||||
|
using TrashLib.Radarr.Config;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
|
namespace TrashLib.Tests.Config.Settings;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
[Parallelizable(ParallelScope.All)]
|
||||||
|
public class SettingsPersisterTest
|
||||||
|
{
|
||||||
|
[Test, AutoMockData]
|
||||||
|
public void Load_should_create_settings_file_if_not_exists(
|
||||||
|
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fileSystem,
|
||||||
|
[Frozen] IResourcePaths paths,
|
||||||
|
SettingsPersister sut)
|
||||||
|
{
|
||||||
|
paths.SettingsPath.Returns("test_path");
|
||||||
|
|
||||||
|
sut.Load();
|
||||||
|
|
||||||
|
fileSystem.AllFiles.Should().ContainSingle(x => x.EndsWith(paths.SettingsPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, AutoMockData]
|
||||||
|
public void Load_defaults_when_file_does_not_exist(
|
||||||
|
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fileSystem,
|
||||||
|
[Frozen(Matching.ImplementedInterfaces)] YamlSerializerFactory serializerFactory,
|
||||||
|
[Frozen(Matching.ImplementedInterfaces)] SettingsProvider settingsProvider,
|
||||||
|
[Frozen] IResourcePaths paths,
|
||||||
|
SettingsPersister sut)
|
||||||
|
{
|
||||||
|
paths.SettingsPath.Returns("test_path");
|
||||||
|
|
||||||
|
sut.Load();
|
||||||
|
|
||||||
|
var expectedSettings = new SettingsValues();
|
||||||
|
settingsProvider.Settings.Should().BeEquivalentTo(expectedSettings);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, AutoMockData]
|
||||||
|
public void Load_data_correctly_when_file_exists(
|
||||||
|
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fileSystem,
|
||||||
|
[Frozen] IYamlSerializerFactory serializerFactory,
|
||||||
|
[Frozen] IResourcePaths paths,
|
||||||
|
SettingsPersister sut)
|
||||||
|
{
|
||||||
|
// 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.
|
||||||
|
var expectedYamlData = @"
|
||||||
|
repository:
|
||||||
|
clone_url: http://the_url.com
|
||||||
|
";
|
||||||
|
var deserializer = Substitute.For<IDeserializer>();
|
||||||
|
serializerFactory.CreateDeserializer().Returns(deserializer);
|
||||||
|
paths.SettingsPath.Returns("test_path");
|
||||||
|
fileSystem.AddFile(paths.SettingsPath, new MockFileData(expectedYamlData));
|
||||||
|
|
||||||
|
sut.Load();
|
||||||
|
|
||||||
|
deserializer.Received().Deserialize<SettingsValues>(expectedYamlData);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace TrashLib.Config.Settings;
|
||||||
|
|
||||||
|
public interface ISettingsPersister
|
||||||
|
{
|
||||||
|
void Load();
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace TrashLib.Config.Settings;
|
||||||
|
|
||||||
|
public interface ISettingsProvider
|
||||||
|
{
|
||||||
|
SettingsValues Settings { get; }
|
||||||
|
void UseSettings(SettingsValues settings);
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
using System.IO.Abstractions;
|
||||||
|
using TrashLib.Radarr.Config;
|
||||||
|
|
||||||
|
namespace TrashLib.Config.Settings;
|
||||||
|
|
||||||
|
public class SettingsPersister : ISettingsPersister
|
||||||
|
{
|
||||||
|
private readonly IResourcePaths _paths;
|
||||||
|
private readonly ISettingsProvider _settingsProvider;
|
||||||
|
private readonly IYamlSerializerFactory _serializerFactory;
|
||||||
|
private readonly IFileSystem _fileSystem;
|
||||||
|
|
||||||
|
public SettingsPersister(
|
||||||
|
IResourcePaths paths,
|
||||||
|
ISettingsProvider settingsProvider,
|
||||||
|
IYamlSerializerFactory serializerFactory,
|
||||||
|
IFileSystem fileSystem)
|
||||||
|
{
|
||||||
|
_paths = paths;
|
||||||
|
_settingsProvider = settingsProvider;
|
||||||
|
_serializerFactory = serializerFactory;
|
||||||
|
_fileSystem = fileSystem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Load()
|
||||||
|
{
|
||||||
|
var deserializer = _serializerFactory.CreateDeserializer();
|
||||||
|
var settings = deserializer.Deserialize<SettingsValues?>(LoadOrCreateSettingsFile()) ?? new SettingsValues();
|
||||||
|
_settingsProvider.UseSettings(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string LoadOrCreateSettingsFile()
|
||||||
|
{
|
||||||
|
if (!_fileSystem.File.Exists(_paths.SettingsPath))
|
||||||
|
{
|
||||||
|
CreateDefaultSettingsFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
return _fileSystem.File.ReadAllText(_paths.SettingsPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CreateDefaultSettingsFile()
|
||||||
|
{
|
||||||
|
const string fileData =
|
||||||
|
"# Edit this file to customize the behavior of Trash Updater beyond its defaults\n" +
|
||||||
|
"# For the settings file reference guide, visit the link to the wiki below:\n" +
|
||||||
|
"# https://github.com/rcdailey/trash-updater/wiki/Settings-Reference\n";
|
||||||
|
|
||||||
|
_fileSystem.File.WriteAllText(_paths.SettingsPath, fileData);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
namespace TrashLib.Config.Settings;
|
||||||
|
|
||||||
|
public class SettingsProvider : ISettingsProvider
|
||||||
|
{
|
||||||
|
public SettingsValues Settings { get; private set; } = new();
|
||||||
|
|
||||||
|
public void UseSettings(SettingsValues settings)
|
||||||
|
{
|
||||||
|
Settings = settings;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
namespace TrashLib.Config.Settings;
|
||||||
|
|
||||||
|
public record TrashRepository
|
||||||
|
{
|
||||||
|
public string CloneUrl { get; init; } = "https://github.com/TRaSH-/Guides.git";
|
||||||
|
}
|
||||||
|
|
||||||
|
public record SettingsValues
|
||||||
|
{
|
||||||
|
public TrashRepository Repository { get; init; } = new();
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
This page contains the YAML reference for `settings.yml`. This file is located in the following
|
||||||
|
locations depending on your platform:
|
||||||
|
|
||||||
|
| Platform | Location |
|
||||||
|
| -------- | ---------------------------------------------------------- |
|
||||||
|
| Windows | `%APPDATA%\trash-updater\settings.yml` |
|
||||||
|
| Linux | `~/.config/trash-updater/settings.yml` |
|
||||||
|
| MacOS | `~/Library/Application Support/trash-updater/settings.yml` |
|
||||||
|
|
||||||
|
Settings in this file affect the behavior of Trash Updater regardless of instance-specific
|
||||||
|
configuration for Radarr and Sonarr.
|
||||||
|
|
||||||
|
If this file does not exist, Trash Updater will create it for you. Starting out, this file will be
|
||||||
|
empty and default behavior will be used. There is absolutely no need to touch this file unless you
|
||||||
|
have a specific reason to. It is recommended that you only add the specific properties for the
|
||||||
|
customizations you need and leave the rest alone.
|
||||||
|
|
||||||
|
# YAML Reference
|
||||||
|
|
||||||
|
Table of Contents
|
||||||
|
|
||||||
|
- [Repository Settings](#repository-settings)
|
||||||
|
|
||||||
|
## Repository Settings
|
||||||
|
|
||||||
|
```yml
|
||||||
|
repository:
|
||||||
|
clone_url: https://github.com/TRaSH-/Guides.git
|
||||||
|
```
|
||||||
|
|
||||||
|
- `clone_url`<br>
|
||||||
|
A URL compatible with `git clone` that is used to clone the [Trash Guides
|
||||||
|
repository](official_repo). This setting exists for enthusiasts that may want to instead have
|
||||||
|
Trash Updater pull data from a fork instead of the official repository.
|
||||||
|
|
||||||
|
[official_repo]: https://github.com/TRaSH-/Guides
|
Loading…
Reference in new issue