Skip to content

Commit

Permalink
[rel/3.7] Add tests for Polyfill type forwarding (#4797)
Browse files Browse the repository at this point in the history
Co-authored-by: Youssef1313 <youssefvictor00@gmail.com>
  • Loading branch information
youssef-backport-bot and Youssef1313 authored Jan 27, 2025
1 parent e886418 commit 2a34431
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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.Runtime.InteropServices;

using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
using Microsoft.Testing.Platform.Helpers;

namespace MSTest.Acceptance.IntegrationTests;

[TestGroup]
public sealed class WinUITests : AcceptanceTestBase
{
private static readonly string WinUITargetFramework = $"{TargetFrameworks.NetCurrent.Arguments}-windows10.0.19041.0";
private readonly TestAssetFixture _testAssetFixture;

public WinUITests(ITestExecutionContext testExecutionContext, TestAssetFixture testAssetFixture)
: base(testExecutionContext) => _testAssetFixture = testAssetFixture;

public async Task SimpleWinUITestCase()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// WinUI is Windows-only :)
return;
}

var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, WinUITargetFramework);
TestHostResult testHostResult = await testHost.ExecuteAsync();

// Assert
testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 1, skipped: 0);
}

[TestFixture(TestFixtureSharingStrategy.PerTestGroup)]
public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : TestAssetFixtureBase(acceptanceFixture.NuGetGlobalPackagesFolder)
{
public const string ProjectName = "WinUITests";

public string ProjectPath => GetAssetPath(ProjectName);

public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// WinUI is Windows-only :)
yield break;
}

yield return (ProjectName, ProjectName,
SourceCode
.PatchCodeWithReplace("$TargetFramework$", WinUITargetFramework)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
}

private const string SourceCode = """
#file WinUITests.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFramework>$TargetFramework$</TargetFramework>
<UseWinUI>true</UseWinUI>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" />
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
</ItemGroup>
</Project>
#file UnitTest1.cs
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TestClass1
{
[TestMethod]
public void TestMethod1()
{
}
}
""";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
using Microsoft.Testing.Platform.Helpers;

namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;

[TestGroup]
public class TypeForwardingTests : AcceptanceTestBase
{
private const string AssetName = "TypeForwardingTests";

private readonly AcceptanceFixture _acceptanceFixture;

// The idea of this test is to have a netstandard2.0 library that sets an init-only property.
// The library is compiled against netstandard2.0 API of MTP. So, IsExternalInit is coming through Polyfill.
// Then, console app is consuming the library and uses the latest TFM for MTP, which has IsExternalInit from BCL.
// What happens now is:
// At IL-level (compile-time), IsExternalInit from Polyfill is accessed.
// At runtime, IsExternalInit doesn't exist from Polyfill and exists only through BCL.
// For this situation to work, a TypeForwardedTo(typeof(IsExternalInit)) is needed in MTP when
// compiling for a TFM that has IsExternalInit from BCL.
// See https://github.com/SimonCropp/Polyfill/issues/290
private const string Sources = """
#file ClassLib/ClassLib.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<OutputType>Library</OutputType>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Testing.Platform" Version="$MicrosoftTestingPlatformVersion$" />
</ItemGroup>
</Project>
#file ClassLib/MyClassCompiledAgainstNetStandardBinary.cs
using Microsoft.Testing.Platform.Extensions.Messages;
public static class MyClassCompiledAgainstNetStandardBinary
{
public static TestNode M()
=> new TestNode() { DisplayName = "MyDisplayName", Uid = new("MyUid") };
}
#file ConsoleApp/ConsoleApp.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$TargetFrameworks$</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLib\ClassLib.csproj" Version="$MicrosoftTestingPlatformVersion$" />
</ItemGroup>
</Project>
#file ConsoleApp/Program.cs
using System;
Console.WriteLine(MyClassCompiledAgainstNetStandardBinary.M().DisplayName);
""";

public TypeForwardingTests(ITestExecutionContext testExecutionContext, AcceptanceFixture acceptanceFixture)
: base(testExecutionContext) => _acceptanceFixture = acceptanceFixture;

public async Task SettingDisplayNameFromNetStandardLibraryDuringNetCurrentRuntimeExecutionShouldNotCrash()
{
string patchedSources = Sources
.PatchTargetFrameworks(TargetFrameworks.NetCurrent)
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion);

TestAsset testAsset = await TestAsset.GenerateAssetAsync(AssetName, patchedSources);
await DotnetCli.RunAsync($"build -m:1 -nodeReuse:false {testAsset.TargetAssetPath}/ConsoleApp -c Release", _acceptanceFixture.NuGetGlobalPackagesFolder.Path);

var testHost = TestInfrastructure.TestHost.LocateFrom($"{testAsset.TargetAssetPath}/ConsoleApp", "ConsoleApp", TargetFrameworks.NetCurrent.Arguments);
TestHostResult testHostResult = await testHost.ExecuteAsync();

testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContains("MyDisplayName");
}
}

0 comments on commit 2a34431

Please sign in to comment.