Skip to content

Commit e3293ab

Browse files
EvangelinkCopilot
andauthored
Add diagnostic for TestClass on abstract classes (#11240)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent dd93788 commit e3293ab

18 files changed

Lines changed: 425 additions & 0 deletions

‎src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
Rule ID | Category | Severity | Notes
77
--------|----------|----------|-------
88
MSTEST0084 | Usage | Info | OSPlatformAttributesShouldBeConsistentAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0084)
9+
MSTEST0085 | Usage | Info | TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0085)

‎src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ internal static class DiagnosticIds
8989
public const string InheritedMemberFromDifferentMSTestVersionRuleId = "MSTEST0082";
9090
public const string UseExecutableConditionAttributeInsteadOfProcessCheckRuleId = "MSTEST0083";
9191
public const string OSPlatformAttributesShouldBeConsistentRuleId = "MSTEST0084";
92+
public const string TestClassAttributeShouldNotBeAppliedToAbstractClassRuleId = "MSTEST0085";
9293
}

‎src/Analyzers/MSTest.Analyzers/Resources.resx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,18 @@ The type declaring these methods should also respect the following rules:
391391
<data name="TestClassShouldBeValidTitle" xml:space="preserve">
392392
<value>Test classes should have valid layout</value>
393393
</data>
394+
<data name="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription" xml:space="preserve">
395+
<value>MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</value>
396+
<comment>{Locked="MSTest"}{Locked="[TestClass]"}</comment>
397+
</data>
398+
<data name="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat" xml:space="preserve">
399+
<value>'[TestClass]' has no effect on abstract class '{0}'</value>
400+
<comment>{0} is the class name. {Locked="[TestClass]"}</comment>
401+
</data>
402+
<data name="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle" xml:space="preserve">
403+
<value>'[TestClass]' should not be applied to abstract classes</value>
404+
<comment>{Locked="[TestClass]"}</comment>
405+
</data>
394406
<data name="TestClassShouldHaveTestMethodDescription" xml:space="preserve">
395407
<value>Test class should have at least one test method or be 'static' with method(s) marked by '[AssemblyInitialize]' and/or '[AssemblyCleanup]'.</value>
396408
<comment>{Locked="[AssemblyInitialize]"}{Locked="[AssemblyCleanup]"}{Locked="static"}{Locked="class"}</comment>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Immutable;
5+
6+
using Analyzer.Utilities.Extensions;
7+
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.Diagnostics;
10+
11+
using MSTest.Analyzers.Helpers;
12+
13+
namespace MSTest.Analyzers;
14+
15+
/// <summary>
16+
/// MSTEST0085: <inheritdoc cref="Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassTitle"/>.
17+
/// </summary>
18+
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
19+
public sealed class TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer : DiagnosticAnalyzer
20+
{
21+
private static readonly LocalizableResourceString Title = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassTitle), Resources.ResourceManager, typeof(Resources));
22+
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat), Resources.ResourceManager, typeof(Resources));
23+
private static readonly LocalizableResourceString Description = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassDescription), Resources.ResourceManager, typeof(Resources));
24+
25+
/// <summary>
26+
/// Gets the diagnostic descriptor reported by this analyzer.
27+
/// </summary>
28+
public static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
29+
DiagnosticIds.TestClassAttributeShouldNotBeAppliedToAbstractClassRuleId,
30+
Title,
31+
MessageFormat,
32+
Description,
33+
Category.Usage,
34+
DiagnosticSeverity.Info,
35+
isEnabledByDefault: true);
36+
37+
/// <inheritdoc />
38+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
39+
40+
/// <inheritdoc />
41+
public override void Initialize(AnalysisContext context)
42+
{
43+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
44+
context.EnableConcurrentExecution();
45+
46+
context.RegisterCompilationStartAction(context =>
47+
{
48+
if (context.Compilation.TryGetOrCreateTypeByMetadataName(
49+
WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute,
50+
out INamedTypeSymbol? testClassAttributeSymbol))
51+
{
52+
context.RegisterSymbolAction(
53+
context => AnalyzeSymbol(context, testClassAttributeSymbol),
54+
SymbolKind.NamedType);
55+
}
56+
});
57+
}
58+
59+
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testClassAttributeSymbol)
60+
{
61+
var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;
62+
if (namedTypeSymbol.TypeKind != TypeKind.Class
63+
|| !namedTypeSymbol.IsAbstract
64+
|| namedTypeSymbol.IsStatic)
65+
{
66+
return;
67+
}
68+
69+
foreach (AttributeData attribute in namedTypeSymbol.GetAttributes())
70+
{
71+
if (attribute.AttributeClass.Inherits(testClassAttributeSymbol)
72+
&& attribute.ApplicationSyntaxReference is { } syntaxReference)
73+
{
74+
context.ReportDiagnostic(syntaxReference.CreateDiagnostic(Rule, context.CancellationToken, namedTypeSymbol.Name));
75+
}
76+
}
77+
}
78+
}

‎src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,21 @@ Typ deklarující tyto metody by měl také respektovat následující pravidla:
985985
<target state="translated">Použijte Assert místo StringAssert</target>
986986
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
987987
</trans-unit>
988+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
989+
<source>MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</source>
990+
<target state="new">MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</target>
991+
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
992+
</trans-unit>
993+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
994+
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
995+
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
996+
<note>{0} is the class name. {Locked="[TestClass]"}</note>
997+
</trans-unit>
998+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
999+
<source>'[TestClass]' should not be applied to abstract classes</source>
1000+
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
1001+
<note>{Locked="[TestClass]"}</note>
1002+
</trans-unit>
9881003
<trans-unit id="TestClassShouldBeValidMessageFormat">
9891004
<source>Test class '{0}' should be valid</source>
9901005
<target state="translated">Testovací class {0} by měla být platná.</target>

‎src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,21 @@ Der Typ, der diese Methoden deklariert, sollte auch die folgenden Regeln beachte
985985
<target state="translated">Verwenden Sie „Assert“ anstelle von „StringAssert“.</target>
986986
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
987987
</trans-unit>
988+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
989+
<source>MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</source>
990+
<target state="new">MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</target>
991+
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
992+
</trans-unit>
993+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
994+
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
995+
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
996+
<note>{0} is the class name. {Locked="[TestClass]"}</note>
997+
</trans-unit>
998+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
999+
<source>'[TestClass]' should not be applied to abstract classes</source>
1000+
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
1001+
<note>{Locked="[TestClass]"}</note>
1002+
</trans-unit>
9881003
<trans-unit id="TestClassShouldBeValidMessageFormat">
9891004
<source>Test class '{0}' should be valid</source>
9901005
<target state="translated">Die Test class „{0}“ muss gültig sein</target>

‎src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,21 @@ El tipo que declara estos métodos también debe respetar las reglas siguientes:
985985
<target state="translated">Usar 'Assert' en lugar de 'StringAssert'</target>
986986
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
987987
</trans-unit>
988+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
989+
<source>MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</source>
990+
<target state="new">MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</target>
991+
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
992+
</trans-unit>
993+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
994+
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
995+
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
996+
<note>{0} is the class name. {Locked="[TestClass]"}</note>
997+
</trans-unit>
998+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
999+
<source>'[TestClass]' should not be applied to abstract classes</source>
1000+
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
1001+
<note>{Locked="[TestClass]"}</note>
1002+
</trans-unit>
9881003
<trans-unit id="TestClassShouldBeValidMessageFormat">
9891004
<source>Test class '{0}' should be valid</source>
9901005
<target state="translated">La class de prueba '{0}' debe ser válida</target>

‎src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,21 @@ Le type déclarant ces méthodes doit également respecter les règles suivantes
985985
<target state="translated">Utilisez « Assert » au lieu de « StringAssert »</target>
986986
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
987987
</trans-unit>
988+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
989+
<source>MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</source>
990+
<target state="new">MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</target>
991+
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
992+
</trans-unit>
993+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
994+
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
995+
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
996+
<note>{0} is the class name. {Locked="[TestClass]"}</note>
997+
</trans-unit>
998+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
999+
<source>'[TestClass]' should not be applied to abstract classes</source>
1000+
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
1001+
<note>{Locked="[TestClass]"}</note>
1002+
</trans-unit>
9881003
<trans-unit id="TestClassShouldBeValidMessageFormat">
9891004
<source>Test class '{0}' should be valid</source>
9901005
<target state="translated">La classe de test « {0} » doit être valide</target>

‎src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,21 @@ Anche il tipo che dichiara questi metodi deve rispettare le regole seguenti:
985985
<target state="translated">Usa 'Assert' invece di 'StringAssert'</target>
986986
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
987987
</trans-unit>
988+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
989+
<source>MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</source>
990+
<target state="new">MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</target>
991+
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
992+
</trans-unit>
993+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
994+
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
995+
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
996+
<note>{0} is the class name. {Locked="[TestClass]"}</note>
997+
</trans-unit>
998+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
999+
<source>'[TestClass]' should not be applied to abstract classes</source>
1000+
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
1001+
<note>{Locked="[TestClass]"}</note>
1002+
</trans-unit>
9881003
<trans-unit id="TestClassShouldBeValidMessageFormat">
9891004
<source>Test class '{0}' should be valid</source>
9901005
<target state="translated">La classe di test '{0}' deve essere valida</target>

‎src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,21 @@ The type declaring these methods should also respect the following rules:
985985
<target state="translated">'StringAssert' の代わりに 'Assert' を使用</target>
986986
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
987987
</trans-unit>
988+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
989+
<source>MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</source>
990+
<target state="new">MSTest ignores '[TestClass]' attributes applied to abstract classes. Test methods declared by an abstract class are discovered through concrete derived test classes, which must declare their own '[TestClass]' attribute.</target>
991+
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
992+
</trans-unit>
993+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
994+
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
995+
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
996+
<note>{0} is the class name. {Locked="[TestClass]"}</note>
997+
</trans-unit>
998+
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
999+
<source>'[TestClass]' should not be applied to abstract classes</source>
1000+
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
1001+
<note>{Locked="[TestClass]"}</note>
1002+
</trans-unit>
9881003
<trans-unit id="TestClassShouldBeValidMessageFormat">
9891004
<source>Test class '{0}' should be valid</source>
9901005
<target state="translated">テスト class '{0}' は有効である必要があります</target>

0 commit comments

Comments
 (0)