diff --git a/src/Recyclarr.Tests/Migration/Steps/MigrateTrashUpdaterAppDataDirTest.cs b/src/Recyclarr.Tests/Migration/Steps/MigrateTrashUpdaterAppDataDirTest.cs new file mode 100644 index 00000000..95dd2738 --- /dev/null +++ b/src/Recyclarr.Tests/Migration/Steps/MigrateTrashUpdaterAppDataDirTest.cs @@ -0,0 +1,60 @@ +using System.IO.Abstractions.TestingHelpers; +using System.Text.RegularExpressions; +using AutoFixture.NUnit3; +using FluentAssertions; +using NSubstitute; +using NUnit.Framework; +using Recyclarr.Migration; +using Recyclarr.Migration.Steps; +using Serilog; +using TestLibrary.AutoFixture; + +namespace Recyclarr.Tests.Migration.Steps; + +[TestFixture] +[Parallelizable(ParallelScope.All)] +public class MigrateTrashUpdaterAppDataDirTest +{ + private static readonly string BasePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + + [Test, AutoMockData] + public void Migration_check_returns_true_if_trash_updater_dir_exists( + [Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs, + MigrateTrashUpdaterAppDataDir sut) + { + fs.AddDirectory(Path.Combine(BasePath, "trash-updater")); + sut.CheckIfNeeded().Should().BeTrue(); + } + + [Test, AutoMockData] + public void Migration_check_returns_false_if_trash_updater_dir_doesnt_exists( + [Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs, + MigrateTrashUpdaterAppDataDir sut) + { + sut.CheckIfNeeded().Should().BeFalse(); + } + + [Test, AutoMockData] + public void Migration_throws_if_recyclarr_dir_already_exists( + [Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs, + MigrateTrashUpdaterAppDataDir sut) + { + fs.AddDirectory(Path.Combine(BasePath, "recyclarr")); + + var act = () => sut.Execute(Substitute.For()); + + act.Should().Throw().WithMessage("*already exist*"); + } + + [Test, AutoMockData] + public void Migration_success( + [Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs, + MigrateTrashUpdaterAppDataDir sut) + { + fs.AddDirectory(Path.Combine(BasePath, "trash-updater")); + + sut.Execute(Substitute.For()); + + fs.AllDirectories.Should().ContainSingle(x => Regex.IsMatch(x, @"[/\\]recyclarr$")); + } +} diff --git a/src/Recyclarr/AppPaths.cs b/src/Recyclarr/AppPaths.cs index 07e0cbc0..4471a7ee 100644 --- a/src/Recyclarr/AppPaths.cs +++ b/src/Recyclarr/AppPaths.cs @@ -3,7 +3,7 @@ internal static class AppPaths { public static string AppDataPath { get; } = - Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "trash-updater"); + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "recyclarr"); public static string DefaultConfigPath { get; } = Path.Combine(AppContext.BaseDirectory, "recyclarr.yml"); diff --git a/src/Recyclarr/Migration/Steps/MigrateTrashUpdaterAppDataDir.cs b/src/Recyclarr/Migration/Steps/MigrateTrashUpdaterAppDataDir.cs new file mode 100644 index 00000000..1574d13c --- /dev/null +++ b/src/Recyclarr/Migration/Steps/MigrateTrashUpdaterAppDataDir.cs @@ -0,0 +1,49 @@ +using System.IO.Abstractions; +using JetBrains.Annotations; +using Serilog; + +namespace Recyclarr.Migration.Steps; + +/// +/// Rename `trash.yml` to `recyclarr.yml`. +/// +/// +/// Implemented on 4/30/2022. +/// +[UsedImplicitly] +public class MigrateTrashUpdaterAppDataDir : IMigrationStep +{ + private readonly IFileSystem _fileSystem; + + private readonly string _oldPath = + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "trash-updater"); + + // Do not use AppPaths class here since that may change yet again in the future and break this migration step. + private readonly string _newPath = + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "recyclarr"); + + public MigrateTrashUpdaterAppDataDir(IFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + + public int Order => 20; + + public string Description => "Rename app data directory from 'trash-updater' to 'recyclarr'"; + + public bool CheckIfNeeded() => _fileSystem.Directory.Exists(_oldPath); + + public void Execute(ILogger log) + { + try + { + _fileSystem.Directory.Move(_oldPath, _newPath); + log.Information("Migration: App data directory renamed from {Old} to {New}", _oldPath, _newPath); + } + catch (IOException) + { + throw new MigrationException(Description, + $"Unable to move due to IO Exception (does the '${_newPath}' directory already exist?)"); + } + } +} diff --git a/src/Recyclarr/Migration/Steps/MigrateTrashYml.cs b/src/Recyclarr/Migration/Steps/MigrateTrashYml.cs index ad6d3a61..67165ad0 100644 --- a/src/Recyclarr/Migration/Steps/MigrateTrashYml.cs +++ b/src/Recyclarr/Migration/Steps/MigrateTrashYml.cs @@ -24,7 +24,7 @@ public class MigrateTrashYml : IMigrationStep _fileSystem = fileSystem; } - public int Order => 1; + public int Order => 10; public string Description => "Migration from 'trash.yml' to 'recyclarr.yml'";