3bfa289c2a
The agent resolves the requested shell: PowerShell 7 (Program Files or PATH), Windows PowerShell, cmd.exe (%ComSpec%) or its own built-in PowerShell console; automatic takes the first of those that exists. A missing explicit choice fails the open with a clear message. The built-in shell is the hidden `shell-host` verb: the SDK console host (ConsoleShell.Start) on the embedded engine with the bundled modules on PSModulePath, so a shell works in Windows PE without WinPE-PowerShell. It starts no agent workers and touches no config or credentials. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
4.1 KiB
C#
114 lines
4.1 KiB
C#
using Agent.Shell;
|
|
using Shared.Contracts.Models;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
public class WindowsShellResolverTests
|
|
{
|
|
private const string Pwsh = @"C:\Program Files\PowerShell\7\pwsh.exe";
|
|
private const string PathPwsh = @"D:\Tools\pwsh\pwsh.exe";
|
|
private const string WindowsPowerShell = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
|
|
private const string ComSpec = @"C:\Windows\System32\cmd.exe";
|
|
private const string Agent = @"C:\Program Files\ExecuteR\Agent\ExecuteR-Agent.exe";
|
|
|
|
private static readonly WindowsShellLocations Locations = new(
|
|
ProgramFiles: @"C:\Program Files",
|
|
SystemDirectory: @"C:\Windows\System32",
|
|
ComSpec: ComSpec,
|
|
SearchPath: @"C:\Windows\System32;""D:\Tools\pwsh"";",
|
|
AgentExecutable: Agent);
|
|
|
|
private static ShellLaunch Resolve(string? requested, params string[] existing) =>
|
|
WindowsShellResolver.Resolve(requested, Locations, path => existing.Contains(path, StringComparer.OrdinalIgnoreCase));
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
[InlineData("auto")]
|
|
[InlineData("something-newer")]
|
|
public void AutomaticPrefersPowerShell7(string? requested)
|
|
{
|
|
var launch = Resolve(requested, Pwsh, WindowsPowerShell, Agent, ComSpec);
|
|
|
|
Assert.Equal(ShellKinds.Pwsh, launch.Kind);
|
|
Assert.Equal(Pwsh, launch.FileName);
|
|
Assert.Equal(["-NoLogo"], launch.Args);
|
|
}
|
|
|
|
[Fact]
|
|
public void AutomaticFallsBackInOrder()
|
|
{
|
|
Assert.Equal(ShellKinds.WindowsPowerShell, Resolve(null, WindowsPowerShell, Agent, ComSpec).Kind);
|
|
Assert.Equal(ShellKinds.BuiltIn, Resolve(null, Agent, ComSpec).Kind);
|
|
Assert.Equal(ShellKinds.Cmd, Resolve(null, ComSpec).Kind);
|
|
}
|
|
|
|
[Fact]
|
|
public void AutomaticWithNothingFails()
|
|
{
|
|
Assert.Throws<ShellUnavailableException>(() => Resolve(null));
|
|
}
|
|
|
|
[Fact]
|
|
public void PwshIsFoundOnPath()
|
|
{
|
|
var launch = Resolve("pwsh", PathPwsh, WindowsPowerShell);
|
|
|
|
Assert.Equal(PathPwsh, launch.FileName);
|
|
}
|
|
|
|
[Fact]
|
|
public void ExplicitChoiceIsHonoredOverAutomaticOrder()
|
|
{
|
|
string[] all = [Pwsh, WindowsPowerShell, Agent, ComSpec];
|
|
|
|
Assert.Equal(WindowsPowerShell, Resolve("powershell", all).FileName);
|
|
Assert.Equal(ComSpec, Resolve("CMD", all).FileName);
|
|
|
|
var builtIn = Resolve("builtin", all);
|
|
Assert.Equal(Agent, builtIn.FileName);
|
|
Assert.Equal([WindowsShellResolver.HostVerb], builtIn.Args);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("pwsh", "PowerShell 7 (pwsh.exe) is not installed on this device.")]
|
|
[InlineData("powershell", "Windows PowerShell (powershell.exe) is not installed on this device.")]
|
|
[InlineData("cmd", "Command Prompt (cmd.exe) was not found on this device.")]
|
|
public void MissingExplicitChoiceFailsWithoutFallingBack(string requested, string message)
|
|
{
|
|
// Every other shell exists; the explicit choice must not silently fall back.
|
|
var others = new[] { Pwsh, WindowsPowerShell, ComSpec }.Where(path => !path.Contains(requested + ".exe", StringComparison.OrdinalIgnoreCase));
|
|
|
|
var ex = Assert.Throws<ShellUnavailableException>(() => Resolve(requested, [.. others, Agent]));
|
|
Assert.Equal(message, ex.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void CmdFallsBackToSystemDirectoryWithoutComSpec()
|
|
{
|
|
var launch = WindowsShellResolver.Resolve("cmd", Locations with { ComSpec = null }, path => path == ComSpec);
|
|
|
|
Assert.Equal(ComSpec, launch.FileName);
|
|
Assert.Empty(launch.Args);
|
|
}
|
|
|
|
[Fact]
|
|
public void BuiltInIsUnavailableUnderDotnetHost()
|
|
{
|
|
var locations = Locations with { AgentExecutable = @"C:\Program Files\dotnet\dotnet.exe" };
|
|
|
|
Assert.Throws<ShellUnavailableException>(
|
|
() => WindowsShellResolver.Resolve("builtin", locations, _ => true));
|
|
}
|
|
|
|
[Fact]
|
|
public void KindsNormalize()
|
|
{
|
|
Assert.Equal(ShellKinds.Auto, ShellKinds.Normalize(null));
|
|
Assert.Equal(ShellKinds.Auto, ShellKinds.Normalize(" "));
|
|
Assert.Equal(ShellKinds.BuiltIn, ShellKinds.Normalize(" BuiltIn "));
|
|
Assert.Null(ShellKinds.Normalize("bash"));
|
|
}
|
|
}
|