2016-04-15 04:33:41 +00:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
2016-05-02 22:46:48 +00:00
|
|
|
|
using System.IO;
|
2016-04-15 04:33:41 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils
|
|
|
|
|
{
|
|
|
|
|
public class BuiltInCommandTests : TestBase
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tests that BuiltInCommand.Execute returns the correct exit code and a
|
|
|
|
|
/// valid StartInfo FileName and Arguments.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void TestExecute()
|
|
|
|
|
{
|
|
|
|
|
Func<string[], int> testCommand = args => args.Length;
|
|
|
|
|
string[] testCommandArgs = new[] { "1", "2" };
|
|
|
|
|
|
|
|
|
|
var builtInCommand = new BuiltInCommand("fakeCommand", testCommandArgs, testCommand);
|
|
|
|
|
CommandResult result = builtInCommand.Execute();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(testCommandArgs.Length, result.ExitCode);
|
|
|
|
|
Assert.Equal(new Muxer().MuxerPath, result.StartInfo.FileName);
|
|
|
|
|
Assert.Equal("fakeCommand 1 2", result.StartInfo.Arguments);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tests that BuiltInCommand.Execute raises the OnOutputLine and OnErrorLine
|
|
|
|
|
/// the correct number of times and with the correct content.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void TestOnOutputLines()
|
|
|
|
|
{
|
2016-05-02 22:46:48 +00:00
|
|
|
|
const int exitCode = 29;
|
|
|
|
|
|
|
|
|
|
TestBuiltInCommandEnvironment environment = new TestBuiltInCommandEnvironment();
|
2016-04-15 04:33:41 +00:00
|
|
|
|
|
|
|
|
|
Func<string[], int> testCommand = args =>
|
|
|
|
|
{
|
2016-05-02 22:46:48 +00:00
|
|
|
|
TextWriter outWriter = environment.GetConsoleOut();
|
|
|
|
|
outWriter.Write("first");
|
|
|
|
|
outWriter.WriteLine("second");
|
|
|
|
|
outWriter.WriteLine("third");
|
2016-04-15 04:33:41 +00:00
|
|
|
|
|
2016-05-02 22:46:48 +00:00
|
|
|
|
TextWriter errorWriter = environment.GetConsoleError();
|
|
|
|
|
errorWriter.WriteLine("fourth");
|
|
|
|
|
errorWriter.WriteLine("fifth");
|
2016-04-15 04:33:41 +00:00
|
|
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int onOutputLineCallCount = 0;
|
|
|
|
|
int onErrorLineCallCount = 0;
|
|
|
|
|
|
2016-05-02 22:46:48 +00:00
|
|
|
|
CommandResult result = new BuiltInCommand("fakeCommand", Enumerable.Empty<string>(), testCommand, environment)
|
2016-04-15 04:33:41 +00:00
|
|
|
|
.OnOutputLine(line =>
|
|
|
|
|
{
|
|
|
|
|
onOutputLineCallCount++;
|
|
|
|
|
|
|
|
|
|
if (onOutputLineCallCount == 1)
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal($"firstsecond", line);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal($"third", line);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.OnErrorLine(line =>
|
|
|
|
|
{
|
|
|
|
|
onErrorLineCallCount++;
|
|
|
|
|
|
|
|
|
|
if (onErrorLineCallCount == 1)
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal($"fourth", line);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal($"fifth", line);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.Execute();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(exitCode, result.ExitCode);
|
|
|
|
|
Assert.Equal(2, onOutputLineCallCount);
|
|
|
|
|
Assert.Equal(2, onErrorLineCallCount);
|
|
|
|
|
}
|
2016-05-02 22:46:48 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-15 04:33:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|