Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue where TextRunner accidentally disposes System.Out #4318

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/NUnitFramework/nunitlite.tests/TextRunnerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.IO;
using System.Text;
using NUnit.Framework;

namespace NUnitLite.Tests
{
[TestFixture]
public class TextRunnerTests
{
[Test]
public void CommandLineTests()
{
Console.SetOut(new WriteToStreamAndConsole(Console.Out, new MemoryStream()));

var textRunner = new TextRunner();
textRunner.Execute(new[] { "--noresult" });
Console.Out.WriteLine("Broken");
}
}

public class WriteToStreamAndConsole : TextWriter
{
private readonly TextWriter _writer;
private readonly StreamWriter _streamWriter;

public WriteToStreamAndConsole(TextWriter writer, Stream stream)
{
_writer = writer;
_streamWriter = new StreamWriter(stream);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
_writer.Dispose();
_streamWriter.Dispose();
}

base.Dispose(disposing);
}

// Only overriden WriteLine for simplicity sake
public override void WriteLine(string value)
{
_writer.WriteLine(value);
_streamWriter.WriteLine(value);

base.WriteLine(value);
}

public override Encoding Encoding => _writer.Encoding;
}
}
7 changes: 7 additions & 0 deletions src/NUnitFramework/nunitlite/ColorConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public override void WriteLabelLine(string label, object option, ColorStyle valu
WriteLine();
}

protected override void Dispose(bool disposing)
{
// The base class disposes the writer it's wrapping
// In our case the writer is System.Out,
// We don't want to dispose of System.Out as it could potentially break other peoples code
}

#endregion
}
}