parent
3fa3231f00
commit
db6cf7f897
@ -0,0 +1,101 @@
|
||||
using System.IO.Abstractions;
|
||||
using Autofac;
|
||||
using Autofac.Features.ResolveAnything;
|
||||
using Recyclarr.Compatibility;
|
||||
using Recyclarr.Platform;
|
||||
using Recyclarr.TestLibrary;
|
||||
using Recyclarr.TestLibrary.Autofac;
|
||||
using Recyclarr.VersionControl;
|
||||
using Spectre.Console;
|
||||
using Spectre.Console.Testing;
|
||||
|
||||
namespace Recyclarr.Cli.IntegrationTests;
|
||||
|
||||
public abstract class IntegrationTestFixture2 : IDisposable
|
||||
{
|
||||
private readonly Lazy<ILifetimeScope> _container;
|
||||
protected ILifetimeScope Container => _container.Value;
|
||||
protected MockFileSystem Fs { get; }
|
||||
protected TestConsole Console { get; } = new();
|
||||
protected TestableLogger Logger { get; } = new();
|
||||
|
||||
protected IntegrationTestFixture2()
|
||||
{
|
||||
Fs = new MockFileSystem(new MockFileSystemOptions
|
||||
{
|
||||
CreateDefaultTempDir = false
|
||||
});
|
||||
|
||||
// Use Lazy because we shouldn't invoke virtual methods at construction time
|
||||
_container = new Lazy<ILifetimeScope>(() =>
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
RegisterTypes(builder);
|
||||
RegisterStubsAndMocks(builder);
|
||||
builder.RegisterSource<AnyConcreteTypeNotAlreadyRegisteredSource>();
|
||||
return builder.Build();
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register "real" types (usually Module-derived classes from other projects). This call happens
|
||||
/// before
|
||||
/// RegisterStubsAndMocks().
|
||||
/// </summary>
|
||||
protected virtual void RegisterTypes(ContainerBuilder builder)
|
||||
{
|
||||
CompositionRoot.Setup(builder);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override registrations made in the RegisterTypes() method. This method is called after
|
||||
/// RegisterTypes().
|
||||
/// </summary>
|
||||
protected virtual void RegisterStubsAndMocks(ContainerBuilder builder)
|
||||
{
|
||||
builder.RegisterType<StubEnvironment>().As<IEnvironment>();
|
||||
|
||||
builder.RegisterInstance(Fs).As<IFileSystem>();
|
||||
builder.RegisterInstance(Console).As<IAnsiConsole>();
|
||||
builder.RegisterInstance(Logger).As<ILogger>();
|
||||
|
||||
builder.RegisterMockFor<IGitRepository>();
|
||||
builder.RegisterMockFor<IGitRepositoryFactory>();
|
||||
builder.RegisterMockFor<IServiceInformation>(m =>
|
||||
{
|
||||
// By default, choose some extremely high number so that all the newest features are enabled.
|
||||
m.GetVersion(default!).ReturnsForAnyArgs(_ => new Version("99.0.0.0"));
|
||||
});
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void DumpLogsToConsole()
|
||||
{
|
||||
foreach (var line in Console.Lines)
|
||||
{
|
||||
System.Console.WriteLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
protected T Resolve<T>() where T : notnull
|
||||
{
|
||||
return Container.Resolve<T>();
|
||||
}
|
||||
|
||||
// ReSharper disable once VirtualMemberNeverOverridden.Global
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposing || !_container.IsValueCreated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_container.Value.Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
using System.IO.Abstractions;
|
||||
using Recyclarr.Common.Extensions;
|
||||
using Recyclarr.Platform;
|
||||
|
||||
namespace Recyclarr.Cli.IntegrationTests;
|
||||
|
||||
public class StubEnvironment(IFileSystem fs) : IEnvironment
|
||||
{
|
||||
private readonly Dictionary<string, string> _env = new();
|
||||
|
||||
private IDirectoryInfo BuildSpecialFolderPath(Environment.SpecialFolder folder)
|
||||
{
|
||||
return fs.CurrentDirectory().SubDir("special_folder", folder.ToString());
|
||||
}
|
||||
|
||||
public string GetFolderPath(Environment.SpecialFolder folder)
|
||||
{
|
||||
return BuildSpecialFolderPath(folder).FullName;
|
||||
}
|
||||
|
||||
public string GetFolderPath(Environment.SpecialFolder folder, Environment.SpecialFolderOption folderOption)
|
||||
{
|
||||
var path = BuildSpecialFolderPath(folder);
|
||||
if (folderOption == Environment.SpecialFolderOption.Create)
|
||||
{
|
||||
path.Create();
|
||||
}
|
||||
|
||||
return path.FullName;
|
||||
}
|
||||
|
||||
public void AddEnvironmentVariable(string variable, string value)
|
||||
{
|
||||
_env.Add(variable, value);
|
||||
}
|
||||
|
||||
public string? GetEnvironmentVariable(string variable)
|
||||
{
|
||||
return _env.GetValueOrDefault(variable);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
using AutoMapper;
|
||||
|
||||
namespace Recyclarr.Cli.IntegrationTests;
|
||||
namespace Recyclarr.Cli.IntegrationTests.Tests;
|
||||
|
||||
[TestFixture]
|
||||
internal class AutoMapperConfigurationTest : CliIntegrationFixture
|
@ -1,9 +1,8 @@
|
||||
using System.IO.Abstractions;
|
||||
using Recyclarr.Cli.Console.Setup;
|
||||
using Recyclarr.Settings;
|
||||
using Recyclarr.TestLibrary;
|
||||
|
||||
namespace Recyclarr.Cli.IntegrationTests;
|
||||
namespace Recyclarr.Cli.IntegrationTests.Tests;
|
||||
|
||||
[TestFixture]
|
||||
internal class BaseCommandSetupIntegrationTest : CliIntegrationFixture
|
@ -0,0 +1,55 @@
|
||||
using System.IO.Abstractions;
|
||||
using Autofac;
|
||||
using Recyclarr.Cli.Console.Commands;
|
||||
using Recyclarr.Common.Extensions;
|
||||
using Recyclarr.Platform;
|
||||
using Recyclarr.TestLibrary.Autofac;
|
||||
using Recyclarr.TestLibrary.AutoFixture;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Recyclarr.Cli.IntegrationTests.Tests;
|
||||
|
||||
[TestFixture]
|
||||
public class PlatformIntegrationTest : IntegrationTestFixture2
|
||||
{
|
||||
protected override void RegisterStubsAndMocks(ContainerBuilder builder)
|
||||
{
|
||||
base.RegisterStubsAndMocks(builder);
|
||||
builder.RegisterMockFor<IRuntimeInformation>();
|
||||
}
|
||||
|
||||
[Test, AutoMockData]
|
||||
public async Task Migrate_app_data_on_mac_os_dotnet8(CommandContext ctx)
|
||||
{
|
||||
// The "old" app data directory has to be in place before we create SyncCommand, since we want the
|
||||
// automatic/transparent move of that directory to happen before the migration system checks it.
|
||||
var env = Resolve<IEnvironment>();
|
||||
|
||||
var oldPath = new AppPaths(Fs.DirectoryInfo
|
||||
.New(env.GetFolderPath(Environment.SpecialFolder.UserProfile))
|
||||
.SubDir(".config", AppPaths.DefaultAppDataDirectoryName));
|
||||
|
||||
Fs.AddFile(oldPath.ConfigsDirectory.File("test.yml"), new MockFileData(
|
||||
"""
|
||||
radarr:
|
||||
test_instance:
|
||||
base_url: http://localhost:1000
|
||||
api_key: key
|
||||
"""));
|
||||
|
||||
// Fs.AddEmptyFile(oldPath.File("test.yml"));
|
||||
|
||||
// The runtime info mocking has to happen before the instantiation of SyncCommand.
|
||||
var runtimeInfo = Resolve<IRuntimeInformation>();
|
||||
runtimeInfo.IsPlatformOsx().Returns(true);
|
||||
|
||||
var cmd = Resolve<SyncCommand>();
|
||||
var settings = new SyncCommand.CliSettings();
|
||||
var newPath = Resolve<IAppPaths>();
|
||||
|
||||
var returnCode = await cmd.ExecuteAsync(ctx, settings);
|
||||
|
||||
returnCode.Should().Be(0);
|
||||
Fs.AllFiles.Should().Contain(newPath.ConfigsDirectory.SubDir("configs").File("test.yml").FullName);
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
using System.IO.Abstractions;
|
||||
using Recyclarr.Settings;
|
||||
|
||||
namespace Recyclarr.Cli.IntegrationTests;
|
||||
namespace Recyclarr.Cli.IntegrationTests.Tests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ServiceCompatibilityIntegrationTest : CliIntegrationFixture
|
Loading…
Reference in new issue