diff --git a/src/Proc.Fs/README.md b/src/Proc.Fs/README.md index 564250e..91d3fb4 100644 --- a/src/Proc.Fs/README.md +++ b/src/Proc.Fs/README.md @@ -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**: diff --git a/src/Proc.Fs/Shell.fs b/src/Proc.Fs/Shell.fs index ff74835..04c928e 100644 --- a/src/Proc.Fs/Shell.fs +++ b/src/Proc.Fs/Shell.fs @@ -184,8 +184,16 @@ type ShellBuilder(echoCommands: bool) = result) /// Runs the block. Background processes still running at its end are stopped. - member _.Run(step: ShellStep<'T>) : 'T = - let ctx = ShellContext(EchoCommands = echoCommands) + member b.Run(step: ShellStep<'T>) : 'T = b.Run(step, CancellationToken.None) + + /// + /// Runs the block until is cancelled: cancelling it stops the running command + /// and throws , and no command after it starts. While the token can be + /// cancelled, commands print through and line by line instead + /// of sharing the console, so a host that replaces those writers sees every line. + /// + member _.Run(step: ShellStep<'T>, cancellationToken: CancellationToken) : 'T = + let ctx = ShellContext(EchoCommands = echoCommands, Token = cancellationToken) try ShellStep.run ctx step finally ctx.StopBackgrounds() diff --git a/tests/Proc.Fs.Tests/ConcurrencyTests.fs b/tests/Proc.Fs.Tests/ConcurrencyTests.fs index ea5e50d..4b8cc8a 100644 --- a/tests/Proc.Fs.Tests/ConcurrencyTests.fs +++ b/tests/Proc.Fs.Tests/ConcurrencyTests.fs @@ -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 @@ -295,3 +296,38 @@ type ConcurrentlyTests() = return () }) Assert.Equal(Nullable 6, ex.ExitCode) + +[] +type CancellationTests() = + + [] + 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}") + + [] + 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}")