Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions tests/Main.Tests/Integration/ExamplesCommandOutputTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace KernelMemory.Main.Tests.Integration;
/// <summary>
/// Test that executes 'km examples' and verifies output contains expected sections.
/// Uses bash execution to provide proper TTY for Spectre.Console.
/// This test uses an isolated temp directory to avoid accessing ~/.km.
/// </summary>
public sealed class ExamplesCommandOutputTest
{
Expand All @@ -18,15 +19,21 @@ public void KmExamples_ExecutesAndOutputsAllSections()
var kmDll = Path.Combine(solutionRoot, "src/Main/bin/Debug/net10.0/KernelMemory.Main.dll");
var outputFile = Path.Combine(Path.GetTempPath(), $"km-examples-test-{Guid.NewGuid():N}.txt");

// Create isolated temp directory for config to avoid accessing ~/.km
var tempDir = Path.Combine(Path.GetTempPath(), $"km-test-{Guid.NewGuid():N}");
Directory.CreateDirectory(tempDir);
var tempConfigPath = Path.Combine(tempDir, "config.json");

Assert.True(File.Exists(kmDll), $"KernelMemory.Main.dll not found at {kmDll}");

try
{
// Act: Execute km examples via bash and capture output
// Act: Execute km examples via bash with isolated config path
// Note: --config must come AFTER the command name (Spectre.Console.Cli requirement)
var process = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "bash",
Arguments = $"-c \"dotnet \\\"{kmDll}\\\" examples > \\\"{outputFile}\\\" 2>&1\"",
Arguments = $"-c \"dotnet \\\"{kmDll}\\\" examples --config \\\"{tempConfigPath}\\\" > \\\"{outputFile}\\\" 2>&1\"",
UseShellExecute = false
});

Expand Down Expand Up @@ -79,10 +86,17 @@ public void KmExamples_ExecutesAndOutputsAllSections()
}
finally
{
// Clean up output file
if (File.Exists(outputFile))
{
File.Delete(outputFile);
}

// Clean up temp directory
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, recursive: true);
}
}
}
}
44 changes: 40 additions & 4 deletions tests/Main.Tests/Unit/CLI/CliApplicationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,58 @@

namespace KernelMemory.Main.Tests.Unit.CLI;

public sealed class CliApplicationBuilderTests
/// <summary>
/// Tests for CliApplicationBuilder.
/// These tests verify that the CLI application builder correctly creates and configures
/// command applications. Tests use isolated temp directories to avoid accessing ~/.km.
/// </summary>
public sealed class CliApplicationBuilderTests : IDisposable
{
private readonly string _tempDir;
private readonly string _tempConfigPath;

public CliApplicationBuilderTests()
{
// Create isolated temp directory for each test to avoid ~/.km access
this._tempDir = Path.Combine(Path.GetTempPath(), $"km-test-{Guid.NewGuid():N}");
Directory.CreateDirectory(this._tempDir);
this._tempConfigPath = Path.Combine(this._tempDir, "config.json");
}

public void Dispose()
{
// Clean up temp directory after test
if (Directory.Exists(this._tempDir))
{
Directory.Delete(this._tempDir, recursive: true);
}
}

[Fact]
public void Build_CreatesCommandApp()
{
// Arrange: Use temp config path to avoid accessing ~/.km
var builder = new CliApplicationBuilder();
var app = builder.Build();
var args = new[] { "--config", this._tempConfigPath };

// Act
var app = builder.Build(args);

// Assert
Assert.NotNull(app);
}

[Fact]
public void Configure_SetsApplicationName()
{
// Arrange: Use temp config path to avoid accessing ~/.km
var builder = new CliApplicationBuilder();
var app = builder.Build();
// App is configured with name "km"
var args = new[] { "--config", this._tempConfigPath };

// Act
var app = builder.Build(args);

// Assert: App is configured with name "km"
Assert.NotNull(app);
}
}
Loading