Skip to content

Commit

Permalink
Merge pull request #4317 from normj/normj/fix-console-dispose
Browse files Browse the repository at this point in the history
Ensure Console.Out is not disposed and is restored if it was replaced during the test run
  • Loading branch information
OsirisTerje committed Mar 31, 2023
2 parents ce3b2ce + 3ed73fa commit 24ad51a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
34 changes: 33 additions & 1 deletion src/NUnitFramework/nunitlite.tests/ExtendedTextWrapperTests.cs
@@ -1,4 +1,4 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -80,5 +80,37 @@ public void WriteLabelLine_ColorStyle()
writer.WriteLabelLine(LABEL, OPTION, ColorStyle.Pass);
Assert.That(sb.ToString(), Is.EqualTo(LABEL + OPTION + NL));
}

[Test]
public void DisposeSkipsStdOut()
{
var originalStdOut = Console.Out;

var textWriter = new DisposeCheckerWriter();

Console.SetOut(textWriter);
try
{
var colorWriter = new ColorConsoleWriter();
colorWriter.Dispose();

Assert.IsFalse(textWriter.IsDisposed);
}
finally
{
Console.SetOut(originalStdOut);
}
}

class DisposeCheckerWriter : StringWriter
{
public bool IsDisposed { get; private set; }
protected override void Dispose(bool disposing)
{
IsDisposed = true;

base.Dispose(disposing);
}
}
}
}
2 changes: 1 addition & 1 deletion src/NUnitFramework/nunitlite/ColorConsoleWriter.cs
Expand Up @@ -22,7 +22,7 @@ public class ColorConsoleWriter : ExtendedTextWrapper
/// </summary>
/// <param name="colorEnabled">Flag indicating whether color should be enabled</param>
public ColorConsoleWriter(bool colorEnabled)
: base(Console.Out)
: base(Console.Out, shouldDisposeWriter:false)
{
_colorEnabled = colorEnabled;
}
Expand Down
14 changes: 12 additions & 2 deletions src/NUnitFramework/nunitlite/ExtendedTextWrapper.cs
@@ -1,4 +1,4 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System.IO;
using System.Text;
Expand All @@ -14,12 +14,19 @@ namespace NUnit.Common
public class ExtendedTextWrapper : ExtendedTextWriter
{
private readonly TextWriter _writer;
private readonly bool _shouldDisposeWriter = true;

public ExtendedTextWrapper(TextWriter writer)
{
_writer = writer;
}

public ExtendedTextWrapper(TextWriter writer, bool shouldDisposeWriter)
: this(writer)
{
_shouldDisposeWriter = shouldDisposeWriter;
}

#region TextWriter Overrides

/// <summary>
Expand Down Expand Up @@ -56,7 +63,10 @@ public override void WriteLine(string value)
/// </summary>
protected override void Dispose(bool disposing)
{
_writer.Dispose();
if(_shouldDisposeWriter)
{
_writer.Dispose();
}
}

#endregion
Expand Down
45 changes: 35 additions & 10 deletions src/NUnitFramework/nunitlite/TextRunner.cs
Expand Up @@ -89,6 +89,9 @@ public TextRunner(Assembly testAssembly)

public int Execute(string[] args)
{
TextWriter originalStdOutWriter = null;
TextWriter originalStdErrWriter = null;

_options = new NUnitLiteOptions(_testAssembly == null, args);

ExtendedTextWriter outWriter = null;
Expand All @@ -97,27 +100,49 @@ public int Execute(string[] args)
var outFile = Path.Combine(_options.WorkDirectory, _options.OutFile);
var textWriter = TextWriter.Synchronized(new StreamWriter(outFile));
outWriter = new ExtendedTextWrapper(textWriter);
originalStdOutWriter = Console.Out;
Console.SetOut(outWriter);
}
else
{
outWriter = new ColorConsoleWriter(!_options.NoColor);
}

using (outWriter)
try
{
TextWriter errWriter = null;
if (_options.ErrFile != null)
using (outWriter)
{
var errFile = Path.Combine(_options.WorkDirectory, _options.ErrFile);
errWriter = TextWriter.Synchronized(new StreamWriter(errFile));
Console.SetError(errWriter);
TextWriter errWriter = null;
if (_options.ErrFile != null)
{
var errFile = Path.Combine(_options.WorkDirectory, _options.ErrFile);
errWriter = TextWriter.Synchronized(new StreamWriter(errFile));
originalStdErrWriter = Console.Error;
Console.SetError(errWriter);
}

try
{
using (errWriter)
{
_textUI = new TextUI(outWriter, Console.In, _options);
return Execute();
}
}
finally
{
if (originalStdErrWriter != null)
{
Console.SetError(originalStdErrWriter);
}
}
}

using (errWriter)
}
finally
{
if (originalStdOutWriter != null)
{
_textUI = new TextUI(outWriter, Console.In, _options);
return Execute();
Console.SetOut(originalStdOutWriter);
}
}
}
Expand Down

0 comments on commit 24ad51a

Please sign in to comment.