tests: work on writing tests for console functionality
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="Spectre.Console.Testing" Version="0.49.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
|
||||
|
||||
@@ -1,16 +1,82 @@
|
||||
using Spectre.Console;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console.Testing;
|
||||
|
||||
namespace AltGen.Console.Tests.Unit;
|
||||
|
||||
public class AddConfigCommandTests
|
||||
{
|
||||
[Fact]
|
||||
public Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldCreateSettings()
|
||||
readonly Mock<IFileSystem> _fileSystem = new();
|
||||
readonly IAnsiConsole _testConsole = new TestConsole();
|
||||
readonly AddConfigCommand _sut;
|
||||
|
||||
public AddConfigCommandTests()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_sut = new AddConfigCommand(_testConsole, _fileSystem.Object);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldCreateSettings()
|
||||
{
|
||||
var testSettingsPath = "appsettings.json";
|
||||
|
||||
_fileSystem
|
||||
.Setup(static x => x.Path.Combine(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(testSettingsPath);
|
||||
|
||||
_fileSystem
|
||||
.Setup(static x => x.File.Exists(It.IsAny<string>()))
|
||||
.Returns(false);
|
||||
|
||||
var commandSettings = new AddConfigCommand.Settings(_fileSystem.Object)
|
||||
{
|
||||
Provider = "provider",
|
||||
Key = "key",
|
||||
};
|
||||
|
||||
var result = await _sut.ExecuteAsync(null!, commandSettings);
|
||||
|
||||
result.Should().Be(0);
|
||||
|
||||
_fileSystem
|
||||
.Verify(
|
||||
x => x.File.WriteAllTextAsync(testSettingsPath, It.IsAny<string>(), default),
|
||||
Times.Once
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ExecuteAsync_WhenSettingsExist_ItShouldUpdateSettings()
|
||||
public async Task ExecuteAsync_WhenSettingsExist_ItShouldUpdateSettings()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var testSettingsPath = "appsettings.json";
|
||||
|
||||
_fileSystem
|
||||
.Setup(static x => x.Path.Combine(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(testSettingsPath);
|
||||
|
||||
_fileSystem
|
||||
.Setup(static x => x.File.Exists(It.IsAny<string>()))
|
||||
.Returns(true);
|
||||
|
||||
_fileSystem
|
||||
.Setup(static x => x.File.ReadAllTextAsync(It.IsAny<string>(), default))
|
||||
.ReturnsAsync("{}");
|
||||
|
||||
var commandSettings = new AddConfigCommand.Settings(_fileSystem.Object)
|
||||
{
|
||||
Provider = "provider",
|
||||
Key = "key",
|
||||
};
|
||||
|
||||
var result = await _sut.ExecuteAsync(null!, commandSettings);
|
||||
|
||||
result.Should().Be(0);
|
||||
|
||||
_fileSystem
|
||||
.Verify(
|
||||
x => x.File.WriteAllTextAsync(testSettingsPath, It.IsAny<string>(), default),
|
||||
Times.Once
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,14 @@
|
||||
namespace AltGen.Console.Config;
|
||||
|
||||
record AppSettings(ProviderSettings[] Providers)
|
||||
record AppSettings
|
||||
{
|
||||
public ProviderSettings[] Providers { get; init; } = [];
|
||||
|
||||
public AppSettings(ProviderSettings[] providers)
|
||||
{
|
||||
Providers = providers;
|
||||
}
|
||||
|
||||
public AppSettings AddOrUpdateProvider(AddConfigCommand.Settings settings)
|
||||
{
|
||||
var updatedProviders = Providers.Select(p =>
|
||||
|
||||
Reference in New Issue
Block a user