Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnyCPU tests to choose default architecture based on process #2206

Merged
merged 7 commits into from Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/build.ps1
Expand Up @@ -608,7 +608,7 @@ function Create-NugetPackages
Copy-Item $tpNuspecDir\uap\"Microsoft.TestPlatform.TestHost.Uap.targets" $testhostUapPackageDir\Microsoft.TestPlatform.TestHost.targets -Force

$testhostCorePackageDir = $(Join-Path $env:TP_OUT_DIR "$TPB_Configuration\Microsoft.TestPlatform.TestHost\$TPB_TargetFrameworkCore20")
Copy-Item $tpNuspecDir\"Microsoft.TestPlatform.TestHost.NetCore.props" $testhostCorePackageDir\Microsoft.TestPlatform.TestHost.props -Force
Copy-Item $tpNuspecDir\"Microsoft.TestPlatform.TestHost.NetCore.targets" $testhostCorePackageDir\Microsoft.TestPlatform.TestHost.targets -Force

# Call nuget pack on these components.
$nugetExe = Join-Path $env:TP_PACKAGES_DIR -ChildPath "Nuget.CommandLine" | Join-Path -ChildPath $env:NUGET_EXE_Version | Join-Path -ChildPath "tools\NuGet.exe"
Expand Down
Expand Up @@ -67,6 +67,8 @@ public class DotnetTestHostManager : ITestRuntimeProvider

private Architecture architecture;

private bool isVersionCheckRequired = true;

/// <summary>
/// Initializes a new instance of the <see cref="DotnetTestHostManager"/> class.
/// </summary>
Expand Down Expand Up @@ -111,8 +113,20 @@ public DotnetTestHostManager()

/// <summary>
/// Gets a value indicating whether the test host supports protocol version check
/// By default this is set to true. For host package version 15.0.0, this will be set to false;
/// </summary>
internal virtual bool IsVersionCheckRequired => !this.hostPackageVersion.StartsWith("15.0.0");
internal virtual bool IsVersionCheckRequired
{
get
{
return this.isVersionCheckRequired;
}

set
vagisha-nidhi marked this conversation as resolved.
Show resolved Hide resolved
{
this.isVersionCheckRequired = value;
}
}

/// <summary>
/// Gets a value indicating whether the test host supports protocol version check
Expand Down Expand Up @@ -203,14 +217,45 @@ public async Task<bool> LaunchTestHostAsync(TestProcessStartInfo testHostStartIn
EqtTrace.Verbose("DotnetTestHostmanager: File {0}, doesnot exist", depsFilePath);
}

// If Testhost.exe is available use it
var exeName = this.architecture == Architecture.X86 ? "testhost.x86.exe" : "testhost.exe";
var fullExePath = Path.Combine(sourceDirectory, exeName);
if (this.platformEnvironment.OperatingSystem.Equals(PlatformOperatingSystem.Windows) && this.fileHelper.Exists(fullExePath))
var runtimeConfigDevPath = Path.Combine(sourceDirectory, string.Concat(sourceFile, ".runtimeconfig.dev.json"));
var testHostPath = this.GetTestHostPath(runtimeConfigDevPath, depsFilePath, sourceDirectory);

// If testhost.exe is available use it
bool testHostExeFound = false;
if (this.platformEnvironment.OperatingSystem.Equals(PlatformOperatingSystem.Windows))
{
startInfo.FileName = fullExePath;
var exeName = this.architecture == Architecture.X86 ? "testhost.x86.exe" : "testhost.exe";
var fullExePath = Path.Combine(sourceDirectory, exeName);

// check for testhost.exe in sourceDirectory. If not found, check in nuget folder.
if (this.fileHelper.Exists(fullExePath))
{
EqtTrace.Verbose("DotnetTestHostManager: Testhost.exe/testhost.x86.exe found at path: " + fullExePath);
startInfo.FileName = fullExePath;
testHostExeFound = true;
}
else
{
// Check if testhost.dll is found in nuget folder.
if (testHostPath.IndexOf("microsoft.testplatform.testhost", StringComparison.OrdinalIgnoreCase) >= 0)
{
// testhost.dll is present in path {testHostNugetRoot}\lib\netcoreapp2.1\testhost.dll
// testhost.(x86).exe is present in location {testHostNugetRoot}\build\netcoreapp2.1\{x86/x64}\{testhost.x86.exe/testhost.exe}
var folderName = this.architecture == Architecture.X86 ? "x86" : "x64";
var testHostNugetRoot = new DirectoryInfo(testHostPath).Parent.Parent.Parent;
var testHostExeNugetPath = Path.Combine(testHostNugetRoot.FullName, "build", "netcoreapp2.1", folderName, exeName);

if (this.fileHelper.Exists(testHostExeNugetPath))
{
EqtTrace.Verbose("DotnetTestHostManager: Testhost.exe/testhost.x86.exe found at path: " + testHostExeNugetPath);
startInfo.FileName = testHostExeNugetPath;
testHostExeFound = true;
}
}
}
}
else

if (!testHostExeFound)
{
var currentProcessPath = this.processHelper.GetCurrentProcessFileName();

Expand All @@ -227,9 +272,6 @@ public async Task<bool> LaunchTestHostAsync(TestProcessStartInfo testHostStartIn
startInfo.FileName = this.dotnetHostHelper.GetDotnetPath();
}

var runtimeConfigDevPath = Path.Combine(sourceDirectory, string.Concat(sourceFile, ".runtimeconfig.dev.json"));
var testHostPath = this.GetTestHostPath(runtimeConfigDevPath, depsFilePath, sourceDirectory);

EqtTrace.Verbose("DotnetTestHostmanager: Full path of testhost.dll is {0}", testHostPath);
args = "exec" + args;
args += " " + testHostPath.AddDoubleQuote();
Expand Down Expand Up @@ -394,6 +436,7 @@ private string GetTestHostPath(string runtimeConfigDevPath, string depsFilePath,

testHostPath = Path.Combine(testhostPackage.Path, testHostPath);
this.hostPackageVersion = testhostPackage.Version;
this.IsVersionCheckRequired = !this.hostPackageVersion.StartsWith("15.0.0");
EqtTrace.Verbose("DotnetTestHostmanager: Relative path of testhost.dll with respect to package folder is {0}", testHostPath);
}
}
Expand Down
27 changes: 0 additions & 27 deletions src/package/nuspec/Microsoft.TestPlatform.TestHost.NetCore.props

This file was deleted.

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we try CopyToPublishDirectory in the props itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried. It works. Updated the PR

<Target Name="CopyTestHostExesToPublishDirectory" AfterTargets="ComputeFilesToPublish">
<Copy Condition=" ('$(Platform)' == 'x86' OR '$(PlatformTarget)' == 'x86') AND '$(OS)' == 'Windows_NT'" SourceFiles="$(MSBuildThisFileDirectory)x86\testhost.x86.exe" DestinationFolder="$(PublishDir)" />
<Copy Condition=" ('$(Platform)' == 'x86' OR '$(PlatformTarget)' == 'x86') AND '$(OS)' == 'Windows_NT'" SourceFiles="$(MSBuildThisFileDirectory)x86\testhost.x86.dll" DestinationFolder="$(PublishDir)" />
<Copy Condition=" ('$(Platform)' != 'x86' AND '$(PlatformTarget)' != 'x86') AND '$(OS)' == 'Windows_NT'" SourceFiles="$(MSBuildThisFileDirectory)x64\testhost.exe" DestinationFolder="$(PublishDir)" />
<Copy Condition=" ('$(Platform)' != 'x86' AND '$(PlatformTarget)' != 'x86') AND '$(OS)' == 'Windows_NT'" SourceFiles="$(MSBuildThisFileDirectory)x64\testhost.dll" DestinationFolder="$(PublishDir)" />
</Target>
</Project>
2 changes: 1 addition & 1 deletion src/package/nuspec/TestPlatform.TestHost.nuspec
Expand Up @@ -47,7 +47,7 @@
<file src="Microsoft.TestPlatform.TestHost\netcoreapp2.1\win7-x86\testhost.x86.dll" target="build\netcoreapp2.1\x86\" />
<file src="Microsoft.TestPlatform.TestHost\netcoreapp2.1\win7-x86\testhost.x86.exe" target="build\netcoreapp2.1\x86\" />

<file src="Microsoft.TestPlatform.TestHost\netcoreapp2.1\Microsoft.TestPlatform.TestHost.props" target="build\netcoreapp2.1\" />
<file src="Microsoft.TestPlatform.TestHost\netcoreapp2.1\Microsoft.TestPlatform.TestHost.targets" target="build\netcoreapp2.1\" />

<!-- UWP -->
<file src="Microsoft.TestPlatform.TestHost\uap10.0\testhost.dll" target="lib\uap10.0\" />
Expand Down
20 changes: 15 additions & 5 deletions src/vstest.console/TestPlatformHelpers/TestRequestManager.cs
Expand Up @@ -29,6 +29,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Utilities;

/// <summary>
Expand All @@ -47,6 +48,7 @@ internal class TestRequestManager : ITestRequestManager
private readonly object syncObject = new object();
private readonly Task<IMetricsPublisher> metricsPublisher;
private bool isDisposed;
private IProcessHelper processHelper;

/// <summary>
/// Maintains the current active execution request
Expand All @@ -69,18 +71,20 @@ public TestRequestManager()
TestRunResultAggregator.Instance,
TestPlatformEventSource.Instance,
new InferHelper(AssemblyMetadataProvider.Instance),
MetricsPublisherFactory.GetMetricsPublisher(IsTelemetryOptedIn(), CommandLineOptions.Instance.IsDesignMode))
MetricsPublisherFactory.GetMetricsPublisher(IsTelemetryOptedIn(), CommandLineOptions.Instance.IsDesignMode),
new ProcessHelper())
{
}

internal TestRequestManager(CommandLineOptions commandLineOptions, ITestPlatform testPlatform, TestRunResultAggregator testRunResultAggregator, ITestPlatformEventSource testPlatformEventSource, InferHelper inferHelper, Task<IMetricsPublisher> metricsPublisher)
internal TestRequestManager(CommandLineOptions commandLineOptions, ITestPlatform testPlatform, TestRunResultAggregator testRunResultAggregator, ITestPlatformEventSource testPlatformEventSource, InferHelper inferHelper, Task<IMetricsPublisher> metricsPublisher, IProcessHelper processHelper)
{
this.testPlatform = testPlatform;
this.commandLineOptions = commandLineOptions;
this.testRunResultAggregator = testRunResultAggregator;
this.testPlatformEventSource = testPlatformEventSource;
this.inferHelper = inferHelper;
this.metricsPublisher = metricsPublisher;
this.processHelper = processHelper;
}

#endregion
Expand Down Expand Up @@ -381,9 +385,15 @@ private bool UpdateRunSettingsIfRequired(string runsettingsXml, List<string> sou
settingsUpdated |= this.UpdateFramework(document, navigator, sources, sourceFrameworks, registrar, out Framework chosenFramework);

// Choose default architecture based on the framework
// For .NET core, the default platform architecture should be x64.
var defaultArchitecture = chosenFramework.Name.IndexOf("netstandard", StringComparison.OrdinalIgnoreCase) >= 0
|| chosenFramework.Name.IndexOf("netcoreapp", StringComparison.OrdinalIgnoreCase) >= 0 ? Architecture.X64 : Architecture.X86;
// For .NET core, the default platform architecture should be based on the process.
// For a 64 bit process,
Architecture defaultArchitecture = Architecture.X86;
if (chosenFramework.Name.IndexOf("netstandard", StringComparison.OrdinalIgnoreCase) >= 0
|| chosenFramework.Name.IndexOf("netcoreapp", StringComparison.OrdinalIgnoreCase) >= 0)
{
var currentProcessName = this.processHelper.GetProcessName(this.processHelper.GetCurrentProcessId());
defaultArchitecture = (currentProcessName.StartsWith("dotnet", StringComparison.OrdinalIgnoreCase) && !Environment.Is64BitProcess) ? Architecture.X86: Architecture.X64;
}

settingsUpdated |= this.UpdatePlatform(document, navigator, sources, sourcePlatforms, defaultArchitecture, out Architecture chosenPlatform);
this.CheckSourcesForCompatibility(chosenFramework, chosenPlatform, sourcePlatforms, sourceFrameworks, registrar);
Expand Down