fix: Properly handle manually specified path in create-config

pull/76/head
Robert Dailey 2 years ago
parent a727c22b2d
commit bf8b860951

@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- `create-config` would fail with `--path` specified.
## [2.1.1] - 2022-05-29
### Fixed

@ -30,19 +30,4 @@ public class CreateConfigCommandTest
file.Should().NotBeNull();
file.Contents.Should().NotBeEmpty();
}
[Test, AutoMockData]
public async Task CreateConfig_SpecifyPath_FileIsCreated(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
CreateConfigCommand cmd)
{
const string ymlPath = "some/other/path.yml";
cmd.Path = ymlPath;
await cmd.ExecuteAsync(Substitute.For<IConsole>()).ConfigureAwait(false);
var file = fs.GetFile(ymlPath);
file.Should().NotBeNull();
file.Contents.Should().NotBeEmpty();
}
}

@ -0,0 +1,66 @@
using System.IO.Abstractions.TestingHelpers;
using AutoFixture.NUnit3;
using Common;
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;
using Recyclarr.Command.Initialization;
using TestLibrary;
using TestLibrary.AutoFixture;
using TrashLib;
namespace Recyclarr.Tests.Command.Initialization;
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class DefaultAppDataSetupTest
{
[Test, AutoMockData]
public void Initialize_using_default_path(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
[Frozen] IEnvironment env,
[Frozen] IAppPaths paths,
DefaultAppDataSetup sut)
{
paths.DefaultAppDataDirectoryName.Returns("app_data");
env.GetFolderPath(Arg.Any<Environment.SpecialFolder>(), Arg.Any<Environment.SpecialFolderOption>())
.Returns(FileUtils.NormalizePath("base/path"));
sut.SetupDefaultPath(null, false);
paths.Received().SetAppDataPath(FileUtils.NormalizePath("base/path/app_data"));
}
[Test, AutoMockData]
public void Initialize_using_path_override(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
[Frozen] IAppPaths paths,
DefaultAppDataSetup sut)
{
var overridePath = FileUtils.NormalizePath("/override/path");
sut.SetupDefaultPath(overridePath, false);
paths.Received().SetAppDataPath(overridePath);
fs.AllDirectories.Should().Contain(overridePath);
}
[Test, AutoMockData]
public void Force_creation_uses_correct_behavior_when_disabled(
[Frozen] IEnvironment env,
DefaultAppDataSetup sut)
{
sut.SetupDefaultPath(null, false);
env.Received().GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.None);
}
[Test, AutoMockData]
public void Force_creation_uses_correct_behavior_when_enabled(
[Frozen] IEnvironment env,
DefaultAppDataSetup sut)
{
sut.SetupDefaultPath(null, true);
env.Received().GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create);
}
}

@ -2,10 +2,8 @@ using System.IO.Abstractions.TestingHelpers;
using AutoFixture.NUnit3;
using Common;
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;
using Recyclarr.Command;
using Recyclarr.Command.Initialization;
using Recyclarr.Command.Initialization.Init;
using TestLibrary.AutoFixture;
using TrashLib;
@ -16,36 +14,6 @@ namespace Recyclarr.Tests.Command.Initialization.Init;
[Parallelizable(ParallelScope.All)]
public class InitializeAppDataPathTest
{
[Test, AutoMockData]
public void Use_default_app_data_if_not_specified(
[Frozen] IEnvironment env,
[Frozen] IAppPaths paths,
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
[Frozen(Matching.ImplementedInterfaces)] DefaultAppDataSetup appDataSetup,
SonarrCommand cmd,
InitializeAppDataPath sut)
{
paths.DefaultAppDataDirectoryName.Returns("recyclarr");
env.GetFolderPath(Arg.Any<Environment.SpecialFolder>(), Arg.Any<Environment.SpecialFolderOption>())
.Returns("app_data");
sut.Initialize(cmd);
env.Received().GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create);
paths.Received().SetAppDataPath(fs.Path.Combine("app_data", "recyclarr"));
}
[Test, AutoMockData]
public void Use_specified_app_data_if_user_provided(
[Frozen] IAppPaths paths,
SonarrCommand cmd,
InitializeAppDataPath sut)
{
cmd.AppDataDirectory = "path";
sut.Initialize(cmd);
paths.Received().SetAppDataPath("path");
}
[Test, AutoMockData]
public void All_directories_are_created(
[Frozen] IEnvironment env,

@ -1,5 +1,4 @@
using System.IO.Abstractions.TestingHelpers;
using System.Text.RegularExpressions;
using AutoFixture.NUnit3;
using FluentAssertions;
using NUnit.Framework;

@ -36,11 +36,11 @@ public class CreateConfigCommand : ICommand
public ValueTask ExecuteAsync(IConsole console)
{
_appDataSetup.SetupDefaultPath();
_appDataSetup.SetupDefaultPath(Path, true);
var reader = new ResourceDataReader(typeof(Program));
var ymlData = reader.ReadData("config-template.yml");
var path = Path ?? _paths.ConfigPath;
var path = _paths.ConfigPath;
if (_fs.File.Exists(path))
{

@ -1,4 +1,5 @@
using System.IO.Abstractions;
using CliFx.Exceptions;
using Common;
using TrashLib;
@ -17,16 +18,39 @@ public class DefaultAppDataSetup : IDefaultAppDataSetup
_fs = fs;
}
public void SetupDefaultPath(bool forceCreate = false)
public void SetupDefaultPath(string? appDataDirectoryOverride, bool forceCreate)
{
var appData = _env.GetFolderPath(Environment.SpecialFolder.ApplicationData,
forceCreate ? Environment.SpecialFolderOption.Create : Environment.SpecialFolderOption.None);
// If the user did not explicitly specify an app data directory, perform some system introspection to verify if
// the user has a home directory.
if (string.IsNullOrEmpty(appDataDirectoryOverride))
{
// If we can't even get the $HOME directory value, throw an exception. User must explicitly specify it with
// --app-data.
var home = _env.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (string.IsNullOrEmpty(home))
{
throw new CommandException(
"The system does not have a HOME directory, so the application cannot determine where to place " +
"data files. Please use the --app-data option to explicitly set a location for these files.");
}
// Set app data path to application directory value (e.g. `$HOME/.config` on Linux) and ensure it is
// created.
var appData = _env.GetFolderPath(Environment.SpecialFolder.ApplicationData,
forceCreate ? Environment.SpecialFolderOption.Create : Environment.SpecialFolderOption.None);
if (string.IsNullOrEmpty(appData))
{
throw new DirectoryNotFoundException("Unable to find the default app data directory");
}
if (string.IsNullOrEmpty(appData))
_paths.SetAppDataPath(_fs.Path.Combine(appData, _paths.DefaultAppDataDirectoryName));
}
else
{
throw new DirectoryNotFoundException("Unable to find the default app data directory");
// Ensure user-specified app data directory is created and use it.
_fs.Directory.CreateDirectory(appDataDirectoryOverride);
_paths.SetAppDataPath(appDataDirectoryOverride);
}
_paths.SetAppDataPath(_fs.Path.Combine(appData, _paths.DefaultAppDataDirectoryName));
}
}

@ -2,5 +2,5 @@ namespace Recyclarr.Command.Initialization;
public interface IDefaultAppDataSetup
{
void SetupDefaultPath(bool forceCreate = false);
void SetupDefaultPath(string? appDataDirectoryOverride, bool forceCreate);
}

@ -1,6 +1,4 @@
using System.IO.Abstractions;
using CliFx.Exceptions;
using Common;
using TrashLib;
namespace Recyclarr.Command.Initialization.Init;
@ -9,47 +7,18 @@ public class InitializeAppDataPath : IServiceInitializer
{
private readonly IFileSystem _fs;
private readonly IAppPaths _paths;
private readonly IEnvironment _env;
private readonly IDefaultAppDataSetup _appDataSetup;
public InitializeAppDataPath(
IFileSystem fs,
IAppPaths paths,
IEnvironment env,
IDefaultAppDataSetup appDataSetup)
public InitializeAppDataPath(IFileSystem fs, IAppPaths paths, IDefaultAppDataSetup appDataSetup)
{
_fs = fs;
_paths = paths;
_env = env;
_appDataSetup = appDataSetup;
}
public void Initialize(ServiceCommand cmd)
{
// If the user did not explicitly specify an app data directory, perform some system introspection to verify if
// the user has a home directory.
if (string.IsNullOrEmpty(cmd.AppDataDirectory))
{
// If we can't even get the $HOME directory value, throw an exception. User must explicitly specify it with
// --app-data.
var home = _env.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (string.IsNullOrEmpty(home))
{
throw new CommandException(
"The system does not have a HOME directory, so the application cannot determine where to place " +
"data files. Please use the --app-data option to explicitly set a location for these files.");
}
// Set app data path to application directory value (e.g. `$HOME/.config` on Linux) and ensure it is
// created.
_appDataSetup.SetupDefaultPath(true);
}
else
{
// Ensure user-specified app data directory is created and use it.
_fs.Directory.CreateDirectory(cmd.AppDataDirectory);
_paths.SetAppDataPath(cmd.AppDataDirectory);
}
_appDataSetup.SetupDefaultPath(cmd.AppDataDirectory, true);
// Initialize other directories used throughout the application
_fs.Directory.CreateDirectory(_paths.RepoDirectory);

@ -28,7 +28,7 @@ public class MigrateCommand : ICommand
public ValueTask ExecuteAsync(IConsole console)
{
_appDataSetup.SetupDefaultPath();
_appDataSetup.SetupDefaultPath(null, false);
PerformMigrations();
return ValueTask.CompletedTask;
}

Loading…
Cancel
Save