3c6897d0be
Output objects were sent as PSObject.ToString(), so anything that was not
already a string came back as a type name or a terse dump:
"System.Collections.Hashtable", "System.Diagnostics.Process (svchost)",
"@{Name=widget; Size=42}".
Both PowerShell engines now pipe the script's output through
`Out-String -Stream -Width 4096`, PowerShell's own formatter: objects
arrive as their table or list view (a hashtable's Name/Value rows,
properties as columns), still streamed line by line as they are produced,
wide enough that tables are not cut short with "…" at 80/120 columns.
Strings pass through unchanged; trailing padding is trimmed; the exit
code is unaffected. Bash output was already text.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
5.3 KiB
C#
136 lines
5.3 KiB
C#
using System.Collections;
|
|
using Agent.PowerShell;
|
|
using Shared.Contracts.Messages;
|
|
using Shared.Contracts.Models;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
public class ExternalScriptRunnerTests
|
|
{
|
|
[Fact]
|
|
public void PsLiteralRendersTypedValues()
|
|
{
|
|
Assert.Equal("'it''s'", ExternalScriptRunner.PsLiteral("it's"));
|
|
Assert.Equal("42", ExternalScriptRunner.PsLiteral(42));
|
|
Assert.Equal("$true", ExternalScriptRunner.PsLiteral(true));
|
|
Assert.Equal("$null", ExternalScriptRunner.PsLiteral(null));
|
|
Assert.Equal("@('a', 2)", ExternalScriptRunner.PsLiteral(new object?[] { "a", 2 }));
|
|
|
|
var table = new Hashtable { ["Name"] = "x", [42] = true };
|
|
var rendered = ExternalScriptRunner.PsLiteral(table);
|
|
Assert.StartsWith("@{", rendered);
|
|
Assert.Contains("'Name' = 'x'", rendered);
|
|
Assert.Contains("42 = $true", rendered);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WindowsPowerShell51RunsWithTypedParametersAndSecrets()
|
|
{
|
|
Assert.SkipUnless(OperatingSystem.IsWindows(), "powershell.exe is Windows-only");
|
|
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var result = await ExternalScriptRunner.RunAsync(
|
|
ScriptEngine.WindowsPowerShell,
|
|
"""
|
|
param([string]$Label, [int]$Count, [hashtable]$Map)
|
|
Write-Output "v=$($PSVersionTable.PSVersion.Major) $Label x$Count map=$($Map.a) secret=$($Secrets.ApiKey)"
|
|
""",
|
|
prelude: null,
|
|
parameters: new Dictionary<string, object?>
|
|
{
|
|
["Label"] = "widget",
|
|
["Count"] = 3,
|
|
["Map"] = new Hashtable { ["a"] = 7 },
|
|
},
|
|
secrets: [new ScriptSecret("ApiKey", "k-123")],
|
|
secretsVariableName: "Secrets",
|
|
onRecord: r => { lock (records) records.Add(r); },
|
|
CancellationToken.None);
|
|
|
|
Assert.False(result.WasCancelled);
|
|
Assert.Equal(0, result.ExitCode);
|
|
// 5.1 host, typed params bound via splat, case-insensitive secrets.
|
|
Assert.Contains(records, r => r.Text == "v=5 widget x3 map=7 secret=k-123");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WindowsPowerShell51FormatsObjectsAndKeepsTheExitCode()
|
|
{
|
|
Assert.SkipUnless(OperatingSystem.IsWindows(), "powershell.exe is Windows-only");
|
|
|
|
var wide = new string('y', 200);
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var result = await ExternalScriptRunner.RunAsync(
|
|
ScriptEngine.WindowsPowerShell,
|
|
$"@{{ Region = 'eu-west' }}; [pscustomobject]@{{ Value = '{wide}'; Other = 'end' }} | Format-Table; exit 3",
|
|
prelude: null,
|
|
parameters: new Dictionary<string, object?>(),
|
|
secrets: null,
|
|
secretsVariableName: "Secrets",
|
|
onRecord: r => { lock (records) records.Add(r); },
|
|
CancellationToken.None);
|
|
|
|
Assert.Equal(3, result.ExitCode);
|
|
Assert.DoesNotContain(records, r => r.Text.Contains("System.Collections.Hashtable"));
|
|
Assert.Contains(records, r => r.Text.StartsWith("Region") && r.Text.Contains("eu-west"));
|
|
// Not cut at the 120-column console width.
|
|
Assert.Contains(records, r => r.Text.Contains(wide) && r.Text.Contains("end"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task BashRunsWhenAvailable()
|
|
{
|
|
Assert.SkipUnless(
|
|
ExternalScriptRunner.IsAvailable(ScriptEngine.Bash),
|
|
"bash is not available on this machine");
|
|
|
|
var records = new List<NormalizedOutputRecord>();
|
|
var result = await ExternalScriptRunner.RunAsync(
|
|
ScriptEngine.Bash,
|
|
"echo \"label=$Label secret=$ApiKey\"",
|
|
prelude: "# prelude comment",
|
|
parameters: new Dictionary<string, object?> { ["Label"] = "widget" },
|
|
secrets: [new ScriptSecret("ApiKey", "k-456")],
|
|
secretsVariableName: "Secrets",
|
|
onRecord: r => { lock (records) records.Add(r); },
|
|
CancellationToken.None);
|
|
|
|
Assert.False(result.WasCancelled);
|
|
Assert.Equal(0, result.ExitCode);
|
|
Assert.Contains(records, r => r.Text == "label=widget secret=k-456");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task BashSeesTheScriptsExactNameOverAnInheritedDifferentCase()
|
|
{
|
|
Assert.SkipUnless(
|
|
ExternalScriptRunner.IsAvailable(ScriptEngine.Bash),
|
|
"bash is not available on this machine");
|
|
|
|
// On Windows the inherited "EXECUTERCASEPROBE" and the script's
|
|
// "ExecuterCaseProbe" are one variable; bash must still see the
|
|
// script's spelling.
|
|
Environment.SetEnvironmentVariable("EXECUTERCASEPROBE", "inherited");
|
|
try
|
|
{
|
|
var records = new List<NormalizedOutputRecord>();
|
|
await ExternalScriptRunner.RunAsync(
|
|
ScriptEngine.Bash,
|
|
"echo \"probe=$ExecuterCaseProbe\"",
|
|
prelude: null,
|
|
parameters: new Dictionary<string, object?> { ["ExecuterCaseProbe"] = "from-script" },
|
|
secrets: null,
|
|
secretsVariableName: "Secrets",
|
|
onRecord: r => { lock (records) records.Add(r); },
|
|
CancellationToken.None);
|
|
|
|
Assert.Contains(records, r => r.Text == "probe=from-script");
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable("EXECUTERCASEPROBE", null);
|
|
}
|
|
}
|
|
}
|