FakeProcessRunner Class
A test double of IProcessRunner that starts no process: it records each run, and returns the outcome that a callback provides.
public sealed class FakeProcessRunner : IProcessRunnerRemarks
FakeProcessRunner.Runs lists the recorded runs. FakeProcessRunner.OnRun provides the outcome of each run. Without it, every run succeeds and has no output.
The fake treats the outcome of a run as ProcessRunner treats the outcome of a child, so that the code under test sees the same behavior from both:
- When
ProcessRunOptions.InheritStandardStreamsisfalse, the fake callsProcessRunOptions.OnStandardOutputLineonce per line of the standard output, thenProcessRunOptions.OnStandardErrorLineonce per line of the standard error. Each text of theProcessResultholds every line, followed byEnvironment.NewLine, whatever line terminatorFakeProcessRunner.OnRunwrote. - When
ProcessRunOptions.InheritStandardStreamsistrue, the fake calls neither callback, and both texts of theProcessResultare empty. - When the exit code is not 0 and
ProcessRunOptions.ThrowOnNonZeroExitCodeistrue,FakeProcessRunner.RunAsyncthrows aProcessFailedException. - When the
CancellationTokenof a run is signaled,FakeProcessRunner.RunAsyncthrows anOperationCanceledException. A token that is signaled when the run starts stands for a child that never started, and the fake does not record the run. A token thatFakeProcessRunner.OnRunor a line callback signals stands for a child that was killed. - When
FakeProcessRunner.OnRunor a line callback throws an exception,FakeProcessRunner.RunAsyncthrows the same exception. An exception ofFakeProcessRunner.OnRunstands for a child that cannot start.
The fake reads nothing from ProcessRunOptions.StandardInput, and makes no use of ProcessRunOptions.Environment, ProcessRunOptions.WorkingDirectory, and ProcessRunOptions.OutputEncoding. A test finds them in the FakeProcessRun.Options of the recorded run.
A run ends before FakeProcessRunner.RunAsync returns, and the fake calls every callback from the calling thread. One instance can serve any number of runs at the same time.
- Implements
Constructors
| Name | Description |
|---|---|
| FakeProcessRunner() |
FakeProcessRunner()
public FakeProcessRunner()No documentation is available.
Properties
| Name | Description |
|---|---|
| OnRun | Gets the callback that provides the outcome of each run. |
| Runs | Gets the runs that the fake recorded, in the order they started. |
OnRun
Gets the callback that provides the outcome of each run.
public Func<string, IReadOnlyList<string>, ProcessRunOptions, ProcessResult>? OnRun { get; init; }Property value
Func<string, IReadOnlyList<string>, ProcessRunOptions, ProcessResult>? A callback, or null for none. The default is null.
Remarks
The callback receives the executable, the arguments, and the options of the run, as FakeProcessRun holds them, and returns the outcome of the child it stands for: the exit code, and the text the child writes to each stream.
With null, the outcome of every run has the exit code 0, two empty texts, the command line that CommandLineFormatter.Format returns, and an elapsed time of zero.
Runs
Gets the runs that the fake recorded, in the order they started.
public IReadOnlyList<FakeProcessRun> Runs { get; }Property value
IReadOnlyList<FakeProcessRun> A copy of the list of runs, which later runs do not change.
Methods
| Name | Description |
|---|---|
| RunAsync(string, IEnumerable<string>, ProcessRunOptions?, CancellationToken) | Asynchronously runs a child process and waits for it to exit. |
RunAsync(string, IEnumerable<string>, ProcessRunOptions?, CancellationToken)
Asynchronously runs a child process and waits for it to exit.
public Task<ProcessResult> RunAsync(string executable, IEnumerable<string> arguments, ProcessRunOptions? options = null, CancellationToken cancellationToken = default)Parameters
executablestring- The name or the path of the program to run.
argumentsIEnumerable<string>- The arguments of the program. Each item reaches the program as one argument, whatever characters it holds.
optionsProcessRunOptions?- The options of the run, or
nullto use the default value of every option. cancellationTokenCancellationToken- A
CancellationTokenthat, when signaled, ends the run.
Returns
Task<ProcessResult> A Task<TResult> whose result describes the outcome of the run.
Exceptions
ArgumentNullExceptionexecutableisnull. - or -argumentsisnull.ArgumentExceptionargumentsholds anullitem.ProcessFailedException- The process exited with a code other than 0, and
ProcessRunOptions.ThrowOnNonZeroExitCodeistrue. OperationCanceledExceptioncancellationTokenwas signaled before the run ended.