diff --git a/tests/Main.Tests/Integration/ExamplesCommandOutputTest.cs b/tests/Main.Tests/Integration/ExamplesCommandOutputTest.cs index 870bb7aac..74cee3963 100644 --- a/tests/Main.Tests/Integration/ExamplesCommandOutputTest.cs +++ b/tests/Main.Tests/Integration/ExamplesCommandOutputTest.cs @@ -5,6 +5,7 @@ namespace KernelMemory.Main.Tests.Integration; /// /// 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. /// public sealed class ExamplesCommandOutputTest { @@ -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 }); @@ -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); + } } } } diff --git a/tests/Main.Tests/Unit/CLI/CliApplicationBuilderTests.cs b/tests/Main.Tests/Unit/CLI/CliApplicationBuilderTests.cs index bb05465c6..8303eac86 100644 --- a/tests/Main.Tests/Unit/CLI/CliApplicationBuilderTests.cs +++ b/tests/Main.Tests/Unit/CLI/CliApplicationBuilderTests.cs @@ -4,22 +4,58 @@ namespace KernelMemory.Main.Tests.Unit.CLI; -public sealed class CliApplicationBuilderTests +/// +/// 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. +/// +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); } }