feat: initial cli implementation

This commit is contained in:
Stevan Freeborn
2026-04-17 15:28:59 -05:00
parent ab57ac5a13
commit ba927c0e18
41 changed files with 1124 additions and 17 deletions
@@ -0,0 +1,36 @@
using System.CommandLine;
using Moq;
using StevanFreeborn.Extensions.Configuration.Secure.Cli.Commands;
using StevanFreeborn.Extensions.Configuration.Secure.Configuration;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cli.Tests.Unit.Commands;
public class DeleteCommandFactoryTests
{
private readonly Mock<ISecureConfig> _mockSecureConfig = new();
private readonly Mock<ITerminal> _mockTerminal = new();
private readonly DeleteCommandFactory _sut;
public DeleteCommandFactoryTests()
{
_sut = new(_mockSecureConfig.Object, _mockTerminal.Object);
}
[Fact]
public void Create_WhenCalled_ItShouldReturnExpectedCommand()
{
var expectedCommand = new Command("delete", "Delete the value for the given key")
{
new Argument<string>("key")
{
Description = "The key whose value you want to remove."
},
};
var result = _sut.Create();
result.Should().BeEquivalentTo(expectedCommand, static o => o.IgnoringCyclicReferences());
}
}
@@ -0,0 +1,52 @@
using System.CommandLine;
using Moq;
using StevanFreeborn.Extensions.Configuration.Secure.Cli.Commands;
using StevanFreeborn.Extensions.Configuration.Secure.Configuration;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cli.Tests.Unit.Commands;
public class DeleteCommandTests
{
private readonly Mock<ISecureConfig> _mockSecureConfig = new();
private readonly Mock<ITerminal> _mockTerminal = new();
private readonly Command _sut;
public DeleteCommandTests()
{
_sut = new DeleteCommandFactory(_mockSecureConfig.Object, _mockTerminal.Object).Create();
}
[Fact]
public async Task Delete_WhenCalledWithoutKey_ItShouldReturnNonZeroExitCode()
{
var result = await _sut.Parse([]).InvokeAsync();
result.Should().NotBe(0);
}
[Fact]
public async Task Delete_WhenCalledWithNonExistentKey_ItShouldReturnNonZeroExitCode()
{
var expectedKey = "key";
_mockSecureConfig.Setup(m => m.DeleteAsync(expectedKey, It.IsAny<CancellationToken>())).ReturnsAsync(false);
var result = await _sut.Parse([expectedKey]).InvokeAsync();
result.Should().NotBe(0);
}
[Fact]
public async Task Delete_WhenCalledWithExistingKey_ItShouldReturnZeroExitCode()
{
var expectedKey = "key";
_mockSecureConfig.Setup(m => m.DeleteAsync(expectedKey, It.IsAny<CancellationToken>())).ReturnsAsync(true);
var result = await _sut.Parse([expectedKey]).InvokeAsync();
result.Should().Be(0);
}
}
@@ -0,0 +1,41 @@
using System.CommandLine;
using Moq;
using StevanFreeborn.Extensions.Configuration.Secure.Cli.Commands;
using StevanFreeborn.Extensions.Configuration.Secure.Configuration;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cli.Tests.Unit.Commands;
public class GetCommandFactoryTests
{
private readonly Mock<ISecureConfig> _mockSecureConfig = new();
private readonly Mock<ITerminal> _mockTerminal = new();
private readonly GetCommandFactory _sut;
public GetCommandFactoryTests()
{
_sut = new(_mockSecureConfig.Object, _mockTerminal.Object);
}
[Fact]
public void Create_WhenCalled_ItShouldReturnExpectedCommand()
{
var expectedCommand = new Command("get", "Retrieves the value for the given key")
{
new Argument<string>("key")
{
Description = "The key whose value you want to retrieve."
},
new Option<bool>("--pretty", "-p")
{
Description = "Indicates whether the value - if JSON - should be pretty printed or not"
},
};
var result = _sut.Create();
result.Should().BeEquivalentTo(expectedCommand, static o => o.IgnoringCyclicReferences());
result.Action.Should().NotBeNull();
}
}
@@ -0,0 +1,96 @@
using System.CommandLine;
using System.Text.Json;
using Moq;
using StevanFreeborn.Extensions.Configuration.Secure.Cli.Commands;
using StevanFreeborn.Extensions.Configuration.Secure.Configuration;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cli.Tests.Unit.Commands;
public class GetCommandTests
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
};
private readonly Mock<ISecureConfig> _mockSecureConfig = new();
private readonly Mock<ITerminal> _mockTerminal = new();
private readonly Command _sut;
public GetCommandTests()
{
_sut = new GetCommandFactory(_mockSecureConfig.Object, _mockTerminal.Object).Create();
}
[Fact]
public async Task Get_WhenCalledWithoutKeyArgument_ItShouldReturnNonZeroExitCode()
{
var result = await _sut.Parse([]).InvokeAsync();
result.Should().NotBe(0);
}
[Fact]
public async Task Get_WhenCalledWithNonExistentKey_ItShouldReturnNonZeroExitCode()
{
var expectedKey = "key";
_mockSecureConfig
.Setup(m => m.GetAsync<string>(expectedKey, It.IsAny<CancellationToken>()))
.ReturnsAsync((string?)null);
var result = await _sut.Parse([expectedKey]).InvokeAsync();
result.Should().NotBe(0);
}
[Fact]
public async Task Get_WhenCalledWithKeyForNonJsonValue_ItShouldReturnZeroExitCode()
{
var expectedKey = "key";
var expectedValue = "value";
_mockSecureConfig
.Setup(m => m.GetAsync<string>(expectedKey, It.IsAny<CancellationToken>()))
.ReturnsAsync(expectedValue);
var result = await _sut.Parse([expectedKey]).InvokeAsync();
result.Should().Be(0);
}
[Fact]
public async Task Get_WhenCalledWithKeyForJsonValue_ItShouldReturnZeroExitCode()
{
var expectedKey = "key";
var expectedValue = JsonSerializer.Serialize(new { ApiKey = "Key" });
_mockSecureConfig
.Setup(m => m.GetAsync<string>(expectedKey, It.IsAny<CancellationToken>()))
.ReturnsAsync(expectedValue);
var result = await _sut.Parse([expectedKey]).InvokeAsync();
result.Should().Be(0);
}
[Fact]
public async Task Get_WhenCalledWithKeyForJsonValueAndPrettyPrint_ItShouldReturnZeroExitCode()
{
var expectedKey = "key";
var testValue = new { ApiKey = "Key" };
var expectedValue = JsonSerializer.Serialize(testValue);
var expectedPrintedValue = JsonSerializer.Serialize(testValue, JsonOptions);
_mockSecureConfig
.Setup(m => m.GetAsync<string>(expectedKey, It.IsAny<CancellationToken>()))
.ReturnsAsync(expectedValue);
var result = await _sut.Parse([expectedKey, "-p"]).InvokeAsync();
result.Should().Be(0);
_mockTerminal.Verify(m => m.WriteLine(expectedPrintedValue), Times.Once());
}
}
@@ -0,0 +1,41 @@
using System.CommandLine;
using Moq;
using StevanFreeborn.Extensions.Configuration.Secure.Cli.Commands;
using StevanFreeborn.Extensions.Configuration.Secure.Configuration;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cli.Tests.Unit.Commands;
public class SetCommandFactoryTests
{
private readonly Mock<ISecureConfig> _mockSecureConfig = new();
private readonly Mock<ITerminal> _mockTerminal = new();
private readonly SetCommandFactory _sut;
public SetCommandFactoryTests()
{
_sut = new(_mockSecureConfig.Object, _mockTerminal.Object);
}
[Fact]
public void Create_WhenCalled_ItShouldReturnExpectedCommand()
{
var expectedCommand = new Command("set", "Sets the value for the given key")
{
new Argument<string>("key")
{
Description = "The key whose value you want to set.",
},
new Argument<string>("value")
{
Description = "The value that you want to set for the key."
},
};
var result = _sut.Create();
result.Should().BeEquivalentTo(expectedCommand, static o => o.IgnoringCyclicReferences());
result.Action.Should().NotBeNull();
}
}
@@ -0,0 +1,63 @@
using System.CommandLine;
using System.Text.Json;
using Moq;
using StevanFreeborn.Extensions.Configuration.Secure.Cli.Commands;
using StevanFreeborn.Extensions.Configuration.Secure.Configuration;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cli.Tests.Unit.Commands;
public class SetCommandTests
{
private readonly Mock<ISecureConfig> _mockSecureConfig = new();
private readonly Mock<ITerminal> _mockTerminal = new();
private readonly Command _sut;
public SetCommandTests()
{
_sut = new SetCommandFactory(_mockSecureConfig.Object, _mockTerminal.Object).Create();
}
[Fact]
public async Task Set_WhenCalledWithoutKey_ItShouldReturnNonZeroExitCode()
{
var result = await _sut.Parse([]).InvokeAsync();
result.Should().NotBe(0);
}
[Fact]
public async Task Set_WhenCalledWithoutValue_ItShouldReturnNonZeroExitCode()
{
var result = await _sut.Parse(["key"]).InvokeAsync();
result.Should().NotBe(0);
}
[Fact]
public async Task Set_WhenCalledWithKeyAndValue_ItShouldReturnZeroExitCode()
{
var expectedKey = "key";
var expectedValue = "value";
var result = await _sut.Parse([expectedKey, expectedValue]).InvokeAsync();
result.Should().Be(0);
_mockSecureConfig.Verify(m => m.SetAsync(expectedKey, expectedValue, It.IsAny<CancellationToken>()), Times.Once());
}
[Fact]
public async Task Set_WhenCalledWithKeyAndJsonValue_ItShouldReturnZeroExitCode()
{
var expectedKey = "key";
var expectedValue = JsonSerializer.Serialize(new { ApiKey = "ApiKey" });
var result = await _sut.Parse([expectedKey, expectedValue]).InvokeAsync();
result.Should().Be(0);
_mockSecureConfig.Verify(m => m.SetAsync(expectedKey, expectedValue, It.IsAny<CancellationToken>()), Times.Once());
}
}
@@ -0,0 +1,36 @@
using StevanFreeborn.Extensions.Configuration.Secure.Cli.Commands;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cli.Tests.Unit.Commands;
public class TerminalTests : IDisposable
{
private readonly StringWriter _stringWriter = new();
private readonly Terminal _sut = new();
public TerminalTests()
{
Console.SetOut(_stringWriter);
}
[Fact]
public void WriteLine_WhenCalled_ItShouldWriteToConsole()
{
var message = "test message";
_sut.WriteLine(message);
_stringWriter.ToString().Should().Be($"test message{Environment.NewLine}");
}
public void Dispose()
{
var standardOutput = new StreamWriter(Console.OpenStandardOutput())
{
AutoFlush = true
};
Console.SetOut(standardOutput);
GC.SuppressFinalize(this);
}
}