feat: add remove, list, and add config commands

This commit is contained in:
Stevan Freeborn
2025-03-10 21:45:46 -05:00
parent 5c2059cfa9
commit e9b9f7fc13
10 changed files with 340 additions and 28 deletions
@@ -1,13 +1,9 @@
using Spectre.Console;
using Spectre.Console.Cli;
using Spectre.Console.Testing;
namespace AltGen.Console.Tests.Unit;
public class AddConfigCommandTests
public class AddConfigCommandTests : IDisposable
{
readonly Mock<IFileSystem> _fileSystem = new();
readonly IAnsiConsole _testConsole = new TestConsole();
readonly TestConsole _testConsole = new();
readonly AddConfigCommand _sut;
public AddConfigCommandTests()
@@ -20,6 +16,11 @@ public class AddConfigCommandTests
public async Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldCreateSettings()
{
var testSettingsPath = "appsettings.json";
var expectedAppSettings = new AppSettings([
new("provider", "key", false)
]);
var expectedJson = JsonSerializer.Serialize(expectedAppSettings, JsonOptions.Default);
_fileSystem
.Setup(static x => x.Path.Combine(It.IsAny<string>(), It.IsAny<string>()))
@@ -41,13 +42,56 @@ public class AddConfigCommandTests
_fileSystem
.Verify(
x => x.File.WriteAllTextAsync(testSettingsPath, It.IsAny<string>(), default),
x => x.File.WriteAllTextAsync(testSettingsPath, expectedJson, default),
Times.Once
);
}
[Fact]
public async Task ExecuteAsync_WhenSettingsExist_ItShouldUpdateSettings()
{
var testSettingsPath = "appsettings.json";
var existingAppSettings = new AppSettings([
new("existing", "existing", false)
]);
var existingJson = JsonSerializer.Serialize(existingAppSettings, JsonOptions.Default);
var expectedAppSettings = new AppSettings([
new("existing", "key", true)
]);
var expectedJson = JsonSerializer.Serialize(expectedAppSettings, JsonOptions.Default);
_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(existingJson);
var commandSettings = new AddConfigCommand.Settings(_fileSystem.Object)
{
Provider = "existing",
Key = "key",
Default = true,
};
var result = await _sut.ExecuteAsync(null!, commandSettings);
result.Should().Be(0);
_fileSystem
.Verify(
x => x.File.WriteAllTextAsync(testSettingsPath, expectedJson, default),
Times.Once
);
}
[Fact]
public async Task ExecuteAsync_WhenSettingsExistButCanNotBeDeserialized_ItShouldThrow()
{
var testSettingsPath = "appsettings.json";
@@ -61,7 +105,7 @@ public class AddConfigCommandTests
_fileSystem
.Setup(static x => x.File.ReadAllTextAsync(It.IsAny<string>(), default))
.ReturnsAsync("{}");
.ReturnsAsync("null");
var commandSettings = new AddConfigCommand.Settings(_fileSystem.Object)
{
@@ -69,14 +113,14 @@ public class AddConfigCommandTests
Key = "key",
};
var result = await _sut.ExecuteAsync(null!, commandSettings);
var act = async () => await _sut.ExecuteAsync(null!, commandSettings);
result.Should().Be(0);
await act.Should().ThrowAsync<ConfigException>();
}
_fileSystem
.Verify(
x => x.File.WriteAllTextAsync(testSettingsPath, It.IsAny<string>(), default),
Times.Once
);
public void Dispose()
{
_testConsole.Dispose();
GC.SuppressFinalize(this);
}
}
@@ -0,0 +1,6 @@
namespace AltGen.Console.Tests.Unit;
public class GenerateCommandTests
{
}
@@ -0,0 +1,126 @@
namespace AltGen.Console.Tests.Unit;
public class ListConfigCommandTests : IDisposable
{
readonly Mock<IFileSystem> _fileSystem = new();
readonly TestConsole _testConsole = new();
readonly ListConfigCommand _sut;
public ListConfigCommandTests()
{
_sut = new ListConfigCommand(_testConsole, _fileSystem.Object);
}
[Fact]
public async Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldOutputNoSettings()
{
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 result = await _sut.ExecuteAsync(null!);
result.Should().Be(0);
_testConsole
.Output
.Should()
.Contain("No settings found.");
}
[Fact]
public async Task ExecuteAsync_WhenSettingsExist_ItShouldOutputSettings()
{
var testSettingsPath = "appsettings.json";
var testSettings = new AppSettings([
new("provider", "key", false)
]);
var testSettingsJson = JsonSerializer.Serialize(testSettings, JsonOptions.Default);
_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(testSettingsJson);
var result = await _sut.ExecuteAsync(null!);
result.Should().Be(0);
_testConsole
.Output
.Should()
.Contain("provider key");
}
[Fact]
public async Task ExecuteAsync_WhenSettingsExistAndDefault_ItShouldOutputSettings()
{
var testSettingsPath = "appsettings.json";
var testSettings = new AppSettings([
new("provider", "key", true)
]);
var testSettingsJson = JsonSerializer.Serialize(testSettings, JsonOptions.Default);
_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(testSettingsJson);
var result = await _sut.ExecuteAsync(null!);
result.Should().Be(0);
_testConsole
.Output
.Should()
.Contain("provider key (default)");
}
[Fact]
public async Task ExecuteAsync_WhenSettingsCanNotBeDeserialized_ItShouldThrowConfigException()
{
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("null");
var action = async () => await _sut.ExecuteAsync(null!);
await action.Should().ThrowAsync<ConfigException>();
}
public void Dispose()
{
_testConsole.Dispose();
GC.SuppressFinalize(this);
}
}
@@ -1,23 +1,118 @@
namespace AltGen.Console.Tests.Unit;
public class RemoveConfigCommandTests
public class RemoveConfigCommandTests : IDisposable
{
readonly Mock<IFileSystem> _fileSystem = new();
readonly TestConsole _testConsole = new();
readonly RemoveConfigCommand _sut;
public RemoveConfigCommandTests()
{
_sut = new RemoveConfigCommand(_fileSystem.Object, _testConsole);
}
[Fact]
public Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldThrow()
public async Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldThrow()
{
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.Path.Exists(It.IsAny<string>()))
.Returns(false);
var commandSettings = new RemoveConfigCommand.Settings(_fileSystem.Object)
{
Provider = "provider",
};
var action = async () => await _sut.ExecuteAsync(null!, commandSettings);
await action.Should().ThrowAsync<ConfigException>();
}
[Fact]
public Task ExecuteAsync_WhenProviderDoesNotExist_ItShouldDoNothing()
public async Task ExecuteAsync_WhenProviderDoesNotExist_ItShouldDoNothing()
{
throw new NotImplementedException();
var testSettingsPath = "appsettings.json";
var testSettings = new AppSettings([]);
var testSettingsJson = JsonSerializer.Serialize(testSettings, JsonOptions.Default);
_fileSystem
.Setup(static x => x.Path.Combine(It.IsAny<string>(), It.IsAny<string>()))
.Returns(testSettingsPath);
_fileSystem
.Setup(static x => x.Path.Exists(It.IsAny<string>()))
.Returns(true);
_fileSystem
.Setup(static x => x.File.ReadAllTextAsync(It.IsAny<string>(), default))
.ReturnsAsync(testSettingsJson);
var commandSettings = new RemoveConfigCommand.Settings(_fileSystem.Object)
{
Provider = "provider",
};
var result = await _sut.ExecuteAsync(null!, commandSettings);
result.Should().Be(0);
_fileSystem
.Verify(
x => x.File.WriteAllTextAsync(testSettingsPath, testSettingsJson, default),
Times.Once
);
}
[Fact]
public Task ExecuteAsync_WhenProviderExists_ItShouldRemoveProviderFromSettings()
public async Task ExecuteAsync_WhenProviderExists_ItShouldRemoveProviderFromSettings()
{
throw new NotImplementedException();
var testSettingsPath = "appsettings.json";
var testSettings = new AppSettings([
new("provider", "key", false)
]);
var testSettingsJson = JsonSerializer.Serialize(testSettings, JsonOptions.Default);
_fileSystem
.Setup(static x => x.Path.Combine(It.IsAny<string>(), It.IsAny<string>()))
.Returns(testSettingsPath);
_fileSystem
.Setup(static x => x.Path.Exists(It.IsAny<string>()))
.Returns(true);
_fileSystem
.Setup(static x => x.File.ReadAllTextAsync(It.IsAny<string>(), default))
.ReturnsAsync(testSettingsJson);
var commandSettings = new RemoveConfigCommand.Settings(_fileSystem.Object)
{
Provider = "provider",
};
var result = await _sut.ExecuteAsync(null!, commandSettings);
result.Should().Be(0);
var expectedSettings = new AppSettings([]);
var expectedSettingsJson = JsonSerializer.Serialize(expectedSettings, JsonOptions.Default);
_fileSystem
.Verify(
x => x.File.WriteAllTextAsync(testSettingsPath, expectedSettingsJson, default),
Times.Once
);
}
public void Dispose()
{
_testConsole.Dispose();
GC.SuppressFinalize(this);
}
}
+5
View File
@@ -1,5 +1,7 @@
global using System.IO.Abstractions;
global using System.Text.Json;
global using AltGen.Console.Common;
global using AltGen.Console.Config;
global using AltGen.Console.Generate;
@@ -8,3 +10,6 @@ global using FluentAssertions;
global using Moq;
global using RichardSzalay.MockHttp;
global using Spectre.Console;
global using Spectre.Console.Testing;
@@ -9,12 +9,11 @@ static class HostBuilderExtensions
app.Configure(static c =>
{
c.PropagateExceptions();
c.AddBranch("config", static c =>
{
c.AddCommand<AddConfigCommand>("add");
c.AddCommand<RemoveConfigCommand>("remove");
c.AddCommand<ListConfigCommand>("list");
});
});
@@ -24,7 +24,7 @@ sealed class AddConfigCommand(
[Description("Set the provider as the default.")]
public bool Default { get; init; }
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, "appsettings.json");
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
}
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
+2
View File
@@ -2,6 +2,8 @@ namespace AltGen.Console.Config;
record AppSettings
{
public const string SettingsFileName = "appsettings.json";
public ProviderSettings[] Providers { get; init; } = [];
public AppSettings(ProviderSettings[] providers)
@@ -0,0 +1,34 @@
namespace AltGen.Console.Config;
sealed class ListConfigCommand(IAnsiConsole console, IFileSystem fileSystem) : AsyncCommand
{
readonly IAnsiConsole _console = console;
readonly IFileSystem _fileSystem = fileSystem;
public override async Task<int> ExecuteAsync(CommandContext context)
{
var settingsPath = _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
var settingsExist = _fileSystem.File.Exists(settingsPath);
if (settingsExist is false)
{
_console.MarkupLine("No settings found.");
return 0;
}
var settingsJson = await _fileSystem.File.ReadAllTextAsync(settingsPath);
var appSettings = JsonSerializer.Deserialize<AppSettings>(settingsJson, JsonOptions.Default)
?? throw new ConfigException("Failed to deserialize settings.");
foreach (var provider in appSettings.Providers)
{
var providerName = provider.Provider;
var providerKey = provider.Key;
var isDefault = provider.Default ? " (default)" : string.Empty;
_console.MarkupLine($"[bold]{providerName}[/] [dim]{providerKey}[/]{isDefault}");
}
return 0;
}
}
@@ -16,7 +16,7 @@ sealed class RemoveConfigCommand(
[Description("The provider to remove.")]
public string Provider { get; init; } = string.Empty;
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, "appsettings.json");
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
}
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
@@ -29,7 +29,8 @@ sealed class RemoveConfigCommand(
}
var settingsJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
var appSettings = JsonSerializer.Deserialize<AppSettings>(settingsJson, JsonOptions.Default) ?? throw new ConfigException("Failed to deserialize app settings.");
var appSettings = JsonSerializer.Deserialize<AppSettings>(settingsJson, JsonOptions.Default)
?? throw new ConfigException("Failed to deserialize app settings.");
var updatedAppSettings = appSettings.RemoveProvider(settings);
var updatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, updatedJson);