Skip to content

Commit

Permalink
Merge pull request #4325 from normj/v3.13-dev
Browse files Browse the repository at this point in the history
Backport PR 4317 - Ensure Console.Out is not disposed and is restored
  • Loading branch information
OsirisTerje committed Apr 3, 2023
2 parents a1fef50 + d2bb453 commit 5c72050
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) 2015 Charlie Poole, Rob Prouse
//
// Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -101,5 +101,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 @@ -43,7 +43,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) 2015 Charlie Poole, Rob Prouse
//
// Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -35,12 +35,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 @@ -80,7 +87,10 @@ public override Encoding Encoding
/// </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 @@ -110,6 +110,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 @@ -118,27 +121,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 5c72050

Please sign in to comment.