2015-12-15 01:39:29 +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 Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using System;
|
2016-01-22 22:05:02 +00:00
|
|
|
|
using System.Diagnostics;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
|
{
|
|
|
|
|
public class TestCommand
|
|
|
|
|
{
|
|
|
|
|
protected string _command;
|
|
|
|
|
|
|
|
|
|
public TestCommand(string command)
|
|
|
|
|
{
|
|
|
|
|
_command = command;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-08 19:03:14 +00:00
|
|
|
|
public virtual CommandResult Execute(string args = "")
|
2015-12-15 01:39:29 +00:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Executing - {_command} {args}");
|
|
|
|
|
|
2016-01-22 22:05:02 +00:00
|
|
|
|
var commandPath = Env.GetCommandPath(_command) ??
|
|
|
|
|
Env.GetCommandPathFromAppBase(AppContext.BaseDirectory, _command);
|
|
|
|
|
|
|
|
|
|
var stdOut = new StreamForwarder();
|
|
|
|
|
var stdErr = new StreamForwarder();
|
|
|
|
|
|
|
|
|
|
stdOut.ForwardTo(write: Reporter.Output.Write, writeLine: Reporter.Output.WriteLine);
|
|
|
|
|
stdErr.ForwardTo(write: Reporter.Error.Write, writeLine: Reporter.Output.WriteLine);
|
|
|
|
|
|
|
|
|
|
return RunProcess(commandPath, args, stdOut, stdErr);
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
2015-12-31 01:02:59 +00:00
|
|
|
|
|
2016-01-08 19:03:14 +00:00
|
|
|
|
public virtual CommandResult ExecuteWithCapturedOutput(string args = "")
|
2015-12-31 01:02:59 +00:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Executing (Captured Output) - {_command} {args}");
|
|
|
|
|
|
2016-01-22 22:05:02 +00:00
|
|
|
|
var commandPath = Env.GetCommandPath(_command, ".exe", ".cmd", "") ??
|
|
|
|
|
Env.GetCommandPathFromAppBase(AppContext.BaseDirectory, _command, ".exe", ".cmd", "");
|
|
|
|
|
|
|
|
|
|
Console.Write("command");
|
|
|
|
|
Console.WriteLine(commandPath);
|
|
|
|
|
|
|
|
|
|
var stdOut = new StreamForwarder();
|
|
|
|
|
var stdErr = new StreamForwarder();
|
|
|
|
|
|
|
|
|
|
stdOut.Capture();
|
|
|
|
|
stdErr.Capture();
|
|
|
|
|
|
|
|
|
|
return RunProcess(commandPath, args, stdOut, stdErr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr)
|
|
|
|
|
{
|
|
|
|
|
var psi = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = executable,
|
|
|
|
|
Arguments = args,
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
RedirectStandardOutput = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var process = new Process
|
|
|
|
|
{
|
|
|
|
|
StartInfo = psi
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process.EnableRaisingEvents = true;
|
|
|
|
|
process.Start();
|
|
|
|
|
|
|
|
|
|
var threadOut = stdOut.BeginRead(process.StandardOutput);
|
|
|
|
|
var threadErr = stdErr.BeginRead(process.StandardError);
|
|
|
|
|
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
threadOut.Join();
|
|
|
|
|
threadErr.Join();
|
|
|
|
|
|
|
|
|
|
var result = new CommandResult(
|
|
|
|
|
process.ExitCode,
|
|
|
|
|
stdOut.GetCapturedOutput(),
|
|
|
|
|
stdErr.GetCapturedOutput());
|
|
|
|
|
|
|
|
|
|
return result;
|
2015-12-31 01:02:59 +00:00
|
|
|
|
}
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|