Files
ExecuteR/tests/Agent.Tests/FilePathExpanderTests.cs
Alphaeus Mote ea60dc7848 feat(scripts): pick S3 files and folders, Download To root, live progress and cleanup [skip ci]
- Script editor: Add From S3 opens the S3 browser in multi-select mode
  (files and folders, search, breadcrumb); each pick gets a unique
  identifier name. Download To (default $ExecuteRJobDir\Files) with the
  variables on hover, per-binding Path inside it, Clean Up Downloads (on).
- Run Script: Review > Files > Change for This Run overrides the selection
  for one run (needs scripts.manage or files.send), sealed into the
  run's snapshot.
- Storage: FileBindingsJson becomes {root, cleanUp, bindings}; the older
  array form still reads (absolute destinations, nothing removed). No
  schema change.
- Server: only the selected keys are listed and presigned; single files
  carry their listed size; a folder over 2,000 objects is refused.
- Agent: paths expand on the device ($ExecuteRJobDir, $ExecuteRAgentDir,
  $ExecuteRAgentPath, %VAR%, $env:VAR, $VAR/${VAR} on Unix, $(...));
  undefined variables and ".." fail before the script runs. Missing
  folders are created (inheriting the parent's ACL). Four downloads at
  once, streamed, retried, checked against size and plain-MD5 ETags,
  timeout/cancel aware. Information lines and progress records while it
  runs, and a summary. $Files (FileInfo/DirectoryInfo, ordered,
  case-insensitive) and $FilesRoot in PowerShell 7 and 5.1;
  EXECUTER_FILE_<NAME> and EXECUTER_FILES_ROOT in Bash. Clean Up
  Downloads removes only what the job wrote, on success, failure,
  timeout or cancel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 06:17:41 -05:00

108 lines
4.7 KiB
C#

using Agent.PowerShell;
using Xunit;
namespace Agent.Tests;
/// <summary>Every form a Download To root or binding path may use, expanded on the device.</summary>
public sealed class FilePathExpanderTests : IDisposable
{
private const string JobDir = @"C:\Jobs\1234";
private const string AgentDir = @"C:\Program Files\ExecuteR\Agent";
private const string AgentPath = @"C:\Program Files\ExecuteR\Agent\executer-agent.exe";
private static readonly Dictionary<string, string> Environment = new(StringComparer.OrdinalIgnoreCase)
{
["ProgramData"] = @"C:\ProgramData",
["DATA_HOME"] = "/srv/data",
["ProgramFiles(x86)"] = @"C:\Program Files (x86)",
};
private readonly Lazy<System.Management.Automation.Runspaces.Runspace> runspace = new(() => PowerShellRunspaceFactory.Create());
public void Dispose()
{
if (runspace.IsValueCreated)
{
runspace.Value.Dispose();
}
}
private FilePathExpander Windows(bool withRunspace = true) => new(
JobDir, withRunspace ? () => runspace.Value : null, unixStyle: false,
environment: name => Environment.GetValueOrDefault(name), agentDirectory: AgentDir, agentPath: AgentPath);
private FilePathExpander Unix() => new(
"/tmp/ExecuteR/1234", () => runspace.Value, unixStyle: true,
environment: name => Environment.GetValueOrDefault(name), agentDirectory: "/opt/executer/agent", agentPath: "/opt/executer/agent/executer-agent");
[Theory]
[InlineData(@"$ExecuteRJobDir\Files", @"C:\Jobs\1234\Files")]
[InlineData(@"${ExecuteRJobDir}\Files", @"C:\Jobs\1234\Files")]
[InlineData(@"$executerjobdir\Files", @"C:\Jobs\1234\Files")]
[InlineData(@"$ExecuteRAgentDir\Tools", @"C:\Program Files\ExecuteR\Agent\Tools")]
[InlineData(@"$ExecuteRAgentPath", AgentPath)]
[InlineData(@"%EXECUTER_JOB_DIR%\Files", @"C:\Jobs\1234\Files")]
[InlineData(@"$env:EXECUTER_AGENT_DIR\x", @"C:\Program Files\ExecuteR\Agent\x")]
[InlineData(@"%ProgramData%\Vendor", @"C:\ProgramData\Vendor")]
[InlineData(@"%ProgramFiles(x86)%\Vendor", @"C:\Program Files (x86)\Vendor")]
[InlineData(@"$env:ProgramData\Vendor", @"C:\ProgramData\Vendor")]
[InlineData(@"${env:ProgramData}\Vendor", @"C:\ProgramData\Vendor")]
[InlineData(@"D:\Plain\Path", @"D:\Plain\Path")]
[InlineData("relative\\sub", "relative\\sub")]
public void WindowsFormsExpandWithoutARunspace(string raw, string expected)
=> Assert.Equal(expected, Windows(withRunspace: false).Expand(raw));
[Theory]
[InlineData(@"$ExecuteRJobDir\Files", "/tmp/ExecuteR/1234/Files")]
[InlineData("$EXECUTER_JOB_DIR/Files", "/tmp/ExecuteR/1234/Files")]
[InlineData("$DATA_HOME/drop", "/srv/data/drop")]
[InlineData("${DATA_HOME}/drop", "/srv/data/drop")]
[InlineData("$env:DATA_HOME/drop", "/srv/data/drop")]
[InlineData("%DATA_HOME%/drop", "/srv/data/drop")]
[InlineData("$ExecuteRAgentDir/bin", "/opt/executer/agent/bin")]
public void UnixFormsExpandAndUseSlashes(string raw, string expected)
=> Assert.Equal(expected, Unix().Expand(raw));
[Fact]
public void SubexpressionsRunInPowerShellWithExecuteRsVariables()
{
var expanded = Windows().Expand(@"$(Join-Path $ExecuteRJobDir 'Deep Folder')\x");
Assert.Equal(@"C:\Jobs\1234\Deep Folder\x", expanded);
}
[Fact]
public void PowerShellsOwnVariablesStillWork()
=> Assert.Equal(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile), "x"),
Path.GetFullPath(Windows().Expand(@"$HOME\x")));
[Theory]
[InlineData(@"%NOT_DEFINED_HERE%\x", "%NOT_DEFINED_HERE%")]
[InlineData(@"$env:NOT_DEFINED_HERE\x", "$env:NOT_DEFINED_HERE")]
[InlineData(@"${env:NOT_DEFINED_HERE}\x", "NOT_DEFINED_HERE")]
[InlineData(@"$NotDefinedHere\x", "$NotDefinedHere")]
public void AnUndefinedVariableIsAnErrorNotAnEmptyString(string raw, string named)
{
var problem = Assert.Throws<FilePathException>(() => Windows().Expand(raw));
Assert.Contains(named, problem.Message);
}
[Fact]
public void AnUndefinedUnixVariableIsAnError()
=> Assert.Throws<FilePathException>(() => Unix().Expand("$NOT_DEFINED_HERE/x"));
[Fact]
public void AnExpandedValueIsNotExpandedAgain()
{
var expander = new FilePathExpander(
@"C:\Jobs\%ProgramData%", null, unixStyle: false, environment: name => Environment.GetValueOrDefault(name));
Assert.Equal(@"C:\Jobs\%ProgramData%\x", expander.Expand(@"$ExecuteRJobDir\x"));
}
[Fact]
public void AnUnclosedSubexpressionIsAnError()
=> Assert.Throws<FilePathException>(() => Windows().Expand(@"$(Get-Item x\y"));
}