Skip to content

Commit

Permalink
Don't inject Test target when `TestingPlatformDisableCustomTestTarget…
Browse files Browse the repository at this point in the history
…` is true (#4838)
  • Loading branch information
Youssef1313 authored Jan 30, 2025
1 parent 09a5cb6 commit 5265976
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<!-- Expose a test target usable by tests projects where this target is injected -->
<Target Name="Test" Condition=" '$(UseMSBuildTestInfrastructure)' != 'True' " >
<CallTarget Targets="_TestingPlatformTest" />
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@
<CallTarget Targets="_TestingPlatformTest" />
</Target>

<!-- Expose a test target usable by tests projects where this target is injected -->
<Target Name="Test" Condition=" '$(UseMSBuildTestInfrastructure)' != 'True' AND '$(TestingPlatformDisableCustomTestTarget)' == 'False' " >
<CallTarget Targets="_TestingPlatformTest" />
</Target>
<!-- The Test target is separated so that we can conditionally define it. -->
<!-- Putting a condition on a target determines whether the target is **executed** or not -->
<!-- We want to not define the Test target in the first place when the user sets TestingPlatformDisableCustomTestTarget to true -->
<Import Project="Microsoft.Testing.Platform.MSBuild.CustomTestTarget.targets" Condition="'$(TestingPlatformDisableCustomTestTarget)' == 'False'" />

<Target Name="_TestingPlatformTest">
<!-- We call VSTest built-in workflow only for the multi-tfm for single tfm we need to directly call our target -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ public async Task InvokeTestingPlatform_Target_Showing_Error_And_Do_Not_Capture_
compilationResult.AssertOutputContains(".NET Testing Platform");
}

[TestMethod]
public async Task TestingPlatformDisableCustomTestTarget_Should_Cause_UserDefined_Target_To_Run()
{
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
AssetName,
SourceCode
.PatchCodeWithReplace("$PlatformTarget$", "<PlatformTarget>x64</PlatformTarget>")
.PatchCodeWithReplace("$TargetFrameworks$", $"<TargetFramework>{TargetFrameworks.NetCurrent}</TargetFramework>")
.PatchCodeWithReplace("$AssertValue$", "true")
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion));
DotnetMuxerResult compilationResult = await DotnetCli.RunAsync($"build {testAsset.TargetAssetPath} -p:TestingPlatformDisableCustomTestTarget=true -p:ImportUserDefinedTestTarget=true -t:\"Build;Test\"", AcceptanceFixture.NuGetGlobalPackagesFolder.Path, failIfReturnValueIsNotZero: false);

compilationResult.AssertOutputContains("Error from UserDefinedTestTarget.targets");
}

private const string SourceCode = """
#file MSBuild Tests.csproj
<Project Sdk="Microsoft.NET.Sdk">
Expand All @@ -270,6 +285,15 @@ public async Task InvokeTestingPlatform_Target_Showing_Error_And_Do_Not_Capture_
<PackageReference Include="Microsoft.Testing.Platform.MSBuild" Version="$MicrosoftTestingPlatformVersion$" />
<PackageReference Include="Microsoft.Testing.Platform" Version="$MicrosoftTestingPlatformVersion$" />
</ItemGroup>
<Import Project="UserDefinedTestTarget.targets" Condition="'$(ImportUserDefinedTestTarget)' == 'true'" />
</Project>
#file UserDefinedTestTarget.targets
<Project>
<Target Name="Test">
<Error Text="Error from UserDefinedTestTarget.targets" />
</Target>
</Project>
#file Program.cs
Expand Down

0 comments on commit 5265976

Please sign in to comment.