Skip to content

Commit

Permalink
Fix running self-contained apps on Windows (#2358)
Browse files Browse the repository at this point in the history
* Copy testhost to the output directory to allow self-contained apps to run (on Windows)

* Add tests for self-contained app
  • Loading branch information
nohwnd authored Mar 10, 2020
1 parent 7eaa0a2 commit 3726dae
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
<ItemGroup Condition=" ('$(Platform)' == 'x86' OR '$(PlatformTarget)' == 'x86') AND '$(OS)' == 'Windows_NT'">
<Content Include="$(MSBuildThisFileDirectory)x86\testhost.x86.exe">
<Link>testhost.x86.exe</Link>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</Content>
<Content Include="$(MSBuildThisFileDirectory)x86\testhost.x86.dll">
<Link>testhost.x86.dll</Link>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</Content>
</ItemGroup>
<ItemGroup Condition=" ('$(Platform)'!= 'x86' AND '$(PlatformTarget)' != 'x86') AND '$(OS)' == 'Windows_NT'" >
<Content Include="$(MSBuildThisFileDirectory)x64\testhost.exe">
<Link>testhost.exe</Link>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</Content>
<Content Include="$(MSBuildThisFileDirectory)x64\testhost.dll">
<Link>testhost.dll</Link>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</Content>
</ItemGroup>
Expand Down
20 changes: 11 additions & 9 deletions test/Microsoft.TestPlatform.AcceptanceTests/AcceptanceTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public class AcceptanceTestBase : IntegrationTestBase
{
public const string DesktopTargetFramework = "net451";
public const string Core21TargetFramework = "netcoreapp2.1";
public const string Core31TargetFramework = "netcoreapp3.1";

public const string Core21FrameworkArgValue = ".NETCoreApp,Version=v2.1";
public const string Core31FrameworkArgValue = ".NETCoreApp,Version=v3.1";
public const string DesktopFrameworkArgValue = ".NETFramework,Version=v4.5.1";
public const string DesktopRunnerTargetRuntime = "win7-x64";
public const string CoreRunnerTargetRuntime = "";
Expand All @@ -29,17 +31,17 @@ protected static void SetTestEnvironment(IntegrationTestEnvironment testEnvironm

protected static string DeriveFrameworkArgValue(IntegrationTestEnvironment testEnvironment)
{
string framworkArgValue = string.Empty;
if (string.Equals(testEnvironment.TargetFramework, Core21TargetFramework, StringComparison.Ordinal))
switch (testEnvironment.TargetFramework)
{
framworkArgValue = Core21FrameworkArgValue;
case Core21TargetFramework:
return Core21FrameworkArgValue;
case Core31TargetFramework:
return Core31FrameworkArgValue;
case DesktopTargetFramework:
return DesktopFrameworkArgValue;
default:
throw new NotSupportedException($"{testEnvironment.TargetFramework} is not supported TargetFramework value.");
}
else if (string.Equals(testEnvironment.TargetFramework, DesktopTargetFramework, StringComparison.Ordinal))
{
framworkArgValue = DesktopFrameworkArgValue;
}

return framworkArgValue;
}

protected bool IsDesktopTargetFramework()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,56 @@ namespace Microsoft.TestPlatform.AcceptanceTests
/// </summary>
public class NetCoreTargetFrameworkDataSource : Attribute, ITestDataSource
{
private List<object[]> dataRows = new List<object[]>();
/// <summary>
/// Initializes a new instance of the <see cref="NetCoreTargetFrameworkDataSource"/> class.
/// </summary>
/// <param name="useDesktopRunner">To run tests with desktop runner(vstest.console.exe)</param>
/// <param name="useCoreRunner">To run tests with core runner(dotnet vstest.console.dll)</param>
public NetCoreTargetFrameworkDataSource(bool useDesktopRunner = true, bool useCoreRunner = true)
public NetCoreTargetFrameworkDataSource(
bool useDesktopRunner = true,
// adding another runner is not necessary until we need to start building against another
// sdk, because the netcoreapp2.1 executable is forward compatible
bool useCoreRunner = true,
bool useNetCore21Target = true,
// laying the ground work here for tests to be able to run against 3.1 but not enabling it for
// all tests to avoid changing all acceptance tests right now
bool useNetCore31Target = false)
{
this.dataRows = new List<object[]>(6);

if (useDesktopRunner)
{
this.dataRows.Add(new object[]
var runnerFramework = IntegrationTestBase.DesktopRunnerFramework;
if (useNetCore21Target)
{
this.AddRunnerDataRow(runnerFramework, AcceptanceTestBase.Core21TargetFramework);
}

if (useNetCore31Target)
{
new RunnerInfo(IntegrationTestBase.DesktopRunnerFramework, AcceptanceTestBase.Core21TargetFramework)
});
this.AddRunnerDataRow(runnerFramework, AcceptanceTestBase.Core31TargetFramework);
}
}

if (useCoreRunner)
{
this.dataRows.Add(new object[]
var runnerFramework = IntegrationTestBase.CoreRunnerFramework;
if (useNetCore21Target)
{
new RunnerInfo(IntegrationTestBase.CoreRunnerFramework, AcceptanceTestBase.Core21TargetFramework)
});
this.AddRunnerDataRow(runnerFramework, AcceptanceTestBase.Core21TargetFramework);
}

if (useNetCore31Target)
{
this.AddRunnerDataRow(runnerFramework, AcceptanceTestBase.Core31TargetFramework);
}
}
}

private List<object[]> dataRows;
private void AddRunnerDataRow(string runnerFramework, string targetFramework)
{
var runnerInfo = new RunnerInfo(runnerFramework, targetFramework);
this.dataRows.Add(new object[] { runnerInfo });
}

public IEnumerable<object[]> GetData(MethodInfo methodInfo)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.AcceptanceTests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

[TestClass]
public class SelfContainedAppTests : AcceptanceTestBase
{
[TestMethod]
// this is core 3.1 only, full framework and netcoreapp2.1 don't "publish" automatically during build
// but if you run it on 2.1 it will pass because we execute the test normally
[NetCoreTargetFrameworkDataSource(useDesktopRunner: false, useNetCore21Target: false, useNetCore31Target: true)]
public void RunningApplicationThatIsBuiltAsSelfContainedWillNotFailToFindHostpolicyDll(RunnerInfo runnerInfo)
{
// when the application is self-contained which is dictated by the RuntimeIdentifier and OutputType project
// properties, the testhost.exe executable is given a runtimeconfig that instructs it to find a hostpolicy.dll and hostfxr.dll next to it
// that will fail if we run the testhost.exe from the .nuget location, but will work when we run it from the output folder
// see https://github.com/dotnet/runtime/issues/3569#issuecomment-595820524 and below for description of how it works
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);

// the app is published to win10-x64 because of the runtime identifier in the project
var assemblyPath = this.BuildMultipleAssemblyPath(@"win10-x64\SelfContainedAppTestProject.dll").Trim('\"');
var arguments = PrepareArguments(assemblyPath, null, null, this.FrameworkArgValue, runnerInfo.InIsolationValue);
this.InvokeVsTest(arguments);

this.ValidateSummaryStatus(passedTestsCount: 1, 0, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ protected bool IsDesktopRunner()

protected bool IsNetCoreRunner()
{
return this.testEnvironment.RunnerFramework == IntegrationTestBase.CoreRunnerFramework;
return this.testEnvironment.RunnerFramework == IntegrationTestBase.CoreRunnerFramework;
}

/// <summary>
Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions test/TestAssets/SelfContainedAppTestProject/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SelfContainedAppTestProject
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}
13 changes: 13 additions & 0 deletions test/TestAssets/TestAssets.sln/TestAssets.sln
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleProjectWithOldTestHos
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleClassLibrary", "..\SimpleClassLibrary\SimpleClassLibrary.csproj", "{F37144D7-2C6D-42AF-9E85-EF10E5244A7B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SelfContainedAppTestProject", "..\SelfContainedAppTestProject\SelfContainedAppTestProject.csproj", "{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoverletCoverageTestProject", "..\CoverletCoverageTestProject\CoverletCoverageTestProject.csproj", "{0D4D59D7-C52F-4858-A220-EAC7484E3827}"
EndProject
Global
Expand Down Expand Up @@ -440,6 +441,18 @@ Global
{F37144D7-2C6D-42AF-9E85-EF10E5244A7B}.Release|x64.Build.0 = Release|Any CPU
{F37144D7-2C6D-42AF-9E85-EF10E5244A7B}.Release|x86.ActiveCfg = Release|Any CPU
{F37144D7-2C6D-42AF-9E85-EF10E5244A7B}.Release|x86.Build.0 = Release|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Debug|x64.ActiveCfg = Debug|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Debug|x64.Build.0 = Debug|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Debug|x86.ActiveCfg = Debug|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Debug|x86.Build.0 = Debug|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Release|Any CPU.Build.0 = Release|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Release|x64.ActiveCfg = Release|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Release|x64.Build.0 = Release|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Release|x86.ActiveCfg = Release|Any CPU
{7F85E9D0-7BCE-431D-B468-4F6104E52DFA}.Release|x86.Build.0 = Release|Any CPU
{0D4D59D7-C52F-4858-A220-EAC7484E3827}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D4D59D7-C52F-4858-A220-EAC7484E3827}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D4D59D7-C52F-4858-A220-EAC7484E3827}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down

0 comments on commit 3726dae

Please sign in to comment.