Console.Clear(); throw System.IO.Exception

Bug #797150 reported by Juan José Montes de Oca Arbós on 2011-06-14
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit V2
Invalid
Undecided
Unassigned

Bug Description

When testing a console application and run the method Console.Clear(); NUnit's console generates the exception System.IO.Exception. This work at MonoDevelop Test GUI Console, NUnit Console no GUI and don't work at NUnit GUI Console and Resharper GUI Console.
Code Example:

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

namespace NUnitConsoleBug
{
    [TestFixture]
    public class NUnitConsole
    {
        [Test]
        public void TestConsoleWriteLine()
        {
            //This work!
            var sw = new StringWriter();
            Console.SetOut(sw);
            Console.WriteLine("Test WriteLine{0}", Environment.NewLine);
            Assert.IsTrue(sw.ToString().Contains(string.Format("Test WriteLine{0}", Environment.NewLine)));
            sw.Dispose();
        }

        [Test]
        public void TestConsoleClear()
        {
            //This dont work!
            var sw = new StringWriter();
            Console.SetOut(sw);
            Assert.DoesNotThrow(Console.Clear);
            sw.Dispose();
        }
    }
}

tags: added: confirm
Changed in nunitv2:
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 2.6.0
Charlie Poole (charlie.poole) wrote :

By experimenting, I ascertained that Console.Clear() always applies to the real console, even when Console.SetOut has been called.

For example, in a console program, this code

public static void Main()
{

}

Charlie Poole (charlie.poole) wrote :

Continuing above comment...

By experimenting, I ascertained that Console.Clear() always applies to the real console, even when Console.SetOut has been called.

For example, in a console program, this code

        public static void Main()
        {
            Console.WriteLine("Starting...");

            var savedOut = Console.Out;
            var sw = new StringWriter();
            Console.WriteLine("Setting Console to StringWriter");
            Console.SetOut(sw);

            Console.WriteLine("This should not appear");
            Console.Clear();
            Console.WriteLine("Neither should this");

            Console.SetOut(savedOut);
            Console.WriteLine("Exiting...");
        }

Results in a cleared screan with "Exiting..." displayed at the top.

Since Console.Clear() is being applied to the real console, the example test runs successfully under nunit-console, but causes an error under the nunit gui, which doesn't have a real console.

An interesting effect, but not an NUnit bug.

Charlie

Changed in nunitv2:
milestone: 2.6.0 → none
assignee: Charlie Poole (charlie.poole) → nobody
status: New → Invalid
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers

Bug attachments