2015-12-14 17:39:29 -08: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 14:05:02 -08:00
using System.Diagnostics ;
2016-01-22 00:42:33 -08:00
using System.IO ;
2016-02-18 01:09:23 -08:00
using System.Collections.Generic ;
2015-12-14 17:39:29 -08:00
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public class TestCommand
{
protected string _command ;
2016-02-18 01:09:23 -08:00
public Dictionary < string , string > Environment { get ; } = new Dictionary < string , string > ( ) ;
2015-12-14 17:39:29 -08:00
public TestCommand ( string command )
{
_command = command ;
}
2016-01-08 11:03:14 -08:00
public virtual CommandResult Execute ( string args = "" )
2015-12-14 17:39:29 -08:00
{
2016-01-22 00:42:33 -08:00
var commandPath = _command ;
if ( ! Path . IsPathRooted ( _command ) )
{
_command = Env . GetCommandPath ( _command ) ? ?
2016-02-09 15:30:04 -08:00
Env . GetCommandPathFromRootPath ( AppContext . BaseDirectory , _command ) ;
2016-01-22 00:42:33 -08:00
}
2015-12-14 17:39:29 -08:00
2016-01-22 00:42:33 -08:00
Console . WriteLine ( $"Executing - {_command} {args}" ) ;
2016-01-22 14:05:02 -08:00
var stdOut = new StreamForwarder ( ) ;
var stdErr = new StreamForwarder ( ) ;
2016-02-04 18:18:08 -08:00
stdOut . ForwardTo ( writeLine : Reporter . Output . WriteLine ) ;
stdErr . ForwardTo ( writeLine : Reporter . Output . WriteLine ) ;
2016-01-22 14:05:02 -08:00
return RunProcess ( commandPath , args , stdOut , stdErr ) ;
2015-12-14 17:39:29 -08:00
}
2015-12-30 17:02:59 -08:00
2016-01-08 11:03:14 -08:00
public virtual CommandResult ExecuteWithCapturedOutput ( string args = "" )
2015-12-30 17:02:59 -08:00
{
Console . WriteLine ( $"Executing (Captured Output) - {_command} {args}" ) ;
2016-01-22 14:05:02 -08:00
var commandPath = Env . GetCommandPath ( _command , ".exe" , ".cmd" , "" ) ? ?
2016-02-09 15:30:04 -08:00
Env . GetCommandPathFromRootPath ( AppContext . BaseDirectory , _command , ".exe" , ".cmd" , "" ) ;
2016-01-22 03:59:04 -08:00
2016-01-22 14:05:02 -08:00
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
} ;
2016-02-18 01:09:23 -08:00
foreach ( var item in Environment )
{
psi . Environment [ item . Key ] = item . Value ;
}
2016-01-22 14:05:02 -08:00
var process = new Process
{
2016-02-18 01:09:23 -08:00
StartInfo = psi ,
2016-01-22 14:05:02 -08:00
} ;
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 (
2016-01-24 01:04:39 -08:00
process . StartInfo ,
2016-01-22 14:05:02 -08:00
process . ExitCode ,
2016-02-04 18:18:08 -08:00
stdOut . CapturedOutput ,
stdErr . CapturedOutput ) ;
2016-01-22 14:05:02 -08:00
return result ;
2015-12-30 17:02:59 -08:00
}
2015-12-14 17:39:29 -08:00
}
}