Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Proc.Fs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ failure of the deferred command goes to standard error.
- A background process the block started that fails also fails the branches.
- Output of the branches interleaves line by line on the console.

### Running a block until a token is cancelled

`ShellBuilder().Run(steps { … }, cancellationToken)` runs a block that a caller can stop. Cancelling the token stops
the running command and throws `OperationCanceledException`; no command after it starts:

```fsharp
use cts = new CancellationTokenSource(TimeSpan.FromMinutes 5.)
ShellBuilder().Run(steps { sh $"dotnet test" }, cts.Token)
```

While the token can be cancelled, commands print through `Console.Out` and `Console.Error` line by line instead of
sharing the console. A host that replaces those writers, for example to prefix each line, sees every line. The output
is then a pipe, so a tool that colours only for a terminal prints without colour.

### Writing commands with `sh`

`sh` takes an interpolated string, `$"…"`. It splits **only the text you wrote**:
Expand Down
12 changes: 10 additions & 2 deletions src/Proc.Fs/Shell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,16 @@ type ShellBuilder(echoCommands: bool) =
result)

/// <summary>Runs the block. Background processes still running at its end are stopped.</summary>
member _.Run(step: ShellStep<'T>) : 'T =
let ctx = ShellContext(EchoCommands = echoCommands)
member b.Run(step: ShellStep<'T>) : 'T = b.Run(step, CancellationToken.None)

/// <summary>
/// Runs the block until <paramref name="cancellationToken"/> is cancelled: cancelling it stops the running command
/// and throws <see cref="OperationCanceledException"/>, and no command after it starts. While the token can be
/// cancelled, commands print through <see cref="Console.Out"/> and <see cref="Console.Error"/> line by line instead
/// of sharing the console, so a host that replaces those writers sees every line.
/// </summary>
member _.Run(step: ShellStep<'T>, cancellationToken: CancellationToken) : 'T =
let ctx = ShellContext(EchoCommands = echoCommands, Token = cancellationToken)
try ShellStep.run ctx step
finally ctx.StopBackgrounds()

Expand Down
36 changes: 36 additions & 0 deletions tests/Proc.Fs.Tests/ConcurrencyTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Proc.Fs.Tests.ConcurrencyTests
open System
open System.Diagnostics
open System.IO
open System.Threading
open ProcNet
open Proc.Fs
open Proc.Fs.Tests.TestBinary
Expand Down Expand Up @@ -295,3 +296,38 @@ type ConcurrentlyTests() =
return ()
})
Assert.Equal(Nullable 6, ex.ExitCode)

[<NotInParallel>]
type CancellationTests() =

[<Test>]
member _.``cancelling the token stops the running command and the ones after it`` () =
use cts = new CancellationTokenSource(TimeSpan.FromSeconds 1.)
let started = Stopwatch.StartNew()
let ranAfter = ref false
let ex =
try
ShellBuilder().Run(
steps {
do! test [ "Sleep"; "30000" ]
ranAfter.Value <- true
},
cts.Token)
None
with e -> Some e
Assert.True(
(match ex with Some (:? OperationCanceledException) -> true | _ -> false),
$"expected a cancellation, got {ex}")
Assert.False(ranAfter.Value, "a command after the cancelled one ran")
Assert.True(started.Elapsed < TimeSpan.FromSeconds 15., $"took {started.Elapsed}")

[<Test>]
member _.``with a token that can be cancelled the output goes through Console.Out`` () =
use cts = new CancellationTokenSource()
let writer = new StringWriter()
let original = Console.Out
Console.SetOut writer
try ShellBuilder().Run(steps { do! test [ "PrintArgs"; "through-console-out" ] }, cts.Token)
finally Console.SetOut original
let printed = string writer
Assert.True(printed.Contains "through-console-out", $"Console.Out saw: {printed}")
Loading