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

Support arm64 native CppUnitTestFramework with dotnet test #3768

Merged
merged 6 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Microsoft.TestPlatform.CrossPlatEngine/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ private bool ShouldRunInProcess(
// Return true if
// 1) Not running in parallel;
// 2) Data collector is not enabled;
// 3) Target framework is X64 or anyCpu;
// 3) Target platform is compatible or AnyCpu;
// 4) DisableAppDomain is false;
// 5) Not running in design mode;
// 6) target framework is NETFramework (Desktop test);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -260,7 +261,6 @@ public virtual TestProcessStartInfo GetTestHostProcessStartInfo(

// Try find testhost.exe (or the architecture specific version). We ship those ngened executables for Windows because they have faster startup time. We ship them only for some platforms.
// When user specified path to dotnet.exe don't try to find the exexutable, because we will always use the testhost.dll together with their dotnet.exe.
// We use dotnet.exe on Windows/ARM.
bool testHostExeFound = false;
if (!useCustomDotnetHostpath
&& _platformEnvironment.OperatingSystem.Equals(PlatformOperatingSystem.Windows)
Expand Down Expand Up @@ -408,7 +408,7 @@ public virtual TestProcessStartInfo GetTestHostProcessStartInfo(

// We silently force x64 only if the target architecture is the default one and is not specified by user
// through --arch or runsettings or -- RunConfiguration.TargetPlatform=arch
bool forceToX64 = SilentlyForceToX64() && _runsettingHelper.IsDefaultTargetArchitecture;
bool forceToX64 = _runsettingHelper.IsDefaultTargetArchitecture && SilentlyForceToX64(sourcePath);
EqtTrace.Verbose($"DotnetTestHostmanager: Current process architetcure '{_processHelper.GetCurrentProcessArchitecture()}'");
bool isSameArchitecture = IsSameArchitecture(_architecture, _processHelper.GetCurrentProcessArchitecture());
var currentProcessPath = _processHelper.GetCurrentProcessFileName()!;
Expand Down Expand Up @@ -531,7 +531,7 @@ static bool IsSameArchitecture(Architecture targetArchitecture, PlatformArchitec
_ => throw new TestPlatformException($"Invalid target architecture '{targetArchitecture}'"),
};

bool SilentlyForceToX64()
bool SilentlyForceToX64(string sourcePath)
{
// We need to force x64 in some scenario
// https://github.com/dotnet/sdk/blob/main/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets#L140-L143
Expand All @@ -547,7 +547,25 @@ bool SilentlyForceToX64()
// If we are running on win-arm64 and the TFM is < 5.0, we have to use a x64 apphost since there are no win-arm64 apphosts previous to .NET 5.0.
return _platformEnvironment.OperatingSystem == PlatformOperatingSystem.Windows &&
_platformEnvironment.Architecture == PlatformArchitecture.ARM64 &&
new Version(_targetFramework.Version).Major < 5;
new Version(_targetFramework.Version).Major < 5 &&
!IsNativeModule(sourcePath);
}

bool IsNativeModule(string modulePath)
{
// Scenario: dotnet test nativeArm64.dll for CppUnitTestFramework
// If the dll is native and we're not running in process(vstest.console.exe)
// the expected target framework is ".NETCoreApp,Version=v1.0".
// In this case we don't want to force x64 architecture
using var assemblyStream = _fileHelper.GetStream(sourcePath, FileMode.Open, FileAccess.Read);
using var peReader = new PEReader(assemblyStream);
if (!peReader.HasMetadata || (peReader.PEHeaders.CorHeader?.Flags & CorFlags.ILOnly) == 0)
{
EqtTrace.Verbose($"DotnetTestHostmanager.IsNativeModule: Source '{sourcePath}' is native.");
return true;
}

return false;
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/Microsoft.TestPlatform.AcceptanceTests/DotnetTestTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.TestPlatform.AcceptanceTests;
Expand Down Expand Up @@ -71,4 +73,21 @@ public void RunDotnetTestWithDllPassInlineSettings(RunnerInfo runnerInfo)
ValidateSummaryStatus(1, 0, 0);
ExitCodeEquals(0);
}

[TestMethod]
// patched dotnet is not published on non-windows systems
[TestCategory("Windows-Review")]
[NetCoreTargetFrameworkDataSource(useDesktopRunner: false)]
public void RunDotnetTestWithNativeDll(RunnerInfo runnerInfo)
{
SetTestEnvironment(_testEnvironment, runnerInfo);

string assemblyRelativePath = @"microsoft.testplatform.testasset.nativecpp\2.0.0\contentFiles\any\any\x64\Microsoft.TestPlatform.TestAsset.NativeCPP.dll";
var assemblyAbsolutePath = Path.Combine(_testEnvironment.PackageDirectory, assemblyRelativePath);

InvokeDotnetTest($@"{assemblyAbsolutePath} --logger:""Console;Verbosity=normal""");

ValidateSummaryStatus(1, 1, 0);
ExitCodeEquals(1);
}
}