using System.Collections.Specialized; using Agent.PowerShell; using Shared.Contracts.Models; using Xunit; namespace Agent.Tests; /// /// The in-process PowerShell 7 engine with Run From File on (the script /// staged in its job folder and invoked by path) and off (from memory, as /// before): what the script sees of itself, and that everything else /// (prelude, parameters, secrets, $Files, exit codes, errors, formatting, /// progress, cancellation) behaves the same in both modes. /// public sealed class RunFromFileTests : IDisposable { private readonly string root = Path.Combine(Path.GetTempPath(), "ExecuteR-Tests-" + Guid.NewGuid().ToString("N")); private readonly JobFolder folder; public RunFromFileTests() => folder = JobFolder.Create(root, Guid.NewGuid()); public void Dispose() { try { Directory.Delete(root, recursive: true); } catch (IOException) { } } private sealed record Run(List Records, List Progress, PowerShellExecutionResult Result, string? ScriptPath) { public IEnumerable Lines => Records.Where(r => r.Stream == OutputStreamKind.Output).Select(r => r.Text); } private async Task RunAsync( string script, bool fromFile, string? prelude = null, IReadOnlyDictionary? parameters = null, IReadOnlyDictionary? sessionVariables = null, bool setWorkingDirectory = false, CancellationToken cancellationToken = default) { var records = new List(); var progress = new List(); var path = fromFile ? folder.WriteScript("Install-Baseline", ScriptEngine.PowerShell, script) : null; var result = await new PowerShellExecutionService().ExecuteAsync( script, parameters, prelude, r => { lock (records) records.Add(r); }, p => { lock (progress) progress.Add(p); }, cancellationToken, sessionVariables: sessionVariables, options: new ScriptRunOptions(folder.Path, path, setWorkingDirectory)); return new Run(records, progress, result, path); } private const string SelfReport = """ "root=$PSScriptRoot" "path=$PSCommandPath" "def=$($MyInvocation.MyCommand.Definition)" "name=$($MyInvocation.MyCommand.Name)" "job=$ExecuteRJobDir" """; [Fact] public async Task FromFileTheScriptSeesItsOwnFile() { var run = await RunAsync(SelfReport, fromFile: true); Assert.False(run.Result.HadErrors); var path = Path.Combine(folder.Path, "Install-Baseline.ps1"); Assert.Equal(path, run.ScriptPath); Assert.Contains($"root={folder.Path}", run.Lines); Assert.Contains($"path={path}", run.Lines); Assert.Contains($"def={path}", run.Lines); Assert.Contains("name=Install-Baseline.ps1", run.Lines); Assert.Contains($"job={folder.Path}", run.Lines); } [Fact] public async Task FromMemoryThereIsNoFileButTheJobFolderIsKnown() { var run = await RunAsync(SelfReport, fromFile: false); Assert.False(run.Result.HadErrors); Assert.Contains("root=", run.Lines); Assert.Contains("path=", run.Lines); Assert.Contains(run.Lines, l => l.StartsWith("def=") && l.Contains("$PSScriptRoot")); Assert.Contains($"job={folder.Path}", run.Lines); Assert.Empty(Directory.GetFiles(folder.Path)); } [Theory] [InlineData(true)] [InlineData(false)] public async Task PreludeFunctionsParametersSecretsAndFilesAreAvailable(bool fromFile) { var secrets = new OrderedDictionary(StringComparer.OrdinalIgnoreCase) { ["ApiKey"] = "k-123" }; var files = new OrderedDictionary(StringComparer.OrdinalIgnoreCase) { ["Payload"] = new FileInfo(Path.Combine(folder.Path, "payload.bin")) }; var run = await RunAsync( """ param([string]$Label, [int]$Count, [hashtable]$Map, [switch]$Force) "$(Get-Greeting $Label) x$Count map=$($Map.a) force=$Force secret=$($Secrets.apikey) file=$($Files.Payload.Name)" """, fromFile, prelude: "function Get-Greeting([string]$Name) { \"hello $Name\" }", parameters: new Dictionary { ["Label"] = "widget", ["Count"] = 3, ["Map"] = new System.Collections.Hashtable { ["a"] = 7 }, ["Force"] = true, }, sessionVariables: new Dictionary { ["Secrets"] = secrets, ["Files"] = files }); Assert.False(run.Result.HadErrors); Assert.Contains("hello widget x3 map=7 force=True secret=k-123 file=payload.bin", run.Lines); } [Theory] [InlineData(true, "exit 3", 3)] [InlineData(false, "exit 3", 3)] [InlineData(true, "exit 0", 0)] [InlineData(false, "exit 0", 0)] [InlineData(true, "'no exit'", null)] [InlineData(false, "'no exit'", null)] public async Task ExitCodesAreCapturedTheSameWay(bool fromFile, string script, int? expected) { var run = await RunAsync(script, fromFile, prelude: "function Unused { }"); Assert.Equal(expected, run.Result.ExitCode); Assert.False(run.Result.HadErrors); } [Theory] [InlineData(true)] [InlineData(false)] public async Task NativeCommandExitCodesDoNotCountAsTheScriptsExit(bool fromFile) { Assert.SkipUnless(OperatingSystem.IsWindows(), "uses cmd.exe"); var run = await RunAsync("cmd /c exit 5; 'after'", fromFile); Assert.Null(run.Result.ExitCode); Assert.False(run.Result.HadErrors); Assert.Equal(new ScriptOutcome(JobState.Succeeded, 0, null), ScriptOutcome.From(run.Result)); } [Fact] public async Task FromFileANestedScriptsExitStaysInsideIt() { var inner = Path.Combine(folder.Path, "inner.ps1"); File.WriteAllText(inner, "exit 7"); var run = await RunAsync("& (Join-Path $PSScriptRoot 'inner.ps1'); \"inner=$LASTEXITCODE\"; exit 2", fromFile: true); Assert.Contains("inner=7", run.Lines); Assert.Equal(2, run.Result.ExitCode); } [Theory] [InlineData(true)] [InlineData(false)] public async Task TerminatingAndNonTerminatingErrorsAreReported(bool fromFile) { var thrown = await RunAsync("'before'; throw 'boom'", fromFile); Assert.True(thrown.Result.HadErrors); Assert.Equal("boom", thrown.Result.FailureMessage); Assert.Contains("before", thrown.Lines); var written = await RunAsync("Write-Error 'soft'; 'after'", fromFile); Assert.True(written.Result.HadErrors); Assert.Null(written.Result.FailureMessage); Assert.Equal("soft", written.Result.Error?.Message); Assert.Contains("after", written.Lines); } [Theory] [InlineData(true)] [InlineData(false)] public async Task APreludeErrorIsReportedAndTheScriptStillRuns(bool fromFile) { var run = await RunAsync("'main ran'", fromFile, prelude: "throw 'prelude broke'"); Assert.True(run.Result.HadErrors); Assert.Null(run.Result.FailureMessage); Assert.Equal("prelude broke", run.Result.Error?.Message); Assert.Equal(2, run.Records.Count); Assert.Equal(OutputStreamKind.Error, run.Records[0].Stream); Assert.StartsWith("prelude broke", run.Records[0].Text); Assert.Equal(new NormalizedOutputRecord(OutputStreamKind.Output, "main ran"), run.Records[1]); Assert.Equal(new ScriptOutcome(JobState.Failed, 1, null), ScriptOutcome.From(run.Result)); } [Theory] [InlineData(true)] [InlineData(false)] public async Task OutputIsFormattedAndStreamsAndProgressAreCaptured(bool fromFile) { var run = await RunAsync( """ @{ Region = 'eu-west' } Write-Warning 'careful' Write-Information 'info line' Write-Verbose 'chatty' -Verbose Write-Progress -Activity 'Working' -Status 'Half' -PercentComplete 50 """, fromFile); Assert.DoesNotContain(run.Records, r => r.Text.Contains("System.Collections.Hashtable")); Assert.Contains(run.Lines, l => l.StartsWith("Region") && l.Contains("eu-west")); Assert.Contains(run.Records, r => r.Stream == OutputStreamKind.Warning && r.Text == "careful"); Assert.Contains(run.Records, r => r.Stream == OutputStreamKind.Information && r.Text == "info line"); Assert.Contains(run.Records, r => r.Stream == OutputStreamKind.Verbose && r.Text == "chatty"); Assert.Contains(run.Progress, p => p.Activity == "Working" && p.PercentComplete == 50); } [Theory] [InlineData(true)] [InlineData(false)] public async Task CancellationStopsTheScript(bool fromFile) { using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)); var started = DateTime.UtcNow; var run = await RunAsync("'started'; Start-Sleep -Seconds 60; 'finished'", fromFile, prelude: "function F { }", cancellationToken: cts.Token); Assert.True(run.Result.WasCancelled); Assert.DoesNotContain("finished", run.Lines); Assert.True(DateTime.UtcNow - started < TimeSpan.FromSeconds(30)); } [Fact] public async Task WorkingDirectoryOnStartsInTheScriptFolder() { Assert.SkipUnless(OperatingSystem.IsWindows(), "uses cmd.exe"); var run = await RunAsync("\"loc=$((Get-Location).Path)\"; \"native=$(cmd /c cd)\"", fromFile: true, setWorkingDirectory: true); // Windows paths compare without case: as SYSTEM the temp folder reads C:\windows\SystemTemp or C:\Windows\SystemTemp. Assert.Contains(run.Lines, l => string.Equals(l, $"loc={folder.Path}", StringComparison.OrdinalIgnoreCase)); Assert.Contains(run.Lines, l => string.Equals(l, $"native={folder.Path}", StringComparison.OrdinalIgnoreCase)); // The agent's own (process-wide) directory is not touched. Assert.NotEqual(folder.Path, Environment.CurrentDirectory); } [Theory] [InlineData(true)] [InlineData(false)] public async Task WorkingDirectoryOffKeepsTheAgentsDirectory(bool fromFile) { var run = await RunAsync("\"loc=$((Get-Location).Path)\"", fromFile, setWorkingDirectory: false); Assert.Contains($"loc={Path.TrimEndingDirectorySeparator(Environment.CurrentDirectory)}", run.Lines.Select(l => l.TrimEnd('\\', '/'))); } [Fact] public async Task WorkingDirectoryIsIgnoredWithoutAFile() { var run = await RunAsync("\"loc=$((Get-Location).Path)\"", fromFile: false, setWorkingDirectory: true); Assert.DoesNotContain($"loc={folder.Path}", run.Lines); } [Fact] public void TheEngineHookForScriptFileExitsIsAvailable() { // Guards SDK upgrades: without it a file's exit code falls back to $LASTEXITCODE. using var runspace = PowerShellRunspaceFactory.Create(); Assert.True(ScriptFileExit.TryPropagateNextExit(runspace)); } }