Skip to content

Commit 58c9e47

Browse files
authored
fix: ensure tests never access ~/.km directory (#1104)
## Summary - Tests now create isolated temporary directories instead of using `~/.km` - Enforces proper test isolation as required by `docs/AGENTS.md` - Prevents tests from touching user data ## Changes - **`tests/Main.Tests/Unit/CLI/CliApplicationBuilderTests.cs`**: - Implemented `IDisposable` for proper cleanup - Uses temp directory with unique GUID for each test - Passes `--config <temp_path>` to avoid `~/.km` access - **`tests/Main.Tests/Integration/ExamplesCommandOutputTest.cs`**: - Creates isolated temp directory for config file - Cleans up temp directory in finally block ## Test plan - [x] All 520 tests pass (306 Core + 214 Main) - [x] Zero skipped tests - [x] Code coverage at 83.82% (above 80% threshold) - [x] `build.sh` passes with 0 warnings - [x] `format.sh` passes - [x] `coverage.sh` passes - [x] Tests no longer access `~/.km` directory ## Compliance Per `docs/AGENTS.md`: > "Tests must never read or write files at `~/.km/` - Tests must use temp dirs"
1 parent 962369d commit 58c9e47

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

tests/Main.Tests/Integration/ExamplesCommandOutputTest.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace KernelMemory.Main.Tests.Integration;
55
/// <summary>
66
/// Test that executes 'km examples' and verifies output contains expected sections.
77
/// Uses bash execution to provide proper TTY for Spectre.Console.
8+
/// This test uses an isolated temp directory to avoid accessing ~/.km.
89
/// </summary>
910
public sealed class ExamplesCommandOutputTest
1011
{
@@ -18,15 +19,21 @@ public void KmExamples_ExecutesAndOutputsAllSections()
1819
var kmDll = Path.Combine(solutionRoot, "src/Main/bin/Debug/net10.0/KernelMemory.Main.dll");
1920
var outputFile = Path.Combine(Path.GetTempPath(), $"km-examples-test-{Guid.NewGuid():N}.txt");
2021

22+
// Create isolated temp directory for config to avoid accessing ~/.km
23+
var tempDir = Path.Combine(Path.GetTempPath(), $"km-test-{Guid.NewGuid():N}");
24+
Directory.CreateDirectory(tempDir);
25+
var tempConfigPath = Path.Combine(tempDir, "config.json");
26+
2127
Assert.True(File.Exists(kmDll), $"KernelMemory.Main.dll not found at {kmDll}");
2228

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

@@ -79,10 +86,17 @@ public void KmExamples_ExecutesAndOutputsAllSections()
7986
}
8087
finally
8188
{
89+
// Clean up output file
8290
if (File.Exists(outputFile))
8391
{
8492
File.Delete(outputFile);
8593
}
94+
95+
// Clean up temp directory
96+
if (Directory.Exists(tempDir))
97+
{
98+
Directory.Delete(tempDir, recursive: true);
99+
}
86100
}
87101
}
88102
}

tests/Main.Tests/Unit/CLI/CliApplicationBuilderTests.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,58 @@
44

55
namespace KernelMemory.Main.Tests.Unit.CLI;
66

7-
public sealed class CliApplicationBuilderTests
7+
/// <summary>
8+
/// Tests for CliApplicationBuilder.
9+
/// These tests verify that the CLI application builder correctly creates and configures
10+
/// command applications. Tests use isolated temp directories to avoid accessing ~/.km.
11+
/// </summary>
12+
public sealed class CliApplicationBuilderTests : IDisposable
813
{
14+
private readonly string _tempDir;
15+
private readonly string _tempConfigPath;
16+
17+
public CliApplicationBuilderTests()
18+
{
19+
// Create isolated temp directory for each test to avoid ~/.km access
20+
this._tempDir = Path.Combine(Path.GetTempPath(), $"km-test-{Guid.NewGuid():N}");
21+
Directory.CreateDirectory(this._tempDir);
22+
this._tempConfigPath = Path.Combine(this._tempDir, "config.json");
23+
}
24+
25+
public void Dispose()
26+
{
27+
// Clean up temp directory after test
28+
if (Directory.Exists(this._tempDir))
29+
{
30+
Directory.Delete(this._tempDir, recursive: true);
31+
}
32+
}
33+
934
[Fact]
1035
public void Build_CreatesCommandApp()
1136
{
37+
// Arrange: Use temp config path to avoid accessing ~/.km
1238
var builder = new CliApplicationBuilder();
13-
var app = builder.Build();
39+
var args = new[] { "--config", this._tempConfigPath };
40+
41+
// Act
42+
var app = builder.Build(args);
43+
44+
// Assert
1445
Assert.NotNull(app);
1546
}
1647

1748
[Fact]
1849
public void Configure_SetsApplicationName()
1950
{
51+
// Arrange: Use temp config path to avoid accessing ~/.km
2052
var builder = new CliApplicationBuilder();
21-
var app = builder.Build();
22-
// App is configured with name "km"
53+
var args = new[] { "--config", this._tempConfigPath };
54+
55+
// Act
56+
var app = builder.Build(args);
57+
58+
// Assert: App is configured with name "km"
2359
Assert.NotNull(app);
2460
}
2561
}

0 commit comments

Comments
 (0)