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

[rel/3.7] Add tests for Polyfill type forwarding #4797

Merged
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
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");
}
}