Fixing the BuiltInCommandTests to not mutate global shared state.

The BuiltInCommandTests sets the current Console.Out and Console.Error, which causes the test to fail if some other test is running and writes to the console at the same time.

Fix #2768
This commit is contained in:
Eric Erhardt 2016-05-02 17:46:48 -05:00
parent 9a85205781
commit 16b996f25b
5 changed files with 121 additions and 17 deletions

View file

@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
@ -35,16 +36,20 @@ namespace Microsoft.DotNet.Cli.Utils
[Fact]
public void TestOnOutputLines()
{
int exitCode = 29;
const int exitCode = 29;
TestBuiltInCommandEnvironment environment = new TestBuiltInCommandEnvironment();
Func<string[], int> testCommand = args =>
{
Console.Out.Write("first");
Console.Out.WriteLine("second");
Console.Out.WriteLine("third");
TextWriter outWriter = environment.GetConsoleOut();
outWriter.Write("first");
outWriter.WriteLine("second");
outWriter.WriteLine("third");
Console.Error.WriteLine("fourth");
Console.Error.WriteLine("fifth");
TextWriter errorWriter = environment.GetConsoleError();
errorWriter.WriteLine("fourth");
errorWriter.WriteLine("fifth");
return exitCode;
};
@ -52,7 +57,7 @@ namespace Microsoft.DotNet.Cli.Utils
int onOutputLineCallCount = 0;
int onErrorLineCallCount = 0;
CommandResult result = new BuiltInCommand("fakeCommand", Enumerable.Empty<string>(), testCommand)
CommandResult result = new BuiltInCommand("fakeCommand", Enumerable.Empty<string>(), testCommand, environment)
.OnOutputLine(line =>
{
onOutputLineCallCount++;
@ -85,5 +90,41 @@ namespace Microsoft.DotNet.Cli.Utils
Assert.Equal(2, onOutputLineCallCount);
Assert.Equal(2, onErrorLineCallCount);
}
private class TestBuiltInCommandEnvironment : IBuiltInCommandEnvironment
{
private TextWriter _consoleOut;
private TextWriter _consoleError;
public TextWriter GetConsoleOut()
{
return _consoleOut;
}
public void SetConsoleOut(TextWriter newOut)
{
_consoleOut = newOut;
}
public TextWriter GetConsoleError()
{
return _consoleError;
}
public void SetConsoleError(TextWriter newError)
{
_consoleError = newError;
}
public string GetWorkingDirectory()
{
return Directory.GetCurrentDirectory();
}
public void SetWorkingDirectory(string path)
{
// no-op
}
}
}
}

View file

@ -1,7 +1,8 @@
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
"emitEntryPoint": true,
"keyFile": "../../tools/Key.snk"
},
"dependencies": {
"Microsoft.NETCore.App": {