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 : IProcessRunner

Remarks

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:

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

NameDescription
FakeProcessRunner()

FakeProcessRunner()

public FakeProcessRunner()

No documentation is available.

Properties

NameDescription
OnRunGets the callback that provides the outcome of each run.
RunsGets the runs that the fake recorded, in the order they started.

OnRun

Gets the callback that provides the outcome of each run.

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

NameDescription
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

executable string
The name or the path of the program to run.
arguments IEnumerable<string>
The arguments of the program. Each item reaches the program as one argument, whatever characters it holds.
options ProcessRunOptions?
The options of the run, or null to use the default value of every option.
cancellationToken CancellationToken
A CancellationToken that, when signaled, ends the run.

Returns

Task<ProcessResult> A Task<TResult> whose result describes the outcome of the run.

Exceptions

ArgumentNullException
executable is null. - or - arguments is null.
ArgumentException
arguments holds a null item.
ProcessFailedException
The process exited with a code other than 0, and ProcessRunOptions.ThrowOnNonZeroExitCode is true.
OperationCanceledException
cancellationToken was signaled before the run ended.