dotnet-installer/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/TestCommand.cs

223 lines
7 KiB
C#
Raw Normal View History

// Copyright (c) .NET Foundation and contributors. All rights reserved.
2015-12-15 01:39:29 +00:00
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Cli.Utils;
using System;
using System.Diagnostics;
2016-01-22 08:42:33 +00:00
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
2015-12-15 01:39:29 +00:00
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public class TestCommand
{
protected string _command;
2016-04-21 20:41:22 +00:00
private string _baseDirectory;
2016-03-28 10:18:13 +00:00
public string WorkingDirectory { get; set; }
public Process CurrentProcess { get; set; }
public Dictionary<string, string> Environment { get; } = new Dictionary<string, string>();
private List<Action<string>> _writeLines = new List<Action<string>>();
2015-12-15 01:39:29 +00:00
public TestCommand(string command)
{
_command = command;
2016-04-21 20:41:22 +00:00
#if NET451
_baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
#else
_baseDirectory = AppContext.BaseDirectory;
#endif
2015-12-15 01:39:29 +00:00
}
public virtual CommandResult Execute(string args = "")
2015-12-15 01:39:29 +00:00
{
2016-01-22 08:42:33 +00:00
var commandPath = _command;
ResolveCommand(ref commandPath, ref args);
2015-12-15 01:39:29 +00:00
Console.WriteLine($"Executing - {commandPath} {args} - {WorkingDirectoryInfo()}");
var stdOut = new StreamForwarder();
var stdErr = new StreamForwarder();
AddWriteLine(Reporter.Output.WriteLine);
stdOut.ForwardTo(writeLine: WriteLine);
stdErr.ForwardTo(writeLine: WriteLine);
return RunProcess(commandPath, args, stdOut, stdErr);
2015-12-15 01:39:29 +00:00
}
public virtual Task<CommandResult> ExecuteAsync(string args = "")
{
var commandPath = _command;
ResolveCommand(ref commandPath, ref args);
Console.WriteLine($"Executing - {commandPath} {args} - {WorkingDirectoryInfo()}");
var stdOut = new StreamForwarder();
var stdErr = new StreamForwarder();
AddWriteLine(Reporter.Output.WriteLine);
stdOut.ForwardTo(writeLine: WriteLine);
stdErr.ForwardTo(writeLine: WriteLine);
return RunProcessAsync(commandPath, args, stdOut, stdErr);
}
public virtual CommandResult ExecuteWithCapturedOutput(string args = "")
{
var command = _command;
ResolveCommand(ref command, ref args);
var commandPath = Env.GetCommandPath(command, ".exe", ".cmd", "") ??
2016-04-21 20:41:22 +00:00
Env.GetCommandPathFromRootPath(_baseDirectory, command, ".exe", ".cmd", "");
Console.WriteLine($"Executing (Captured Output) - {commandPath} {args} - {WorkingDirectoryInfo()}");
var stdOut = new StreamForwarder();
var stdErr = new StreamForwarder();
stdOut.ForwardTo(writeLine: WriteLine);
stdErr.ForwardTo(writeLine: WriteLine);
stdOut.Capture();
stdErr.Capture();
return RunProcess(commandPath, args, stdOut, stdErr);
}
public void KillTree()
{
if (CurrentProcess == null)
{
throw new InvalidOperationException("No process is available to be killed");
}
CurrentProcess.KillTree();
}
public void AddWriteLine(Action<string> writeLine)
{
_writeLines.Add(writeLine);
}
private void ResolveCommand(ref string executable, ref string args)
{
if (executable.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
var newArgs = ArgumentEscaper.EscapeSingleArg(executable);
if (!string.IsNullOrEmpty(args))
{
newArgs += " " + args;
}
args = newArgs;
2016-03-15 23:41:18 +00:00
executable = "dotnet";
}
if (!Path.IsPathRooted(executable))
{
executable = Env.GetCommandPath(executable) ??
2016-04-21 20:41:22 +00:00
Env.GetCommandPathFromRootPath(_baseDirectory, executable);
}
}
private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr)
{
CurrentProcess = StartProcess(executable, args);
var taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput);
var taskErr = stdErr.BeginRead(CurrentProcess.StandardError);
CurrentProcess.WaitForExit();
Task.WaitAll(taskOut, taskErr);
var result = new CommandResult(
CurrentProcess.StartInfo,
CurrentProcess.ExitCode,
stdOut.CapturedOutput,
stdErr.CapturedOutput);
return result;
}
private Task<CommandResult> RunProcessAsync(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr)
{
CurrentProcess = StartProcess(executable, args);
var taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput);
var taskErr = stdErr.BeginRead(CurrentProcess.StandardError);
var tcs = new TaskCompletionSource<CommandResult>();
CurrentProcess.Exited += (sender, arg) =>
{
Task.WaitAll(taskOut, taskErr);
var result = new CommandResult(
CurrentProcess.StartInfo,
CurrentProcess.ExitCode,
stdOut.CapturedOutput,
stdErr.CapturedOutput);
tcs.SetResult(result);
};
return tcs.Task;
}
private Process StartProcess(string executable, string args)
{
var psi = new ProcessStartInfo
{
FileName = executable,
Arguments = args,
RedirectStandardError = true,
RedirectStandardOutput = true,
2016-04-21 20:41:22 +00:00
RedirectStandardInput = true,
UseShellExecute = false
};
foreach (var item in Environment)
{
2016-04-21 20:41:22 +00:00
#if NET451
psi.EnvironmentVariables[item.Key] = item.Value;
#else
psi.Environment[item.Key] = item.Value;
2016-04-21 20:41:22 +00:00
#endif
}
2016-03-28 10:18:13 +00:00
if (!string.IsNullOrWhiteSpace(WorkingDirectory))
{
psi.WorkingDirectory = WorkingDirectory;
}
var process = new Process
{
2016-03-28 10:18:13 +00:00
StartInfo = psi
};
process.EnableRaisingEvents = true;
process.Start();
return process;
}
private void WriteLine(string line)
{
foreach (var writeLine in _writeLines)
{
writeLine(line);
}
}
private string WorkingDirectoryInfo()
{
if (WorkingDirectory == null)
{
return "";
}
return $" in pwd {WorkingDirectory}";
}
2015-12-15 01:39:29 +00:00
}
}