w4c-workflows-api/w4c-workflows-api.Tests/TestHelpers.cs

55 lines
1.5 KiB
C#

using w4c_workflows.Services.Execution;
namespace w4c_workflows.Tests;
/// <summary>A self-cleaning temp directory for executor tests.</summary>
internal sealed class TempDir : IDisposable
{
public string Path { get; }
public TempDir()
{
Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "wf-test-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path);
}
public string Write(string relativePath, string content)
{
var full = System.IO.Path.Combine(Path, relativePath);
var dir = System.IO.Path.GetDirectoryName(full);
if (!string.IsNullOrEmpty(dir))
Directory.CreateDirectory(dir);
File.WriteAllText(full, content);
return full;
}
public void Dispose()
{
try { Directory.Delete(Path, recursive: true); } catch { /* best effort */ }
}
}
internal static class Invocation
{
/// <summary>Builds a minimal task invocation for a single-file language.</summary>
public static TaskInvocation For(
string language,
string entryFile,
string? input = null,
string? workingDir = null,
string? entryFunction = null)
=> new(
TaskInvocation.TypeValue,
"run-1",
"task-1",
"task-key",
language,
entryFile,
entryFunction,
new Dictionary<string, string>(),
input,
workingDir,
"tenant-1",
1);
}