Skip to content

Commit

Permalink
Fix bug where disposing the std input writer on Mono would not dispos…
Browse files Browse the repository at this point in the history
…e the underlying stream. Fixes #43
  • Loading branch information
madelson committed Apr 15, 2019
1 parent 48f3ab9 commit cc0cd4a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions MedallionShell.Tests/PlatformCompatibilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class PlatformCompatibilityTest
[TestMethod]
public void TestAttaching() => RunTest(() => PlatformCompatibilityTests.TestAttaching());

[TestMethod]
public void TestWriteToStandardInput() => RunTest(() => PlatformCompatibilityTests.TestWriteToStandardInput());

private static void RunTest(Expression<Action> testMethod)
{
var compiled = testMethod.Compile();
Expand Down
7 changes: 5 additions & 2 deletions MedallionShell.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Global
{1B474A08-2A88-4FEA-A290-0555556C8229}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B474A08-2A88-4FEA-A290-0555556C8229}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B474A08-2A88-4FEA-A290-0555556C8229}.Release|Any CPU.Build.0 = Release|Any CPU
{1B474A08-2A88-4FEA-A290-0555556C8229}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{1B474A08-2A88-4FEA-A290-0555556C8229}.Testing|Any CPU.Build.0 = Testing|Any CPU
{1B474A08-2A88-4FEA-A290-0555556C8229}.Testing|Any CPU.ActiveCfg = Release|Any CPU
{1B474A08-2A88-4FEA-A290-0555556C8229}.Testing|Any CPU.Build.0 = Release|Any CPU
{1F78EB31-414F-4C1E-893B-C281F6B4FFC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F78EB31-414F-4C1E-893B-C281F6B4FFC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F78EB31-414F-4C1E-893B-C281F6B4FFC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -38,4 +38,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7FBDF352-5B9E-45CD-9D41-F110560CD0CF}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions MedallionShell/Streams/MonoStandardIOWrapperStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public override long Position
set => this.stream.Position = value;
}

public override bool CanTimeout => this.stream.CanTimeout;
public override int ReadTimeout { get => this.stream.ReadTimeout; set => this.stream.ReadTimeout = value; }
public override int WriteTimeout { get => this.stream.WriteTimeout; set => this.stream.WriteTimeout = value; }

public override void Flush() => this.stream.Flush();

public override Task FlushAsync(CancellationToken cancellationToken) => this.stream.FlushAsync(cancellationToken);
Expand Down Expand Up @@ -75,5 +79,14 @@ public async override Task WriteAsync(byte[] buffer, int offset, int count, Canc
try { await this.stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); }
catch (IOException) { }
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
this.stream.Dispose();
}
base.Dispose(disposing);
}
}
}
8 changes: 8 additions & 0 deletions SampleCommand/PlatformCompatibilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public static void TestAttaching()
}
}

public static void TestWriteToStandardInput()
{
var command = Command.Run(SampleCommandPath, new[] { "echo" }, options: o => o.Timeout(TimeSpan.FromSeconds(5)));
command.StandardInput.WriteLine("abcd");
command.StandardInput.Dispose();
if (command.Result.StandardOutput != ("abcd" + Environment.NewLine)) { throw new InvalidOperationException($"Was '{command.StandardOutput}'"); }
}

private static void AssertThrows<TException>(Action action) where TException : Exception
{
try { action(); }
Expand Down

0 comments on commit cc0cd4a

Please sign in to comment.