Skip to content

Commit b656ff1

Browse files
authored
[3.7] Don't report diagnostic for DeploymentItem on abstract class (#4539)
1 parent d4dba98 commit b656ff1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Analyzers/MSTest.Analyzers/UseDeploymentItemWithTestMethodOrTestClassAnalyzer.cs

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public override void Initialize(AnalysisContext context)
5252

5353
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testMethodAttributeSymbol, INamedTypeSymbol testClassAttributeSymbol, INamedTypeSymbol deploymentItemAttributeSymbol)
5454
{
55+
if (context.Symbol is INamedTypeSymbol { IsAbstract: true })
56+
{
57+
// As [DeploymentItem] attribute is inherited, it's okay to be present on an abstract class that is not a test class.
58+
// See https://github.com/microsoft/testfx/issues/2683 for information.
59+
// For now, we do the IsAbstract check specifically for classes and not methods.
60+
// If we got a convincing feedback around a false positive for the attribute on an abstract method, we can adjust the check.
61+
return;
62+
}
63+
5564
bool hasDeploymentItemAttribute = false;
5665
bool isTestMethodOrTestClass = false;
5766
foreach (AttributeData attribute in context.Symbol.GetAttributes())

test/UnitTests/MSTest.Analyzers.UnitTests/UseDeploymentItemWithTestMethodOrTestClassAnalyzerTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public void MyTestMethod()
8484
await VerifyCS.VerifyAnalyzerAsync(code);
8585
}
8686

87+
public async Task WhenAnAbstractClassHasDeploymentItem_NoDiagnostic()
88+
{
89+
string code = """
90+
using Microsoft.VisualStudio.TestTools.UnitTesting;
91+
92+
[DeploymentItem("")]
93+
public abstract class MyTestClass
94+
{
95+
}
96+
""";
97+
98+
await VerifyCS.VerifyAnalyzerAsync(code);
99+
}
100+
87101
public async Task WhenAClassHasDeploymentItem_Diagnostic()
88102
{
89103
string code = """

0 commit comments

Comments
 (0)