Skip to content

Commit d55af40

Browse files
committed
Don't inject Test target when TestingPlatformDisableCustomTestTarget is true
1 parent 935e299 commit d55af40

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project>
2+
<!-- Expose a test target usable by tests projects where this target is injected -->
3+
<Target Name="Test" Condition=" '$(UseMSBuildTestInfrastructure)' != 'True' " >
4+
<CallTarget Targets="_TestingPlatformTest" />
5+
</Target>
6+
</Project>

src/Platform/Microsoft.Testing.Platform.MSBuild/buildMultiTargeting/Microsoft.Testing.Platform.MSBuild.targets

+4-4
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,10 @@
267267
<CallTarget Targets="_TestingPlatformTest" />
268268
</Target>
269269

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

275275
<Target Name="_TestingPlatformTest">
276276
<!-- We call VSTest built-in workflow only for the multi-tfm for single tfm we need to directly call our target -->

test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/MSBuildTests.Test.cs

+24
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ public async Task InvokeTestingPlatform_Target_Showing_Error_And_Do_Not_Capture_
252252
compilationResult.AssertOutputContains(".NET Testing Platform");
253253
}
254254

255+
[TestMethod]
256+
public async Task TestingPlatformDisableCustomTestTarget_Should_Cause_UserDefined_Target_To_Run()
257+
{
258+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
259+
AssetName,
260+
SourceCode
261+
.PatchCodeWithReplace("$PlatformTarget$", "<PlatformTarget>x64</PlatformTarget>")
262+
.PatchCodeWithReplace("$TargetFrameworks$", $"<TargetFramework>{TargetFrameworks.NetCurrent}</TargetFramework>")
263+
.PatchCodeWithReplace("$AssertValue$", "true")
264+
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion));
265+
DotnetMuxerResult compilationResult = await DotnetCli.RunAsync($"build {testAsset.TargetAssetPath} -p:TestingPlatformDisableCustomTestTarget=true -p:ImportUserDefinedTestTarget=true -t:\"Build;Test\"", AcceptanceFixture.NuGetGlobalPackagesFolder.Path, failIfReturnValueIsNotZero: false);
266+
267+
compilationResult.AssertOutputContains("Error from UserDefinedTestTarget.targets");
268+
}
269+
255270
private const string SourceCode = """
256271
#file MSBuild Tests.csproj
257272
<Project Sdk="Microsoft.NET.Sdk">
@@ -270,6 +285,15 @@ public async Task InvokeTestingPlatform_Target_Showing_Error_And_Do_Not_Capture_
270285
<PackageReference Include="Microsoft.Testing.Platform.MSBuild" Version="$MicrosoftTestingPlatformVersion$" />
271286
<PackageReference Include="Microsoft.Testing.Platform" Version="$MicrosoftTestingPlatformVersion$" />
272287
</ItemGroup>
288+
289+
<Import Project="UserDefinedTestTarget.targets" Condition="'$(ImportUserDefinedTestTarget)' == 'true'" />
290+
</Project>
291+
292+
#file UserDefinedTestTarget.targets
293+
<Project>
294+
<Target Name="Test">
295+
<Error Text="Error from UserDefinedTestTarget.targets" />
296+
</Target>
273297
</Project>
274298
275299
#file Program.cs

0 commit comments

Comments
 (0)