tests: work on writing tests for console functionality

This commit is contained in:
Stevan Freeborn
2025-03-09 12:49:28 -05:00
parent 5647070fd3
commit 69a79e297d
8 changed files with 134 additions and 16 deletions
+11
View File
@@ -0,0 +1,11 @@
namespace AltGen.Console.Common;
static class JsonOptions
{
public static JsonSerializerOptions Default { get; } = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
WriteIndented = true
};
}
@@ -5,12 +5,6 @@ sealed class AddConfigCommand(
IFileSystem fileSystem
) : AsyncCommand<AddConfigCommand.Settings>
{
static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
WriteIndented = true
};
readonly IAnsiConsole _console = console;
readonly IFileSystem _fileSystem = fileSystem;
@@ -43,17 +37,17 @@ sealed class AddConfigCommand(
new(settings.Provider, settings.Key, settings.Default)
]);
var json = JsonSerializer.Serialize(appSettings, JsonOptions);
var json = JsonSerializer.Serialize(appSettings, JsonOptions.Default);
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, json);
_console.MarkupLine($"[bold]{settings.Provider}[/] has been configured.");
return 0;
}
var existingJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
var existingAppSettings = JsonSerializer.Deserialize<AppSettings>(existingJson, JsonOptions)
var existingAppSettings = JsonSerializer.Deserialize<AppSettings>(existingJson, JsonOptions.Default)
?? throw new ConfigException("Failed to deserialize settings.");
var updatedAppSettings = existingAppSettings.AddOrUpdateProvider(settings);
var udpatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions);
var udpatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, udpatedJson);
_console.MarkupLine($"[bold]{settings.Provider}[/] has been configured.");
return 0;
@@ -25,14 +25,13 @@ sealed class RemoveConfigCommand(
if (exists is false)
{
_console.MarkupLine("[bold]No existing settings found.[/]");
return 1;
throw new ConfigException("No existing settings found.");
}
var settingsJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
var appSettings = JsonSerializer.Deserialize<AppSettings>(settingsJson) ?? 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);
var updatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, updatedJson);
_console.MarkupLine($"[bold]{settings.Provider}[/] has been removed.");
return 0;