Skip to content

Commit 2a34431

Browse files
[rel/3.7] Add tests for Polyfill type forwarding (#4797)
Co-authored-by: Youssef1313 <[email protected]>
1 parent e886418 commit 2a34431

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Runtime.InteropServices;
5+
6+
using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
7+
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
8+
using Microsoft.Testing.Platform.Helpers;
9+
10+
namespace MSTest.Acceptance.IntegrationTests;
11+
12+
[TestGroup]
13+
public sealed class WinUITests : AcceptanceTestBase
14+
{
15+
private static readonly string WinUITargetFramework = $"{TargetFrameworks.NetCurrent.Arguments}-windows10.0.19041.0";
16+
private readonly TestAssetFixture _testAssetFixture;
17+
18+
public WinUITests(ITestExecutionContext testExecutionContext, TestAssetFixture testAssetFixture)
19+
: base(testExecutionContext) => _testAssetFixture = testAssetFixture;
20+
21+
public async Task SimpleWinUITestCase()
22+
{
23+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
24+
{
25+
// WinUI is Windows-only :)
26+
return;
27+
}
28+
29+
var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, WinUITargetFramework);
30+
TestHostResult testHostResult = await testHost.ExecuteAsync();
31+
32+
// Assert
33+
testHostResult.AssertExitCodeIs(ExitCodes.Success);
34+
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 1, skipped: 0);
35+
}
36+
37+
[TestFixture(TestFixtureSharingStrategy.PerTestGroup)]
38+
public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : TestAssetFixtureBase(acceptanceFixture.NuGetGlobalPackagesFolder)
39+
{
40+
public const string ProjectName = "WinUITests";
41+
42+
public string ProjectPath => GetAssetPath(ProjectName);
43+
44+
public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate()
45+
{
46+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
47+
{
48+
// WinUI is Windows-only :)
49+
yield break;
50+
}
51+
52+
yield return (ProjectName, ProjectName,
53+
SourceCode
54+
.PatchCodeWithReplace("$TargetFramework$", WinUITargetFramework)
55+
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
56+
}
57+
58+
private const string SourceCode = """
59+
#file WinUITests.csproj
60+
<Project Sdk="Microsoft.NET.Sdk">
61+
62+
<PropertyGroup>
63+
<OutputType>Exe</OutputType>
64+
<EnableMSTestRunner>true</EnableMSTestRunner>
65+
<TargetFramework>$TargetFramework$</TargetFramework>
66+
<UseWinUI>true</UseWinUI>
67+
</PropertyGroup>
68+
69+
<ItemGroup>
70+
<PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" />
71+
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
72+
</ItemGroup>
73+
74+
</Project>
75+
76+
#file UnitTest1.cs
77+
using System.Threading.Tasks;
78+
using Microsoft.VisualStudio.TestTools.UnitTesting;
79+
80+
[TestClass]
81+
public class TestClass1
82+
{
83+
[TestMethod]
84+
public void TestMethod1()
85+
{
86+
}
87+
}
88+
""";
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
5+
using Microsoft.Testing.Platform.Helpers;
6+
7+
namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;
8+
9+
[TestGroup]
10+
public class TypeForwardingTests : AcceptanceTestBase
11+
{
12+
private const string AssetName = "TypeForwardingTests";
13+
14+
private readonly AcceptanceFixture _acceptanceFixture;
15+
16+
// The idea of this test is to have a netstandard2.0 library that sets an init-only property.
17+
// The library is compiled against netstandard2.0 API of MTP. So, IsExternalInit is coming through Polyfill.
18+
// Then, console app is consuming the library and uses the latest TFM for MTP, which has IsExternalInit from BCL.
19+
// What happens now is:
20+
// At IL-level (compile-time), IsExternalInit from Polyfill is accessed.
21+
// At runtime, IsExternalInit doesn't exist from Polyfill and exists only through BCL.
22+
// For this situation to work, a TypeForwardedTo(typeof(IsExternalInit)) is needed in MTP when
23+
// compiling for a TFM that has IsExternalInit from BCL.
24+
// See https://github.com/SimonCropp/Polyfill/issues/290
25+
private const string Sources = """
26+
#file ClassLib/ClassLib.csproj
27+
<Project Sdk="Microsoft.NET.Sdk">
28+
<PropertyGroup>
29+
<TargetFramework>netstandard2.0</TargetFramework>
30+
<OutputType>Library</OutputType>
31+
<Nullable>enable</Nullable>
32+
<LangVersion>preview</LangVersion>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<PackageReference Include="Microsoft.Testing.Platform" Version="$MicrosoftTestingPlatformVersion$" />
36+
</ItemGroup>
37+
</Project>
38+
39+
#file ClassLib/MyClassCompiledAgainstNetStandardBinary.cs
40+
using Microsoft.Testing.Platform.Extensions.Messages;
41+
42+
public static class MyClassCompiledAgainstNetStandardBinary
43+
{
44+
public static TestNode M()
45+
=> new TestNode() { DisplayName = "MyDisplayName", Uid = new("MyUid") };
46+
}
47+
48+
#file ConsoleApp/ConsoleApp.csproj
49+
50+
<Project Sdk="Microsoft.NET.Sdk">
51+
<PropertyGroup>
52+
<TargetFramework>$TargetFrameworks$</TargetFramework>
53+
<OutputType>Exe</OutputType>
54+
<Nullable>enable</Nullable>
55+
<LangVersion>preview</LangVersion>
56+
</PropertyGroup>
57+
<ItemGroup>
58+
<ProjectReference Include="..\ClassLib\ClassLib.csproj" Version="$MicrosoftTestingPlatformVersion$" />
59+
</ItemGroup>
60+
</Project>
61+
62+
#file ConsoleApp/Program.cs
63+
using System;
64+
65+
Console.WriteLine(MyClassCompiledAgainstNetStandardBinary.M().DisplayName);
66+
""";
67+
68+
public TypeForwardingTests(ITestExecutionContext testExecutionContext, AcceptanceFixture acceptanceFixture)
69+
: base(testExecutionContext) => _acceptanceFixture = acceptanceFixture;
70+
71+
public async Task SettingDisplayNameFromNetStandardLibraryDuringNetCurrentRuntimeExecutionShouldNotCrash()
72+
{
73+
string patchedSources = Sources
74+
.PatchTargetFrameworks(TargetFrameworks.NetCurrent)
75+
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion);
76+
77+
TestAsset testAsset = await TestAsset.GenerateAssetAsync(AssetName, patchedSources);
78+
await DotnetCli.RunAsync($"build -m:1 -nodeReuse:false {testAsset.TargetAssetPath}/ConsoleApp -c Release", _acceptanceFixture.NuGetGlobalPackagesFolder.Path);
79+
80+
var testHost = TestInfrastructure.TestHost.LocateFrom($"{testAsset.TargetAssetPath}/ConsoleApp", "ConsoleApp", TargetFrameworks.NetCurrent.Arguments);
81+
TestHostResult testHostResult = await testHost.ExecuteAsync();
82+
83+
testHostResult.AssertExitCodeIs(ExitCodes.Success);
84+
testHostResult.AssertOutputContains("MyDisplayName");
85+
}
86+
}

0 commit comments

Comments
 (0)