Skip to content

Commit

Permalink
Fix 'The pipe is being closed' IOException when CMD process ignores a…
Browse files Browse the repository at this point in the history
… Control+C and is stopped. (#19)
  • Loading branch information
brendon1982 authored Nov 24, 2024
1 parent 240084a commit 64bbd38
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Proc/ObservableProcessBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ protected void SendYesForBatPrompt()
}
//best effort
catch (InvalidOperationException) { }
catch (IOException) { }
}
}

Expand Down
17 changes: 15 additions & 2 deletions tests/Proc.Tests/ControlCTestCases.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Xunit;

namespace ProcNet.Tests
{
Expand All @@ -24,6 +24,7 @@ [SkipOnNonWindowsFact] public void ControlC()
seen[0].Should().Be("Written before control+c");
seen[1].Should().Be("Written after control+c");
}

[SkipOnNonWindowsFact] public void ControlCSend()
{
var args = TestCaseArguments(nameof(ControlC));
Expand All @@ -49,7 +50,6 @@ [SkipOnNonWindowsFact] public void ControlCNoWait()
args.SendControlCFirst = true;

var process = new ObservableProcess(args);

var seen = new List<string>();
process.SubscribeLines(c=>
{
Expand All @@ -62,5 +62,18 @@ [SkipOnNonWindowsFact] public void ControlCNoWait()
seen[0].Should().Be("Written before control+c");
}

[SkipOnNonWindowsFact]
public void ControlCIngoredByCmd() {
var args = CmdTestCaseArguments("TrulyLongRunning");
args.SendControlCFirst = true;
args.WaitForExit = TimeSpan.FromSeconds(2);

var process = new ObservableProcess(args);
process.SubscribeLines(c => { });

Action call = () => process.WaitForCompletion(TimeSpan.FromSeconds(1));

call.ShouldNotThrow<IOException>();
}
}
}
9 changes: 9 additions & 0 deletions tests/Proc.Tests/TestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ private static string GetWorkingDir()
return binaryFolder;
}

protected static StartArguments CmdTestCaseArguments(string testcase, params string[] args) {
string[] arguments = ["/C", "dotnet", GetDll(), testcase];

return new StartArguments("cmd", arguments.Concat(args)) {
WorkingDirectory = GetWorkingDir(),
Timeout = WaitTimeout
};
}

protected static StartArguments TestCaseArguments(string testcase, params string[] args)
{
string[] arguments = [GetDll(), testcase];
Expand Down

0 comments on commit 64bbd38

Please sign in to comment.