Skip to content

Commit

Permalink
implement test property on test method analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
mariam-abdulla committed Jan 29, 2024
1 parent 31d9695 commit 2728568
Show file tree
Hide file tree
Showing 20 changed files with 394 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
MSTEST0011 | Usage | Info | TestPropertyAttributeOnTestMethodAnalyzer
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 @@ -11,4 +11,5 @@ internal static class DiagnosticIds
public const string PublicTypeShouldBeTestClassRuleId = "MSTEST0004";
public const string TestContextShouldBeValidRuleId = "MSTEST0005";
public const string AvoidExpectedExceptionAttributeRuleId = "MSTEST0006";
public const string TestPropertyAttributeOnTestMethodRuleId = "MSTEST0011";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal static class WellKnownTypeNames
public const string MicrosoftVisualStudioTestToolsUnitTestingTestContext = "Microsoft.VisualStudio.TestTools.UnitTesting.TestContext";
public const string MicrosoftVisualStudioTestToolsUnitTestingTestMethodAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingExpectedExceptionAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingTestPropertyAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute";

public const string SystemThreadingTasksTask = "System.Threading.Tasks.Task";
public const string SystemThreadingTasksTask1 = "System.Threading.Tasks.Task`1";
Expand Down
27 changes: 27 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.

9 changes: 9 additions & 0 deletions src/Analyzers/MSTest.Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@
<data name="TestMethodShouldBeValidTitle" xml:space="preserve">
<value>Test methods should have valid layout</value>
</data>
<data name="TestPropertyAttributeOnTestMethodAnalyzerDescription" xml:space="preserve">
<value>[TestProperty] can only be set on methods marked with [TestMethod].</value>
</data>
<data name="TestPropertyAttributeOnTestMethodAnalyzerMessageFormat" xml:space="preserve">
<value>[TestProperty] can only be set on methods marked with [TestMethod]</value>
</data>
<data name="TestPropertyAttributeOnTestMethodAnalyzerTitle" xml:space="preserve">
<value>TestPropertyAttribute should be set on TestMethod</value>
</data>
<data name="UseParallelizeAttributeAnalyzerDescription" xml:space="preserve">
<value>By default, MSTest runs tests within the same assembly sequentially, which can lead to severe performance limitations. It is recommended to enable assembly attribute '[Parallelize]' to run tests in parallel, or if the assembly is known to not be parallelizable, to use explicitly the assembly level attribute '[DoNotParallelize]'.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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;

[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class TestPropertyAttributeOnTestMethodAnalyzer : DiagnosticAnalyzer
{
private static readonly LocalizableResourceString Title = new(nameof(Resources.TestPropertyAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString Description = new(nameof(Resources.TestPropertyAttributeOnTestMethodAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.TestPropertyAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));

internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.TestPropertyAttributeOnTestMethodRuleId,
Title,
MessageFormat,
Description,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);

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

public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();

context.RegisterCompilationStartAction(context =>
{
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestMethodAttribute, out var testMethodAttributeSymbol)
&& context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestPropertyAttribute, out var testPropertyAttributeSymbol))
{
context.RegisterSymbolAction(context => AnalyzeSymbol(context, testMethodAttributeSymbol, testPropertyAttributeSymbol), SymbolKind.Method);
}
});
}

private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testMethodAttributeSymbol,
INamedTypeSymbol testPropertyAttributeSymbol)
{
IMethodSymbol methodSymbol = (IMethodSymbol)context.Symbol;

AttributeData? testPropertyAttribute = null;
bool hasTestMethodAttribute = false;
foreach (var methodAttribute in methodSymbol.GetAttributes())
{
if (methodAttribute.AttributeClass.Inherits(testMethodAttributeSymbol))
{
hasTestMethodAttribute = true;
}

if (SymbolEqualityComparer.Default.Equals(methodAttribute?.AttributeClass, testPropertyAttributeSymbol))
{
testPropertyAttribute = methodAttribute;
}
}

if (testPropertyAttribute is not null && !hasTestMethodAttribute)
{
if (testPropertyAttribute.ApplicationSyntaxReference?.GetSyntax() is { } syntax)
{
context.ReportDiagnostic(syntax.CreateDiagnostic(Rule));
}
}
}
}
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 @@ -175,6 +175,21 @@
<target state="translated">Testovací metody by měly mít platné rozložení.</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerDescription">
<source>[TestProperty] can only be set on methods marked with [TestMethod].</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerMessageFormat">
<source>[TestProperty] can only be set on methods marked with [TestMethod]</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerTitle">
<source>TestPropertyAttribute should be set on TestMethod</source>
<target state="new">TestPropertyAttribute should be set on TestMethod</target>
<note />
</trans-unit>
<trans-unit id="UseParallelizeAttributeAnalyzerDescription">
<source>By default, MSTest runs tests within the same assembly sequentially, which can lead to severe performance limitations. It is recommended to enable assembly attribute '[Parallelize]' to run tests in parallel, or if the assembly is known to not be parallelizable, to use explicitly the assembly level attribute '[DoNotParallelize]'.</source>
<target state="translated">Ve výchozím nastavení spouští MSTest testy v rámci stejného sestavení sekvenčně, což může vést k závažným omezením výkonu. Doporučuje se povolit atribut sestavení [Parallelize] k paralelnímu spouštění testů nebo explicitně použít atribut [DoNotParallelize] na úrovni sestavení, pokud je známo, že sestavení není paralelizovatelné.</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 @@ -175,6 +175,21 @@
<target state="translated">Testmethoden müssen über ein gültiges Layout verfügen.</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerDescription">
<source>[TestProperty] can only be set on methods marked with [TestMethod].</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerMessageFormat">
<source>[TestProperty] can only be set on methods marked with [TestMethod]</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerTitle">
<source>TestPropertyAttribute should be set on TestMethod</source>
<target state="new">TestPropertyAttribute should be set on TestMethod</target>
<note />
</trans-unit>
<trans-unit id="UseParallelizeAttributeAnalyzerDescription">
<source>By default, MSTest runs tests within the same assembly sequentially, which can lead to severe performance limitations. It is recommended to enable assembly attribute '[Parallelize]' to run tests in parallel, or if the assembly is known to not be parallelizable, to use explicitly the assembly level attribute '[DoNotParallelize]'.</source>
<target state="translated">Standardmäßig führt MSTest Tests in derselben Assembly sequentiell aus, was zu erheblichen Leistungseinschränkungen führen kann. Es wird empfohlen, das Assembly-Attribut "[Parallelize]" zu aktivieren, um Tests parallel auszuführen, oder, wenn bekannt ist, dass die Assembly nicht parallelisierbar ist, explizit das Assembly-Attribut "[DoNotParallelize]" zu verwenden.</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 @@ -175,6 +175,21 @@
<target state="translated">Los métodos de prueba deben tener un diseño válido</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerDescription">
<source>[TestProperty] can only be set on methods marked with [TestMethod].</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerMessageFormat">
<source>[TestProperty] can only be set on methods marked with [TestMethod]</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerTitle">
<source>TestPropertyAttribute should be set on TestMethod</source>
<target state="new">TestPropertyAttribute should be set on TestMethod</target>
<note />
</trans-unit>
<trans-unit id="UseParallelizeAttributeAnalyzerDescription">
<source>By default, MSTest runs tests within the same assembly sequentially, which can lead to severe performance limitations. It is recommended to enable assembly attribute '[Parallelize]' to run tests in parallel, or if the assembly is known to not be parallelizable, to use explicitly the assembly level attribute '[DoNotParallelize]'.</source>
<target state="translated">De forma predeterminada, MSTest ejecuta pruebas en el mismo ensamblado secuencialmente, lo que puede provocar limitaciones de rendimiento graves. Se recomienda habilitar el atributo de ensamblado ''[Parallelize]'' o, si se sabe que el ensamblado no se puede paralelizar, usar explícitamente el atributo de nivel de ensamblado ''[DoNotParallelize]''.</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 @@ -175,6 +175,21 @@
<target state="translated">Les méthodes de test doivent avoir une disposition valide</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerDescription">
<source>[TestProperty] can only be set on methods marked with [TestMethod].</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerMessageFormat">
<source>[TestProperty] can only be set on methods marked with [TestMethod]</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerTitle">
<source>TestPropertyAttribute should be set on TestMethod</source>
<target state="new">TestPropertyAttribute should be set on TestMethod</target>
<note />
</trans-unit>
<trans-unit id="UseParallelizeAttributeAnalyzerDescription">
<source>By default, MSTest runs tests within the same assembly sequentially, which can lead to severe performance limitations. It is recommended to enable assembly attribute '[Parallelize]' to run tests in parallel, or if the assembly is known to not be parallelizable, to use explicitly the assembly level attribute '[DoNotParallelize]'.</source>
<target state="translated">Par défaut, MSTest exécute des tests dans la même assembly de façon séquentielle, ce qui peut entraîner de graves limitations de performances. Il est recommandé d’activer l’attribut d’assemblée « [Parallelize] » pour exécuter des tests en parallèle ou, si l’assemblée est connu pour ne pas être parallélisable, d’utiliser explicitement l’attribut de niveau assemblée « [DoNotParallelize] ».</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 @@ -175,6 +175,21 @@
<target state="translated">I metodi di test dovrebbero avere un layout valido</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerDescription">
<source>[TestProperty] can only be set on methods marked with [TestMethod].</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerMessageFormat">
<source>[TestProperty] can only be set on methods marked with [TestMethod]</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerTitle">
<source>TestPropertyAttribute should be set on TestMethod</source>
<target state="new">TestPropertyAttribute should be set on TestMethod</target>
<note />
</trans-unit>
<trans-unit id="UseParallelizeAttributeAnalyzerDescription">
<source>By default, MSTest runs tests within the same assembly sequentially, which can lead to severe performance limitations. It is recommended to enable assembly attribute '[Parallelize]' to run tests in parallel, or if the assembly is known to not be parallelizable, to use explicitly the assembly level attribute '[DoNotParallelize]'.</source>
<target state="translated">Per impostazione predefinita, MSTest esegue i test in sequenza nello stesso assemby, il che può causare gravi limitazioni delle prestazioni. È consigliabile abilitare l'attributo di assembly '[Parallelize]' per eseguire i test in parallelo, o, se l'assembly non è parallelizzabile, usare in modo esplicito l'attributo a livello di assembly '[DoNotParallelize]'.</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 @@ -176,6 +176,21 @@
<target state="translated">テスト メソッドには有効なレイアウトが必要です</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerDescription">
<source>[TestProperty] can only be set on methods marked with [TestMethod].</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerMessageFormat">
<source>[TestProperty] can only be set on methods marked with [TestMethod]</source>
<target state="new">[TestProperty] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="TestPropertyAttributeOnTestMethodAnalyzerTitle">
<source>TestPropertyAttribute should be set on TestMethod</source>
<target state="new">TestPropertyAttribute should be set on TestMethod</target>
<note />
</trans-unit>
<trans-unit id="UseParallelizeAttributeAnalyzerDescription">
<source>By default, MSTest runs tests within the same assembly sequentially, which can lead to severe performance limitations. It is recommended to enable assembly attribute '[Parallelize]' to run tests in parallel, or if the assembly is known to not be parallelizable, to use explicitly the assembly level attribute '[DoNotParallelize]'.</source>
<target state="translated">既定では、MSTest は同じアセンブリ内でテストを順番に実行するため、重大なパフォーマンス制限が生じる可能性があります。アセンブリ属性 '[Parallelize]' を有効にして並列でテストを実行するか、アセンブリが並列化できないことがわかっている場合は、アセンブリ レベル属性 '[DoNotParallelize]' を明示的に使用することをお勧めします。</target>
Expand Down
Loading

0 comments on commit 2728568

Please sign in to comment.