tests: work on writing tests for console functionality

This commit is contained in:
Stevan Freeborn
2025-03-10 20:48:19 -05:00
parent 69a79e297d
commit 5c2059cfa9
3 changed files with 80 additions and 6 deletions
@@ -11,6 +11,7 @@
<PackageReference Include="coverlet.collector" Version="6.0.2" /> <PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" /> <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" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" /> <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; namespace AltGen.Console.Tests.Unit;
public class AddConfigCommandTests public class AddConfigCommandTests
{ {
[Fact] readonly Mock<IFileSystem> _fileSystem = new();
public Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldCreateSettings() 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] [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
);
} }
} }
+8 -1
View File
@@ -1,7 +1,14 @@
namespace AltGen.Console.Config; 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) public AppSettings AddOrUpdateProvider(AddConfigCommand.Settings settings)
{ {
var updatedProviders = Providers.Select(p => var updatedProviders = Providers.Select(p =>