b35c956865
Behaviour change: the in-process PowerShell engine now reports what the script asked for, the way pwsh -File does. - `exit 5` reports exit 5 and Failed. It used to report Succeeded, 0: an AddScript exit reaches only the host's SetShouldExit, which nothing read, so a small host now captures it. `exit 0` succeeds, even after errors. - A native command's non-zero $LASTEXITCODE no longer fails a script unless the script exits with it, and neither do errors it silenced (HadErrors is set by both, so the error stream decides now). - Write-Error still fails a script that never calls exit, with exit 1. JobStatePayload gains optional ErrorType, ErrorId and ErrorMessage: the first error's exception type, FullyQualifiedErrorId and message; for external engines the first stderr line. Older agents omit them. It also gains RunId, for the backend to set when relaying to browsers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
413 lines
16 KiB
C#
413 lines
16 KiB
C#
using Agent.PowerShell;
|
|
using Shared.Contracts.Models;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
public class PowerShellExecutionTests
|
|
{
|
|
private static async Task<(List<NormalizedOutputRecord> Records, List<NormalizedProgressRecord> Progress, PowerShellExecutionResult Result)>
|
|
RunAsync(string script, CancellationToken cancellationToken = default)
|
|
{
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var progress = new List<NormalizedProgressRecord>();
|
|
var service = new PowerShellExecutionService();
|
|
var result = await service.ExecuteAsync(
|
|
script, parameters: null,
|
|
onRecord: r => { lock (records) records.Add(r); },
|
|
onProgress: p => { lock (progress) progress.Add(p); },
|
|
cancellationToken);
|
|
return (records, progress, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task OutputStreamIsCapturedAndNormalized()
|
|
{
|
|
var (records, _, result) = await RunAsync("Write-Output 'hello world'");
|
|
|
|
Assert.False(result.HadErrors);
|
|
Assert.Contains(records, r => r.Stream == OutputStreamKind.Output && r.Text.Contains("hello world"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PipelineObjectsArriveAsTheirDisplayedFormNotTypeNames()
|
|
{
|
|
var (records, _, result) = await RunAsync("""
|
|
@{ Region = 'eu-west'; Count = 3 }
|
|
[pscustomobject]@{ Name = 'widget'; Size = 42 }
|
|
[System.Version]'1.2.3.4'
|
|
""");
|
|
|
|
Assert.False(result.HadErrors);
|
|
var text = string.Join('\n', records.Where(r => r.Stream == OutputStreamKind.Output).Select(r => r.Text));
|
|
Assert.DoesNotContain("System.Collections.Hashtable", text);
|
|
Assert.DoesNotContain("System.Management.Automation.PSCustomObject", text);
|
|
Assert.DoesNotContain("@{", text);
|
|
// As the console shows them: the hashtable's keys and values, then
|
|
// (mixed types switch to list view) each property on its own line.
|
|
Assert.Contains(records, r => r.Text.StartsWith("Region") && r.Text.Contains("eu-west"));
|
|
Assert.Contains(records, r => r.Text.StartsWith("Name") && r.Text.Contains("widget"));
|
|
Assert.Contains(records, r => r.Text.StartsWith("Major") && r.Text.Contains('1'));
|
|
Assert.All(records, r => Assert.Equal(r.Text.TrimEnd(), r.Text));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ObjectsOfOneTypeFormatAsATable()
|
|
{
|
|
var (records, _, _) = await RunAsync("""
|
|
[pscustomobject]@{ Name = 'widget'; Size = 42 }
|
|
[pscustomobject]@{ Name = 'gadget'; Size = 7 }
|
|
""");
|
|
|
|
Assert.Contains(records, r => r.Text.StartsWith("Name") && r.Text.Contains("Size"));
|
|
Assert.Contains(records, r => r.Text.StartsWith("widget") && r.Text.EndsWith("42"));
|
|
Assert.Contains(records, r => r.Text.StartsWith("gadget") && r.Text.EndsWith("7"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WideTablesAreNotTruncated()
|
|
{
|
|
var long1 = new string('x', 300);
|
|
var (records, _, _) = await RunAsync($"[pscustomobject]@{{ Value = '{long1}'; Other = 'end' }} | Format-Table");
|
|
|
|
Assert.Contains(records, r => r.Text.Contains(long1) && r.Text.Contains("end"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StringsPassThroughUnchanged()
|
|
{
|
|
var (records, _, _) = await RunAsync("'first'; \"second`nthird\"");
|
|
|
|
var lines = records.Where(r => r.Stream == OutputStreamKind.Output).Select(r => r.Text).ToList();
|
|
Assert.Equal(["first", "second\nthird"], lines);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CoreModulesResolveHeadless()
|
|
{
|
|
// Exercises Microsoft.PowerShell.Utility (Select-Object) and .Management
|
|
// (Get-Process) — the modules the agent intentionally retains.
|
|
var (records, _, result) = await RunAsync(
|
|
"Get-Process | Select-Object -First 1 | ForEach-Object { $_.ProcessName }");
|
|
|
|
Assert.False(result.HadErrors);
|
|
Assert.Contains(records, r => r.Stream == OutputStreamKind.Output && !string.IsNullOrWhiteSpace(r.Text));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ProgressRecordsAreNormalized()
|
|
{
|
|
var (_, progress, _) = await RunAsync(
|
|
"Write-Progress -Activity 'Test' -Status 'Half' -PercentComplete 50");
|
|
|
|
Assert.Contains(progress, p => p.Activity == "Test" && p.PercentComplete == 50 && p.StatusDescription == "Half");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ErrorStreamIsCaptured()
|
|
{
|
|
var (records, _, result) = await RunAsync("Write-Error 'boom'");
|
|
|
|
Assert.True(result.HadErrors);
|
|
Assert.Contains(records, r => r.Stream == OutputStreamKind.Error && r.Text.Contains("boom"));
|
|
}
|
|
|
|
// What the job reports, for each way a script can end. Everything but
|
|
// Write-Error is what pwsh -File exits with (checked against pwsh 7).
|
|
[Theory]
|
|
[InlineData("exit 5", JobState.Failed, 5)]
|
|
[InlineData("exit 0", JobState.Succeeded, 0)]
|
|
[InlineData("exit", JobState.Succeeded, 0)]
|
|
[InlineData("'before'; exit 5; 'after'", JobState.Failed, 5)]
|
|
[InlineData("& { exit 8 }; 'after'", JobState.Failed, 8)]
|
|
[InlineData("function Stop-Now { exit 9 }; Stop-Now", JobState.Failed, 9)]
|
|
[InlineData("'x' | ForEach-Object { exit 11 }", JobState.Failed, 11)]
|
|
[InlineData("$host.SetShouldExit(7); 'after'", JobState.Failed, 7)]
|
|
[InlineData("throw 'boom'", JobState.Failed, 1)]
|
|
[InlineData("Write-Error 'boom'", JobState.Failed, 1)]
|
|
[InlineData("Write-Error 'boom'; exit 0", JobState.Succeeded, 0)]
|
|
[InlineData("Write-Error 'boom'; exit 3", JobState.Failed, 3)]
|
|
[InlineData("try { throw 'boom' } catch { 'handled' }", JobState.Succeeded, 0)]
|
|
[InlineData("Get-Item 'Z:\\missing.txt' -ErrorAction SilentlyContinue; 'x'", JobState.Succeeded, 0)]
|
|
[InlineData("'all good'", JobState.Succeeded, 0)]
|
|
public async Task ScriptsReportTheirOwnExitCode(string script, JobState state, int exitCode)
|
|
{
|
|
var (_, _, result) = await RunAsync(script);
|
|
|
|
var outcome = ScriptOutcome.From(result);
|
|
|
|
Assert.Equal((state, exitCode), (outcome.State, outcome.ExitCode));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("cmd /c exit 3", JobState.Succeeded, 0)]
|
|
[InlineData("cmd /c exit 3; 'after'", JobState.Succeeded, 0)]
|
|
[InlineData("cmd /c exit 3; exit", JobState.Succeeded, 0)]
|
|
[InlineData("cmd /c exit 3; exit $LASTEXITCODE", JobState.Failed, 3)]
|
|
public async Task ANativeCommandsExitCodeCountsOnlyWhenTheScriptExitsWithIt(string script, JobState state, int exitCode)
|
|
{
|
|
Assert.SkipUnless(OperatingSystem.IsWindows(), "cmd.exe is Windows-only");
|
|
|
|
var (_, _, result) = await RunAsync(script);
|
|
|
|
var outcome = ScriptOutcome.From(result);
|
|
|
|
Assert.Equal((state, exitCode), (outcome.State, outcome.ExitCode));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ANonZeroExitWithoutErrorsStillSaysWhatHappened()
|
|
{
|
|
var (_, _, result) = await RunAsync("exit 5");
|
|
|
|
Assert.Equal("The script exited with 5.", ScriptOutcome.From(result).Message);
|
|
Assert.Null(result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ATerminatingErrorNamesItsExceptionAndErrorId()
|
|
{
|
|
var (_, _, result) = await RunAsync("throw [System.IO.FileNotFoundException]::new('nope')");
|
|
|
|
Assert.Equal("System.IO.FileNotFoundException", result.Error?.Type);
|
|
Assert.Equal("nope", result.Error?.Message);
|
|
Assert.Equal("nope", ScriptOutcome.From(result).Message);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WriteErrorKeepsItsNullSummaryAndNamesTheError()
|
|
{
|
|
var (_, _, result) = await RunAsync("Write-Error 'boom'");
|
|
|
|
var outcome = ScriptOutcome.From(result);
|
|
|
|
Assert.Null(outcome.Message);
|
|
Assert.Equal("Microsoft.PowerShell.Commands.WriteErrorException", result.Error?.Type);
|
|
Assert.Equal("Microsoft.PowerShell.Commands.WriteErrorException", result.Error?.Id);
|
|
Assert.Equal("boom", result.Error?.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheFirstErrorIsTheOneReported()
|
|
{
|
|
var (_, _, result) = await RunAsync("""
|
|
Get-Item 'Z:\missing.txt'
|
|
throw 'later'
|
|
""");
|
|
|
|
Assert.Equal("System.Management.Automation.DriveNotFoundException", result.Error?.Type);
|
|
Assert.Contains("DriveNotFound", result.Error?.Id);
|
|
Assert.Equal("later", result.FailureMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnOverlongErrorMessageIsCut()
|
|
{
|
|
var (_, _, result) = await RunAsync($"Write-Error '{new string('x', ScriptErrorInfo.MaxTextLength + 500)}'");
|
|
|
|
Assert.Equal(ScriptErrorInfo.MaxTextLength, result.Error?.Message?.Length);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheHostStaysTheEnginesHeadlessDefault()
|
|
{
|
|
var (records, _, result) = await RunAsync("Write-Host 'hi'; $Host.Name");
|
|
|
|
Assert.False(result.HadErrors);
|
|
Assert.Contains(records, r => r.Stream == OutputStreamKind.Information && r.Text == "hi");
|
|
Assert.Contains(records, r => r.Stream == OutputStreamKind.Output && r.Text == "Default Host");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WarningAndVerboseStreamsAreClassified()
|
|
{
|
|
var (records, _, _) = await RunAsync(
|
|
"$WarningPreference='Continue'; $VerbosePreference='Continue'; Write-Warning 'w'; Write-Verbose 'v'");
|
|
|
|
Assert.Contains(records, r => r.Stream == OutputStreamKind.Warning && r.Text.Contains('w'));
|
|
Assert.Contains(records, r => r.Stream == OutputStreamKind.Verbose && r.Text.Contains('v'));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CancellationStopsExecution()
|
|
{
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(400));
|
|
var (_, _, result) = await RunAsync("Start-Sleep -Seconds 30", cts.Token);
|
|
|
|
Assert.True(result.WasCancelled);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AutoExpansionResolvesBothSyntaxesWithTheDeviceEnvironment()
|
|
{
|
|
Environment.SetEnvironmentVariable("EXECUTER_TEST_VALUE", "expanded-on-device");
|
|
try
|
|
{
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var service = new PowerShellExecutionService();
|
|
|
|
// %VAR%, $env:VAR and $(…) all resolve in one default pass.
|
|
var result = await service.ExecuteAsync(
|
|
"param([string]$Path) Write-Output $Path",
|
|
new Dictionary<string, object?>
|
|
{
|
|
["Path"] = "%EXECUTER_TEST_VALUE%/$env:EXECUTER_TEST_VALUE/$(1 + 1)",
|
|
},
|
|
prelude: null,
|
|
r => { lock (records) records.Add(r); },
|
|
_ => { },
|
|
CancellationToken.None,
|
|
expansions: new Dictionary<string, ValueExpansion> { ["Path"] = ValueExpansion.Auto });
|
|
|
|
Assert.False(result.HadErrors);
|
|
Assert.Contains(records, r => r.Text == "expanded-on-device/expanded-on-device/2");
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable("EXECUTER_TEST_VALUE", null);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LiteralOptOutPassesTheRawValueThrough()
|
|
{
|
|
Environment.SetEnvironmentVariable("EXECUTER_TEST_LIT", "should-not-appear");
|
|
try
|
|
{
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var service = new PowerShellExecutionService();
|
|
|
|
var result = await service.ExecuteAsync(
|
|
"param([string]$Value) Write-Output $Value",
|
|
new Dictionary<string, object?> { ["Value"] = "%EXECUTER_TEST_LIT%-$env:EXECUTER_TEST_LIT" },
|
|
prelude: null,
|
|
r => { lock (records) records.Add(r); },
|
|
_ => { },
|
|
CancellationToken.None,
|
|
expansions: new Dictionary<string, ValueExpansion> { ["Value"] = ValueExpansion.Literal });
|
|
|
|
Assert.False(result.HadErrors);
|
|
Assert.Contains(records, r => r.Text == "%EXECUTER_TEST_LIT%-$env:EXECUTER_TEST_LIT");
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable("EXECUTER_TEST_LIT", null);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExpansionAppliesToArrayElementsAndHashtableValuesOnly()
|
|
{
|
|
Environment.SetEnvironmentVariable("EXECUTER_TEST_ITEM", "item");
|
|
try
|
|
{
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var service = new PowerShellExecutionService();
|
|
var parameters = new Dictionary<string, object?>
|
|
{
|
|
["Items"] = new object?[] { "%EXECUTER_TEST_ITEM%-1", "%EXECUTER_TEST_ITEM%-2" },
|
|
["Map"] = new System.Collections.Hashtable
|
|
{
|
|
["%LITERAL_KEY%"] = "%EXECUTER_TEST_ITEM%-v",
|
|
},
|
|
};
|
|
var expansions = new Dictionary<string, ValueExpansion>
|
|
{
|
|
["Items"] = ValueExpansion.Auto,
|
|
["Map"] = ValueExpansion.Auto,
|
|
};
|
|
|
|
var result = await service.ExecuteAsync(
|
|
"param($Items,$Map) Write-Output \"$($Items -join ',')|$($Map.Keys -join ',')=$($Map['%LITERAL_KEY%'])\"",
|
|
parameters, prelude: null,
|
|
r => { lock (records) records.Add(r); },
|
|
_ => { },
|
|
CancellationToken.None, expansions);
|
|
|
|
Assert.False(result.HadErrors);
|
|
// Elements and values expand; the hashtable key stays literal.
|
|
Assert.Contains(records, r => r.Text == "item-1,item-2|%LITERAL_KEY%=item-v");
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable("EXECUTER_TEST_ITEM", null);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SecretsDictionaryIsCaseInsensitiveAndOrdered()
|
|
{
|
|
// Mirrors PowerShellJobHandler's construction of the $Secrets variable.
|
|
var secrets = new System.Collections.Specialized.OrderedDictionary(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
["ApiKey"] = "k-123",
|
|
["DbPassword"] = "p-456",
|
|
};
|
|
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var service = new PowerShellExecutionService();
|
|
|
|
var result = await service.ExecuteAsync(
|
|
// Case-insensitive lookup + declaration order preserved.
|
|
"Write-Output \"$($Secrets.apikey)|$($Secrets.DBPASSWORD)|$($Secrets.Keys -join ',')\"",
|
|
parameters: null,
|
|
prelude: null,
|
|
r => { lock (records) records.Add(r); },
|
|
_ => { },
|
|
CancellationToken.None,
|
|
sessionVariables: new Dictionary<string, object?> { ["Secrets"] = secrets });
|
|
|
|
Assert.False(result.HadErrors);
|
|
Assert.Contains(records, r => r.Text == "k-123|p-456|ApiKey,DbPassword");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SecretsVariableNameIsConfigurable()
|
|
{
|
|
var secrets = new System.Collections.Specialized.OrderedDictionary(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
["Token"] = "t-789",
|
|
};
|
|
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var service = new PowerShellExecutionService();
|
|
|
|
var result = await service.ExecuteAsync(
|
|
"Write-Output $Vault.Token",
|
|
parameters: null,
|
|
prelude: null,
|
|
r => { lock (records) records.Add(r); },
|
|
_ => { },
|
|
CancellationToken.None,
|
|
sessionVariables: new Dictionary<string, object?> { ["Vault"] = secrets });
|
|
|
|
Assert.False(result.HadErrors);
|
|
Assert.Contains(records, r => r.Text == "t-789");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TypedParametersBindToScript()
|
|
{
|
|
// Named typed parameters, including a hashtable — proves the binding
|
|
// path used by variable injection.
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var service = new PowerShellExecutionService();
|
|
var parameters = new Dictionary<string, object?>
|
|
{
|
|
["Count"] = 3,
|
|
["Label"] = "widget",
|
|
["Map"] = new Dictionary<string, object?> { ["a"] = 1, ["b"] = true },
|
|
};
|
|
|
|
var result = await service.ExecuteAsync(
|
|
"param([int]$Count,[string]$Label,[hashtable]$Map) Write-Output \"$Label x$Count map=$($Map.a),$($Map.b)\"",
|
|
parameters,
|
|
r => { lock (records) records.Add(r); },
|
|
_ => { },
|
|
CancellationToken.None);
|
|
|
|
Assert.False(result.HadErrors);
|
|
Assert.Contains(records, r => r.Text.Contains("widget x3 map=1,True"));
|
|
}
|
|
}
|