2015-11-30 16:24:03 -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 System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
using Microsoft.DotNet.ProjectModel;
|
2016-04-18 15:28:01 -07:00
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
2015-11-30 16:24:03 -08:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test
|
|
|
|
{
|
2016-01-30 21:47:50 -08:00
|
|
|
public class TestCommand
|
2015-11-30 16:24:03 -08:00
|
|
|
{
|
2016-03-25 20:17:15 -07:00
|
|
|
private readonly IDotnetTestRunnerFactory _dotnetTestRunnerFactory;
|
|
|
|
|
|
|
|
public TestCommand(IDotnetTestRunnerFactory testRunnerFactory)
|
|
|
|
{
|
|
|
|
_dotnetTestRunnerFactory = testRunnerFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int DoRun(string[] args)
|
2015-11-30 16:24:03 -08:00
|
|
|
{
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
|
|
|
|
2016-03-11 15:30:37 -08:00
|
|
|
var dotnetTestParams = new DotnetTestParams();
|
2015-11-30 16:24:03 -08:00
|
|
|
|
2016-03-11 15:30:37 -08:00
|
|
|
dotnetTestParams.Parse(args);
|
2016-01-26 06:39:13 -08:00
|
|
|
|
2016-03-11 15:30:37 -08:00
|
|
|
try
|
2015-11-30 16:24:03 -08:00
|
|
|
{
|
2016-03-25 20:17:15 -07:00
|
|
|
if (dotnetTestParams.Help)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-11 15:30:37 -08:00
|
|
|
// Register for parent process's exit event
|
|
|
|
if (dotnetTestParams.ParentProcessId.HasValue)
|
2015-11-30 16:24:03 -08:00
|
|
|
{
|
2016-03-11 15:30:37 -08:00
|
|
|
RegisterForParentProcessExit(dotnetTestParams.ParentProcessId.Value);
|
|
|
|
}
|
2016-01-26 06:39:13 -08:00
|
|
|
|
2016-04-18 15:28:01 -07:00
|
|
|
var projectContexts = CreateProjectContexts(dotnetTestParams.ProjectPath, dotnetTestParams.Runtime);
|
2016-03-01 21:15:07 -08:00
|
|
|
|
2016-03-11 15:30:37 -08:00
|
|
|
var projectContext = projectContexts.First();
|
2016-01-26 06:39:13 -08:00
|
|
|
|
2016-03-11 15:30:37 -08:00
|
|
|
var testRunner = projectContext.ProjectFile.TestRunner;
|
2015-11-30 16:24:03 -08:00
|
|
|
|
2016-03-25 20:17:15 -07:00
|
|
|
IDotnetTestRunner dotnetTestRunner = _dotnetTestRunnerFactory.Create(dotnetTestParams.Port);
|
2016-03-01 21:15:07 -08:00
|
|
|
|
2016-03-11 15:30:37 -08:00
|
|
|
return dotnetTestRunner.RunTests(projectContext, dotnetTestParams);
|
2016-03-01 21:15:07 -08:00
|
|
|
}
|
2016-03-11 15:30:37 -08:00
|
|
|
catch (InvalidOperationException ex)
|
2015-11-30 16:24:03 -08:00
|
|
|
{
|
2016-03-11 15:30:37 -08:00
|
|
|
TestHostTracing.Source.TraceEvent(TraceEventType.Error, 0, ex.ToString());
|
|
|
|
return -1;
|
2015-11-30 16:24:03 -08:00
|
|
|
}
|
2016-02-12 18:22:35 -08:00
|
|
|
catch (Exception ex)
|
2015-11-30 16:24:03 -08:00
|
|
|
{
|
2016-03-11 15:30:37 -08:00
|
|
|
TestHostTracing.Source.TraceEvent(TraceEventType.Error, 0, ex.ToString());
|
|
|
|
return -2;
|
2015-11-30 16:24:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:17:15 -07:00
|
|
|
public static int Run(string[] args)
|
|
|
|
{
|
|
|
|
var testCommand = new TestCommand(new DotnetTestRunnerFactory());
|
|
|
|
|
|
|
|
return testCommand.DoRun(args);
|
|
|
|
}
|
|
|
|
|
2015-11-30 16:24:03 -08:00
|
|
|
private static void RegisterForParentProcessExit(int id)
|
|
|
|
{
|
|
|
|
var parentProcess = Process.GetProcesses().FirstOrDefault(p => p.Id == id);
|
|
|
|
|
|
|
|
if (parentProcess != null)
|
|
|
|
{
|
|
|
|
parentProcess.EnableRaisingEvents = true;
|
|
|
|
parentProcess.Exited += (sender, eventArgs) =>
|
|
|
|
{
|
|
|
|
TestHostTracing.Source.TraceEvent(
|
|
|
|
TraceEventType.Information,
|
|
|
|
0,
|
|
|
|
"Killing the current process as parent process has exited.");
|
|
|
|
|
|
|
|
Process.GetCurrentProcess().Kill();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TestHostTracing.Source.TraceEvent(
|
|
|
|
TraceEventType.Information,
|
|
|
|
0,
|
|
|
|
"Failed to register for parent process's exit event. " +
|
|
|
|
$"Parent process with id '{id}' was not found.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 15:28:01 -07:00
|
|
|
private static IEnumerable<ProjectContext> CreateProjectContexts(string projectPath, string runtime)
|
2015-11-30 16:24:03 -08:00
|
|
|
{
|
|
|
|
projectPath = projectPath ?? Directory.GetCurrentDirectory();
|
|
|
|
|
|
|
|
if (!projectPath.EndsWith(Project.FileName))
|
|
|
|
{
|
|
|
|
projectPath = Path.Combine(projectPath, Project.FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!File.Exists(projectPath))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException($"{projectPath} does not exist.");
|
|
|
|
}
|
|
|
|
|
2016-04-18 15:28:01 -07:00
|
|
|
var runtimeIdentifiers = !string.IsNullOrEmpty(runtime) ?
|
|
|
|
new[] { runtime } :
|
|
|
|
PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers();
|
|
|
|
|
|
|
|
return ProjectContext.CreateContextForEachFramework(projectPath).Select(context => context.CreateRuntimeContext(runtimeIdentifiers));
|
2015-11-30 16:24:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|