Skip to content

Commit 35bab11

Browse files
Fix MSTEST0001 (parallelization analyzer) to work with VSTest by @Youssef1313 in #6480 (backport to rel/3.10) (#6486)
Co-authored-by: Youssef1313 <youssefvictor00@gmail.com>
1 parent 60e41b0 commit 35bab11

File tree

7 files changed

+77
-7
lines changed

7 files changed

+77
-7
lines changed

src/Adapter/MSTest.TestAdapter/buildTransitive/common/MSTest.TestAdapter.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<EnableMSTestV2CopyResources Condition=" '$(EnableMSTestV2CopyResources)' == '' ">true</EnableMSTestV2CopyResources>
5+
<IsMSTestTestAdapterReferenced>true</IsMSTestTestAdapterReferenced>
56
</PropertyGroup>
67

78
<ItemGroup>

src/Adapter/MSTest.TestAdapter/buildTransitive/common/MSTest.TestAdapter.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
<None Include="@(TestAdapterContent)" />
5252
</ItemGroup>
5353

54+
<ItemGroup>
55+
<CompilerVisibleProperty Include="IsMSTestTestAdapterReferenced" />
56+
</ItemGroup>
57+
5458
<Choose>
5559
<!-- Avoid false warning about missing reference (msbuild bug) -->
5660
<!-- https://github.com/dotnet/msbuild/issues/9698#issuecomment-1945763467 -->

src/Adapter/MSTest.TestAdapter/buildTransitive/uwp/MSTest.TestAdapter.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<EnableMSTestV2CopyResources Condition=" '$(EnableMSTestV2CopyResources)' == '' ">true</EnableMSTestV2CopyResources>
5+
<IsMSTestTestAdapterReferenced>true</IsMSTestTestAdapterReferenced>
56
</PropertyGroup>
67

78
</Project>

src/Adapter/MSTest.TestAdapter/buildTransitive/uwp/MSTest.TestAdapter.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<None Include="@(TestAdapterContent)" />
2828
</ItemGroup>
2929

30+
<ItemGroup>
31+
<CompilerVisibleProperty Include="IsMSTestTestAdapterReferenced" />
32+
</ItemGroup>
33+
3034
<Target Name="GetMSTestV2CultureHierarchy">
3135
<!--
3236
Only traversing 5 levels in the culture hierarchy. This is the maximum length for all cultures and should be sufficient

src/Analyzers/MSTest.Analyzers/UseParallelizeAttributeAnalyzer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public override void Initialize(AnalysisContext context)
4545

4646
private static void AnalyzeCompilation(CompilationAnalysisContext context)
4747
{
48-
bool hasTestAdapter = context.Compilation.ReferencedAssemblyNames.Any(asm => asm.Name == "Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter");
48+
bool hasTestAdapter = context.Options.AnalyzerConfigOptionsProvider.GlobalOptions.TryGetValue("build_property.IsMSTestTestAdapterReferenced", out string? isAdapterReferenced) &&
49+
bool.TryParse(isAdapterReferenced, out bool isAdapterReferencedValue) &&
50+
isAdapterReferencedValue;
51+
4952
if (!hasTestAdapter)
5053
{
5154
// We shouldn't produce a diagnostic if only the test framework is referenced, but not the adapter.

test/IntegrationTests/MSTest.Acceptance.IntegrationTests/AnalyzersTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,62 @@ namespace MSTest.Acceptance.IntegrationTests;
99
[TestClass]
1010
public sealed class AnalyzersTests : AcceptanceTestBase<NopAssetFixture>
1111
{
12+
[TestMethod]
13+
[DataRow(false, false)]
14+
[DataRow(false, true)]
15+
[DataRow(true, false)]
16+
[DataRow(true, true)]
17+
public async Task MSTEST0001(bool isMTP, bool isAdapterReferenced)
18+
{
19+
string code = $$"""
20+
#file TestForMSTEST0001.csproj
21+
<Project Sdk="Microsoft.NET.Sdk">
22+
23+
<PropertyGroup>
24+
<TargetFramework>$TargetFrameworks$</TargetFramework>
25+
<RunAnalyzers>true</RunAnalyzers>
26+
<MSTestAnalysisMode>all</MSTestAnalysisMode>
27+
28+
{{(isMTP ? """
29+
<EnableMSTestRunner>true</EnableMSTestRunner>
30+
<OutputType>Exe</OutputType>
31+
""" : string.Empty)}}
32+
</PropertyGroup>
33+
34+
<ItemGroup>
35+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$MicrosoftNETTestSdkVersion$" />
36+
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
37+
{{(isAdapterReferenced ? "<PackageReference Include=\"MSTest.TestAdapter\" Version=\"$MSTestVersion$\" />" : string.Empty)}}
38+
</ItemGroup>
39+
</Project>
40+
41+
#file UnitTest1.cs
42+
using Microsoft.VisualStudio.TestTools.UnitTesting;
43+
44+
[TestClass]
45+
public class UnitTest1
46+
{
47+
[TestMethod]
48+
public void TestMethod()
49+
{
50+
}
51+
}
52+
""".PatchTargetFrameworks(TargetFrameworks.NetCurrent)
53+
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)
54+
.PatchCodeWithReplace("$MicrosoftNETTestSdkVersion$", MicrosoftNETTestSdkVersion);
55+
56+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync("TestForMSTEST0001", code);
57+
DotnetMuxerResult result = await DotnetCli.RunAsync($"build {testAsset.TargetAssetPath}", AcceptanceFixture.NuGetGlobalPackagesFolder.Path, warnAsError: false);
58+
if (isAdapterReferenced)
59+
{
60+
result.AssertOutputContains("warning MSTEST0001");
61+
}
62+
else
63+
{
64+
result.AssertOutputDoesNotContain("warning MSTEST0001");
65+
}
66+
}
67+
1268
[TestMethod]
1369
public async Task AnalyzersShouldBeEnabledWhenUsingMetapackage()
1470
{

test/UnitTests/MSTest.Analyzers.UnitTests/UseParallelizeAttributeAnalyzerTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using Microsoft.CodeAnalysis;
54
using Microsoft.CodeAnalysis.Testing;
6-
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
75

86
using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
97
MSTest.Analyzers.UseParallelizeAttributeAnalyzer,
@@ -23,10 +21,13 @@ private static async Task VerifyAsync(string code, bool includeTestAdapter, para
2321

2422
if (includeTestAdapter)
2523
{
26-
// NOTE: Test constructor already adds TestFramework refs.
27-
#pragma warning disable CS0618 // Type or member is obsolete
28-
test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(MSTestExecutor).Assembly.Location));
29-
#pragma warning restore CS0618 // Type or member is obsolete
24+
test.TestState.AnalyzerConfigFiles.Add((
25+
"/.globalconfig",
26+
"""
27+
is_global = true
28+
29+
build_property.IsMSTestTestAdapterReferenced = true
30+
"""));
3031
}
3132

3233
test.ExpectedDiagnostics.AddRange(expected);

0 commit comments

Comments
 (0)