audit notes work completed, test fixes work (95% done), new sprints, new data sources setup and configuration
This commit is contained in:
158
src/__Tests/interop/StellaOps.Interop.Tests/ToolManagerTests.cs
Normal file
158
src/__Tests/interop/StellaOps.Interop.Tests/ToolManagerTests.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using System.Text;
|
||||
using FluentAssertions;
|
||||
using StellaOps.Interop;
|
||||
using Xunit;
|
||||
using Xunit.Sdk;
|
||||
|
||||
namespace StellaOps.Interop.Tests;
|
||||
|
||||
public sealed class ToolManagerTests
|
||||
{
|
||||
[Fact]
|
||||
public void ResolveToolPath_UsesConfiguredPath()
|
||||
{
|
||||
var workDir = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
var toolPath = Path.Combine(workDir, OperatingSystem.IsWindows() ? "stub-tool.exe" : "stub-tool");
|
||||
File.WriteAllText(toolPath, "stub", Encoding.ASCII);
|
||||
|
||||
var toolPaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["stub-tool"] = toolPath
|
||||
};
|
||||
|
||||
var manager = new ToolManager(workDir, toolPaths);
|
||||
|
||||
manager.ResolveToolPath("stub-tool").Should().Be(toolPath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteDirectory(workDir);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveToolPath_UsesPathProbe()
|
||||
{
|
||||
var workDir = CreateTempDirectory();
|
||||
var toolDir = CreateTempDirectory();
|
||||
var originalPath = Environment.GetEnvironmentVariable("PATH");
|
||||
try
|
||||
{
|
||||
var toolFile = OperatingSystem.IsWindows() ? "probe-tool.exe" : "probe-tool";
|
||||
var toolPath = Path.Combine(toolDir, toolFile);
|
||||
File.WriteAllText(toolPath, "stub", Encoding.ASCII);
|
||||
|
||||
Environment.SetEnvironmentVariable("PATH", toolDir);
|
||||
|
||||
var manager = new ToolManager(workDir);
|
||||
manager.ResolveToolPath("probe-tool").Should().Be(toolPath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.SetEnvironmentVariable("PATH", originalPath);
|
||||
DeleteDirectory(toolDir);
|
||||
DeleteDirectory(workDir);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindOnPath_ReturnsNull_WhenPathMissing()
|
||||
{
|
||||
var originalPath = Environment.GetEnvironmentVariable("PATH");
|
||||
try
|
||||
{
|
||||
Environment.SetEnvironmentVariable("PATH", string.Empty);
|
||||
|
||||
ToolManager.FindOnPath("missing-tool").Should().BeNull();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.SetEnvironmentVariable("PATH", originalPath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RunAsync_ReturnsFailure_WhenToolMissing()
|
||||
{
|
||||
var workDir = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
var manager = new ToolManager(workDir);
|
||||
|
||||
var result = await manager.RunAsync("missing-tool", "--version", CancellationToken.None);
|
||||
|
||||
result.Success.Should().BeFalse();
|
||||
result.Error.Should().Contain("Tool not found");
|
||||
result.ExitCode.Should().Be(-1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteDirectory(workDir);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RunAsync_ReturnsSuccess_WhenShellExecutesScript()
|
||||
{
|
||||
var workDir = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
var scriptPath = WriteShellScript(workDir);
|
||||
var shellPath = ResolveShellPath();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(shellPath) || !File.Exists(shellPath))
|
||||
throw SkipException.ForSkip("Shell not available for interop tool test.");
|
||||
|
||||
var toolPaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["shell"] = shellPath
|
||||
};
|
||||
|
||||
var args = OperatingSystem.IsWindows()
|
||||
? $"/c \"{scriptPath}\""
|
||||
: $"\"{scriptPath}\"";
|
||||
|
||||
var manager = new ToolManager(workDir, toolPaths);
|
||||
var result = await manager.RunAsync("shell", args, CancellationToken.None);
|
||||
|
||||
result.Success.Should().BeTrue();
|
||||
result.StdOut.Should().Contain("ok");
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteDirectory(workDir);
|
||||
}
|
||||
}
|
||||
|
||||
private static string ResolveShellPath()
|
||||
=> OperatingSystem.IsWindows()
|
||||
? Environment.GetEnvironmentVariable("ComSpec") ?? string.Empty
|
||||
: "/bin/sh";
|
||||
|
||||
private static string WriteShellScript(string directory)
|
||||
{
|
||||
var scriptName = OperatingSystem.IsWindows() ? "interop-tool.cmd" : "interop-tool.sh";
|
||||
var scriptPath = Path.Combine(directory, scriptName);
|
||||
var content = OperatingSystem.IsWindows()
|
||||
? "@echo off\r\necho ok\r\nexit /b 0\r\n"
|
||||
: "#!/bin/sh\n\necho ok\nexit 0\n";
|
||||
|
||||
File.WriteAllText(scriptPath, content, Encoding.ASCII);
|
||||
return scriptPath;
|
||||
}
|
||||
|
||||
private static string CreateTempDirectory()
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"interop-tool-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(path);
|
||||
return path;
|
||||
}
|
||||
|
||||
private static void DeleteDirectory(string path)
|
||||
{
|
||||
if (Directory.Exists(path))
|
||||
Directory.Delete(path, recursive: true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user