Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
--------|----------|----------|-------
MSTEST0042 | Usage | Warning | DuplicateDataRowAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0042)
MSTEST0043 | `<Undetected>` | Warning | UseRetryWithTestMethodAnalyzer
Comment thread
Youssef1313 marked this conversation as resolved.
Resolved
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 @@ -47,4 +47,5 @@ internal static class DiagnosticIds
public const string AvoidUsingAssertsInAsyncVoidContextRuleId = "MSTEST0040";
public const string UseConditionBaseWithTestClassRuleId = "MSTEST0041";
public const string DuplicateDataRowRuleId = "MSTEST0042";
public const string UseRetryWithTestMethodRuleId = "MSTEST0043";
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal static class WellKnownTypeNames
public const string MicrosoftVisualStudioTestToolsUnitTestingOwnerAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.OwnerAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingParallelizeAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingPriorityAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.PriorityAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingRetryBaseAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.RetryBaseAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingStringAssert = "Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert";
public const string MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingTestCleanupAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute";
Expand Down
4 changes: 4 additions & 0 deletions src/Analyzers/MSTest.Analyzers/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#nullable enable
MSTest.Analyzers.DuplicateDataRowAnalyzer
MSTest.Analyzers.DuplicateDataRowAnalyzer.DuplicateDataRowAnalyzer() -> void
MSTest.Analyzers.UseRetryWithTestMethodAnalyzer
MSTest.Analyzers.UseRetryWithTestMethodAnalyzer.UseRetryWithTestMethodAnalyzer() -> void
override MSTest.Analyzers.DuplicateDataRowAnalyzer.Initialize(Microsoft.CodeAnalysis.Diagnostics.AnalysisContext! context) -> void
override MSTest.Analyzers.DuplicateDataRowAnalyzer.SupportedDiagnostics.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.DiagnosticDescriptor!>
override MSTest.Analyzers.UseRetryWithTestMethodAnalyzer.Initialize(Microsoft.CodeAnalysis.Diagnostics.AnalysisContext! context) -> void
override MSTest.Analyzers.UseRetryWithTestMethodAnalyzer.SupportedDiagnostics.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.DiagnosticDescriptor!>
18 changes: 18 additions & 0 deletions src/Analyzers/MSTest.Analyzers/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Analyzers/MSTest.Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,10 @@ The type declaring these methods should also respect the following rules:
<data name="DuplicateDataRowMessageFormat" xml:space="preserve">
<value>Do not duplicate 'DataRow' attributes. This is usually a copy/paste error. The attribute indices are '{0}' and '{1}'.</value>
</data>
<data name="UseRetryWithTestMethodTitle" xml:space="preserve">
<value>Use retry attribute on test method</value>
</data>
<data name="UseRetryWithTestMethodMessageFormat" xml:space="preserve">
<value>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</value>
</data>
</root>
79 changes: 79 additions & 0 deletions src/Analyzers/MSTest.Analyzers/UseRetryWithTestMethodAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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>
/// MSTEST0035: <inheritdoc cref="Resources.UseRetryWithTestMethodTitle"/>.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class UseRetryWithTestMethodAnalyzer : DiagnosticAnalyzer
{
private static readonly LocalizableResourceString Title = new(nameof(Resources.UseRetryWithTestMethodTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.UseRetryWithTestMethodMessageFormat), Resources.ResourceManager, typeof(Resources));

internal static readonly DiagnosticDescriptor UseRetryWithTestMethodRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseRetryWithTestMethodRuleId,
Title,
MessageFormat,
null,
Category.Usage,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
escalateToErrorInRecommended: true);

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

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

context.RegisterCompilationStartAction(context =>
{
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestMethodAttribute, out INamedTypeSymbol? testMethodAttributeSymbol) &&
context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingRetryBaseAttribute, out INamedTypeSymbol? retryBaseAttributeSymbol))
{
context.RegisterSymbolAction(
context => AnalyzeSymbol(context, testMethodAttributeSymbol, retryBaseAttributeSymbol),
SymbolKind.Method);
}
});
}

private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testMethodAttributeSymbol, INamedTypeSymbol retryBaseAttributeSymbol)
{
bool hasRetryBaseAttribute = false;
foreach (AttributeData attribute in context.Symbol.GetAttributes())
{
if (attribute.AttributeClass.Inherits(testMethodAttributeSymbol))
{
// We are looking for retry attributes that are not applied to test methods.
// If this is already a test method, we just return.
return;
}

if (attribute.AttributeClass.Inherits(retryBaseAttributeSymbol))
{
hasRetryBaseAttribute = true;
}
}

// We looked all attributes, and we found a retry attribute, but didn't find TestMethodAttribute.
if (hasRetryBaseAttribute)
{
context.ReportDiagnostic(context.Symbol.CreateDiagnostic(UseRetryWithTestMethodRule));
}
}
}
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ Typ deklarující tyto metody by měl také respektovat následující pravidla:
<target state="translated">Použít správné metody Assert</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,16 @@ Der Typ, der diese Methoden deklariert, sollte auch die folgenden Regeln beachte
<target state="translated">Geeignete Assert-Methoden verwenden</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ El tipo que declara estos métodos también debe respetar las reglas siguientes:
<target state="translated">Usar métodos "Assert" adecuados</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ Le type doit être une classe
<target state="translated">Utiliser les méthodes 'Assert' appropriées</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ Anche il tipo che dichiara questi metodi deve rispettare le regole seguenti:
<target state="translated">Usa metodi 'Assert' appropriati</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ The type declaring these methods should also respect the following rules:
<target state="translated">適切な 'Assert' メソッドを使用する</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ The type declaring these methods should also respect the following rules:
<target state="translated">적절한 'Assert' 메서드 사용</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ Typ deklarujący te metody powinien również przestrzegać następujących regu
<target state="translated">Użyj odpowiednich metod „Assert”</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ O tipo que declara esses métodos também deve respeitar as seguintes regras:
<target state="translated">Usar métodos 'Assert' adequados</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,16 @@ The type declaring these methods should also respect the following rules:
<target state="translated">Используйте правильные методы "Assert"</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,16 @@ Bu yöntemleri bildiren tipin ayrıca aşağıdaki kurallara uyması gerekir:
<target state="translated">Uygun 'Assert' yöntemlerini kullan</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ The type declaring these methods should also respect the following rules:
<target state="translated">使用正确的“断言”方法</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ The type declaring these methods should also respect the following rules:
<target state="translated">使用適當的 'Assert' 方法</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodMessageFormat">
<source>An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</source>
<target state="new">An attribute that derives from 'RetryBaseAttribute' can be specified only on a test method</target>
<note />
</trans-unit>
<trans-unit id="UseRetryWithTestMethodTitle">
<source>Use retry attribute on test method</source>
<target state="new">Use retry attribute on test method</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Loading