Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
MSTEST0084 | Usage | Info | OSPlatformAttributesShouldBeConsistentAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0084)
MSTEST0085 | Usage | Info | TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0085)
1 change: 1 addition & 0 deletions src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ internal static class DiagnosticIds
public const string InheritedMemberFromDifferentMSTestVersionRuleId = "MSTEST0082";
public const string UseExecutableConditionAttributeInsteadOfProcessCheckRuleId = "MSTEST0083";
public const string OSPlatformAttributesShouldBeConsistentRuleId = "MSTEST0084";
public const string TestClassAttributeShouldNotBeAppliedToAbstractClassRuleId = "MSTEST0085";
}
12 changes: 12 additions & 0 deletions src/Analyzers/MSTest.Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,18 @@ The type declaring these methods should also respect the following rules:
<data name="TestClassShouldBeValidTitle" xml:space="preserve">
<value>Test classes should have valid layout</value>
</data>
<data name="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription" xml:space="preserve">
<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>
<comment>{Locked="MSTest"}{Locked="[TestClass]"}</comment>
</data>
<data name="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat" xml:space="preserve">
<value>'[TestClass]' has no effect on abstract class '{0}'</value>
<comment>{0} is the class name. {Locked="[TestClass]"}</comment>
</data>
<data name="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle" xml:space="preserve">
<value>'[TestClass]' should not be applied to abstract classes</value>
<comment>{Locked="[TestClass]"}</comment>
</data>
<data name="TestClassShouldHaveTestMethodDescription" xml:space="preserve">
<value>Test class should have at least one test method or be 'static' with method(s) marked by '[AssemblyInitialize]' and/or '[AssemblyCleanup]'.</value>
<comment>{Locked="[AssemblyInitialize]"}{Locked="[AssemblyCleanup]"}{Locked="static"}{Locked="class"}</comment>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;

using Analyzer.Utilities.Extensions;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

using MSTest.Analyzers.Helpers;

namespace MSTest.Analyzers;

/// <summary>
/// MSTEST0085: <inheritdoc cref="Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassTitle"/>.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class TestClassAttributeShouldNotBeAppliedToAbstractClassAnalyzer : DiagnosticAnalyzer
{
private static readonly LocalizableResourceString Title = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString Description = new(nameof(Resources.TestClassAttributeShouldNotBeAppliedToAbstractClassDescription), Resources.ResourceManager, typeof(Resources));

/// <summary>
/// Gets the diagnostic descriptor reported by this analyzer.
/// </summary>
public static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.TestClassAttributeShouldNotBeAppliedToAbstractClassRuleId,
Title,
MessageFormat,
Description,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);

/// <inheritdoc />
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

/// <inheritdoc />
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();

context.RegisterCompilationStartAction(context =>
{
if (context.Compilation.TryGetOrCreateTypeByMetadataName(
WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute,
out INamedTypeSymbol? testClassAttributeSymbol))
{
context.RegisterSymbolAction(
context => AnalyzeSymbol(context, testClassAttributeSymbol),
SymbolKind.NamedType);
}
});
}

private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testClassAttributeSymbol)
{
var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;
if (namedTypeSymbol.TypeKind != TypeKind.Class
|| !namedTypeSymbol.IsAbstract
|| namedTypeSymbol.IsStatic)
{
return;
}

foreach (AttributeData attribute in namedTypeSymbol.GetAttributes())
{
if (attribute.AttributeClass.Inherits(testClassAttributeSymbol)
&& attribute.ApplicationSyntaxReference is { } syntaxReference)
{
context.ReportDiagnostic(syntaxReference.CreateDiagnostic(Rule, context.CancellationToken, namedTypeSymbol.Name));
}
}
}
}
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,21 @@ Typ deklarující tyto metody by měl také respektovat následující pravidla:
<target state="translated">Použijte Assert místo StringAssert</target>
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
<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>
<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>
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
<note>{0} is the class name. {Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
<source>'[TestClass]' should not be applied to abstract classes</source>
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
<note>{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassShouldBeValidMessageFormat">
<source>Test class '{0}' should be valid</source>
<target state="translated">Testovací class {0} by měla být platná.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,21 @@ Der Typ, der diese Methoden deklariert, sollte auch die folgenden Regeln beachte
<target state="translated">Verwenden Sie „Assert“ anstelle von „StringAssert“.</target>
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
<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>
<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>
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
<note>{0} is the class name. {Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
<source>'[TestClass]' should not be applied to abstract classes</source>
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
<note>{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassShouldBeValidMessageFormat">
<source>Test class '{0}' should be valid</source>
<target state="translated">Die Test class „{0}“ muss gültig sein</target>
Expand Down
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,21 @@ El tipo que declara estos métodos también debe respetar las reglas siguientes:
<target state="translated">Usar 'Assert' en lugar de 'StringAssert'</target>
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
<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>
<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>
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
<note>{0} is the class name. {Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
<source>'[TestClass]' should not be applied to abstract classes</source>
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
<note>{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassShouldBeValidMessageFormat">
<source>Test class '{0}' should be valid</source>
<target state="translated">La class de prueba '{0}' debe ser válida</target>
Expand Down
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,21 @@ Le type déclarant ces méthodes doit également respecter les règles suivantes
<target state="translated">Utilisez « Assert » au lieu de « StringAssert »</target>
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
<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>
<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>
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
<note>{0} is the class name. {Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
<source>'[TestClass]' should not be applied to abstract classes</source>
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
<note>{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassShouldBeValidMessageFormat">
<source>Test class '{0}' should be valid</source>
<target state="translated">La classe de test « {0} » doit être valide</target>
Expand Down
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,21 @@ Anche il tipo che dichiara questi metodi deve rispettare le regole seguenti:
<target state="translated">Usa 'Assert' invece di 'StringAssert'</target>
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
<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>
<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>
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
<note>{0} is the class name. {Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
<source>'[TestClass]' should not be applied to abstract classes</source>
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
<note>{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassShouldBeValidMessageFormat">
<source>Test class '{0}' should be valid</source>
<target state="translated">La classe di test '{0}' deve essere valida</target>
Expand Down
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,21 @@ The type declaring these methods should also respect the following rules:
<target state="translated">'StringAssert' の代わりに 'Assert' を使用</target>
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
<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>
<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>
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
<note>{0} is the class name. {Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
<source>'[TestClass]' should not be applied to abstract classes</source>
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
<note>{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassShouldBeValidMessageFormat">
<source>Test class '{0}' should be valid</source>
<target state="translated">テスト class '{0}' は有効である必要があります</target>
Expand Down
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,21 @@ The type declaring these methods should also respect the following rules:
<target state="translated">'StringAssert' 대신 'Assert' 사용</target>
<note>{Locked="StringAssert"}{Locked="Assert"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassDescription">
<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>
<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>
<note>{Locked="MSTest"}{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassMessageFormat">
<source>'[TestClass]' has no effect on abstract class '{0}'</source>
<target state="new">'[TestClass]' has no effect on abstract class '{0}'</target>
<note>{0} is the class name. {Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassAttributeShouldNotBeAppliedToAbstractClassTitle">
<source>'[TestClass]' should not be applied to abstract classes</source>
<target state="new">'[TestClass]' should not be applied to abstract classes</target>
<note>{Locked="[TestClass]"}</note>
</trans-unit>
<trans-unit id="TestClassShouldBeValidMessageFormat">
<source>Test class '{0}' should be valid</source>
<target state="translated">테스트 class '{0}'은(는) 유효해야 합니다.</target>
Expand Down
Loading