Skip to content

Commit c44d51b

Browse files
CopilotMatthewSteeples
authored andcommitted
Add ImplicitMSTestUsings property to control MSTest implicit usings
The addition of this namespace to Implicit Usings is causing us problems because we make use of the System.ComponentModel.DescriptionAttribute in more places. * Add ImplicitMSTestUsings property to control MSTest implicit usings * Add comprehensive tests for ImplicitMSTestUsings property
1 parent 07d36fd commit c44d51b

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

src/Package/MSTest.Sdk/Sdk/VSTest/VSTest.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
Ensure feature is available and user hasn't opted-out from it.
3232
See https://github.com/dotnet/sdk/blob/f9fdf2c7d94bc86dc443e5a9ffecbd1962b1d85d/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CSharp.props#L26-L34
3333
-->
34-
<ItemGroup Condition=" '$(ImplicitUsings)' == 'true' Or '$(ImplicitUsings)' == 'enable' ">
34+
<ItemGroup Condition="('$(ImplicitUsings)' == 'true' Or '$(ImplicitUsings)' == 'enable') And ('$(ImplicitMSTestUsings)' == '' Or '$(ImplicitMSTestUsings)' == 'true' Or '$(ImplicitMSTestUsings)' == 'enable')">
3535
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
3636
</ItemGroup>
3737

src/TestFramework/TestFramework.Extensions/buildTransitive/net6.0AndLater/MSTest.TestFramework.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Ensure feature is available and user hasn't opted-out from it.
2626
See https://github.com/dotnet/sdk/blob/f9fdf2c7d94bc86dc443e5a9ffecbd1962b1d85d/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CSharp.props#L26-L34
2727
-->
28-
<ItemGroup Condition="'$(ImplicitUsings)' == 'true' Or '$(ImplicitUsings)' == 'enable'">
28+
<ItemGroup Condition="('$(ImplicitUsings)' == 'true' Or '$(ImplicitUsings)' == 'enable') And ('$(ImplicitMSTestUsings)' == '' Or '$(ImplicitMSTestUsings)' == 'true' Or '$(ImplicitMSTestUsings)' == 'enable')">
2929
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
3030
</ItemGroup>
3131
</Project>

src/TestFramework/TestFramework.Extensions/buildTransitive/others/MSTest.TestFramework.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Ensure feature is available and user hasn't opted-out from it.
66
See https://github.com/dotnet/sdk/blob/f9fdf2c7d94bc86dc443e5a9ffecbd1962b1d85d/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CSharp.props#L26-L34
77
-->
8-
<ItemGroup Condition="'$(ImplicitUsings)' == 'true' Or '$(ImplicitUsings)' == 'enable'">
8+
<ItemGroup Condition="('$(ImplicitUsings)' == 'true' Or '$(ImplicitUsings)' == 'enable') And ('$(ImplicitMSTestUsings)' == '' Or '$(ImplicitMSTestUsings)' == 'true' Or '$(ImplicitMSTestUsings)' == 'enable')">
99
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
1010
</ItemGroup>
1111
</Project>

test/IntegrationTests/MSTest.Acceptance.IntegrationTests/SdkTests.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,95 @@ public async Task SettingIsTestApplicationToFalseReducesAddedExtensionsAndMakesP
446446
Assert.IsFalse(binLog.FindChildrenRecursive<SL.Property>(p => p.Name == "OutputType").Any(p => p.Value == "Exe"));
447447
}
448448

449+
[TestMethod]
450+
public async Task ImplicitMSTestUsings_WhenDisabled_DoesNotIncludeImplicitUsing()
451+
{
452+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
453+
AssetName,
454+
SingleTestSourceCode
455+
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)
456+
.PatchCodeWithReplace("$TargetFramework$", TargetFrameworks.NetCurrent)
457+
.PatchCodeWithReplace("$ExtraProperties$", "<ImplicitMSTestUsings>false</ImplicitMSTestUsings>")
458+
.PatchCodeWithReplace("using Microsoft.VisualStudio.TestTools.UnitTesting;", "// Explicit using removed to test implicit usings"));
459+
460+
// When ImplicitMSTestUsings is disabled, the compilation should fail because Microsoft.VisualStudio.TestTools.UnitTesting is not available
461+
DotnetMuxerResult compilationResult = await DotnetCli.RunAsync($"build -c Debug {testAsset.TargetAssetPath}", AcceptanceFixture.NuGetGlobalPackagesFolder.Path, failIfReturnValueIsNotZero: false);
462+
463+
// Should fail to compile because TestClass and TestMethod are not available without the implicit using
464+
compilationResult.AssertExitCodeIsNot(0);
465+
compilationResult.AssertOutputContains("TestClass");
466+
}
467+
468+
[TestMethod]
469+
public async Task ImplicitMSTestUsings_WhenEnabled_IncludesImplicitUsing()
470+
{
471+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
472+
AssetName,
473+
SingleTestSourceCode
474+
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)
475+
.PatchCodeWithReplace("$TargetFramework$", TargetFrameworks.NetCurrent)
476+
.PatchCodeWithReplace("$ExtraProperties$", "<ImplicitMSTestUsings>true</ImplicitMSTestUsings>")
477+
.PatchCodeWithReplace("using Microsoft.VisualStudio.TestTools.UnitTesting;", "// Explicit using removed to test implicit usings"));
478+
479+
// When ImplicitMSTestUsings is enabled, the compilation should succeed because Microsoft.VisualStudio.TestTools.UnitTesting should be implicitly available
480+
DotnetMuxerResult compilationResult = await DotnetCli.RunAsync($"build -c Debug {testAsset.TargetAssetPath}", AcceptanceFixture.NuGetGlobalPackagesFolder.Path);
481+
482+
compilationResult.AssertExitCodeIs(0);
483+
}
484+
485+
[TestMethod]
486+
public async Task ImplicitMSTestUsings_WhenEmpty_DefaultsToEnabled()
487+
{
488+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
489+
AssetName,
490+
SingleTestSourceCode
491+
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)
492+
.PatchCodeWithReplace("$TargetFramework$", TargetFrameworks.NetCurrent)
493+
.PatchCodeWithReplace("$ExtraProperties$", string.Empty) // No ImplicitMSTestUsings property set
494+
.PatchCodeWithReplace("using Microsoft.VisualStudio.TestTools.UnitTesting;", "// Explicit using removed to test implicit usings"));
495+
496+
// When ImplicitMSTestUsings is not set, it should default to enabled and compilation should succeed
497+
DotnetMuxerResult compilationResult = await DotnetCli.RunAsync($"build -c Debug {testAsset.TargetAssetPath}", AcceptanceFixture.NuGetGlobalPackagesFolder.Path);
498+
499+
compilationResult.AssertExitCodeIs(0);
500+
}
501+
502+
[TestMethod]
503+
public async Task ImplicitMSTestUsings_WhenSetToDisable_DoesNotIncludeImplicitUsing()
504+
{
505+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
506+
AssetName,
507+
SingleTestSourceCode
508+
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)
509+
.PatchCodeWithReplace("$TargetFramework$", TargetFrameworks.NetCurrent)
510+
.PatchCodeWithReplace("$ExtraProperties$", "<ImplicitMSTestUsings>disable</ImplicitMSTestUsings>")
511+
.PatchCodeWithReplace("using Microsoft.VisualStudio.TestTools.UnitTesting;", "// Explicit using removed to test implicit usings"));
512+
513+
// When ImplicitMSTestUsings is disabled, the compilation should fail because Microsoft.VisualStudio.TestTools.UnitTesting is not available
514+
DotnetMuxerResult compilationResult = await DotnetCli.RunAsync($"build -c Debug {testAsset.TargetAssetPath}", AcceptanceFixture.NuGetGlobalPackagesFolder.Path, failIfReturnValueIsNotZero: false);
515+
516+
// Should fail to compile because TestClass and TestMethod are not available without the implicit using
517+
compilationResult.AssertExitCodeIsNot(0);
518+
compilationResult.AssertOutputContains("TestClass");
519+
}
520+
521+
[TestMethod]
522+
public async Task ImplicitMSTestUsings_WhenSetToEnable_IncludesImplicitUsing()
523+
{
524+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
525+
AssetName,
526+
SingleTestSourceCode
527+
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)
528+
.PatchCodeWithReplace("$TargetFramework$", TargetFrameworks.NetCurrent)
529+
.PatchCodeWithReplace("$ExtraProperties$", "<ImplicitMSTestUsings>enable</ImplicitMSTestUsings>")
530+
.PatchCodeWithReplace("using Microsoft.VisualStudio.TestTools.UnitTesting;", "// Explicit using removed to test implicit usings"));
531+
532+
// When ImplicitMSTestUsings is set to 'enable', the compilation should succeed because Microsoft.VisualStudio.TestTools.UnitTesting should be implicitly available
533+
DotnetMuxerResult compilationResult = await DotnetCli.RunAsync($"build -c Debug {testAsset.TargetAssetPath}", AcceptanceFixture.NuGetGlobalPackagesFolder.Path);
534+
535+
compilationResult.AssertExitCodeIs(0);
536+
}
537+
449538
public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder)
450539
{
451540
public const string AspireProjectName = "AspireProject";

0 commit comments

Comments
 (0)