feat(welcome): process runner abstraction
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
namespace SilverOS.Welcome.Core.Apply;
|
||||||
|
public readonly record struct ProcessResult(int ExitCode, string StdOut, string StdErr);
|
||||||
|
public interface IProcessRunner
|
||||||
|
{
|
||||||
|
Task<ProcessResult> RunAsync(string file, string args, CancellationToken ct = default);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
namespace SilverOS.Welcome.Core.Apply;
|
||||||
|
|
||||||
|
public sealed class ProcessRunner : IProcessRunner
|
||||||
|
{
|
||||||
|
public async Task<ProcessResult> RunAsync(string file, string args, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
using var p = new Process { StartInfo = new ProcessStartInfo(file, args)
|
||||||
|
{
|
||||||
|
RedirectStandardOutput = true, RedirectStandardError = true,
|
||||||
|
UseShellExecute = false, CreateNoWindow = true
|
||||||
|
}};
|
||||||
|
p.Start();
|
||||||
|
var outT = p.StandardOutput.ReadToEndAsync(ct);
|
||||||
|
var errT = p.StandardError.ReadToEndAsync(ct);
|
||||||
|
await p.WaitForExitAsync(ct);
|
||||||
|
return new ProcessResult(p.ExitCode, await outT, await errT);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using SilverOS.Welcome.Core.Apply;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class ProcessRunnerTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Runs_powershell_and_captures_output_and_exit()
|
||||||
|
{
|
||||||
|
var r = await new ProcessRunner().RunAsync(
|
||||||
|
"powershell.exe", "-NoProfile -Command \"Write-Output hello; exit 3\"");
|
||||||
|
Assert.Equal(3, r.ExitCode);
|
||||||
|
Assert.Contains("hello", r.StdOut);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user