|
| 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