Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WorkItemAttribute analyzer #2180

Closed
Closed
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
2 changes: 1 addition & 1 deletion TestFx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MSTest.Performance.Runner",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Playground", "samples\Playground\Playground.csproj", "{8A41B37E-0732-4F28-B214-A44233B447FE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MSTest.Acceptance.IntegrationTests", "test\IntegrationTests\MSTest.Acceptance.IntegrationTests\MSTest.Acceptance.IntegrationTests.csproj", "{BCB42780-C559-40B6-8C4A-85EBC464AAA8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MSTest.Acceptance.IntegrationTests", "test\IntegrationTests\MSTest.Acceptance.IntegrationTests\MSTest.Acceptance.IntegrationTests.csproj", "{BCB42780-C559-40B6-8C4A-85EBC464AAA8}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it some automatic change?

EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
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
--------|----------|----------|-------
MSTEST0010 | Usage | Info | WorkItemAttributeOnTestMethodAnalyzer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this one 8 as it will be the first I merge (after #2176). Also, it seems that the rule is also not handling the notes part correctly so we will need to fix it by hand copying the rule description.

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 WorkItemAttributeOnTestMethodRuleId = "MSTEST0010";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public const string WorkItemAttributeOnTestMethodRuleId = "MSTEST0010";
public const string WorkItemAttributeOnTestMethodRuleId = "MSTEST0008";

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal static class WellKnownTypeNames
public const string MicrosoftVisualStudioTestToolsUnitTestingTestContext = "Microsoft.VisualStudio.TestTools.UnitTesting.TestContext";
public const string MicrosoftVisualStudioTestToolsUnitTestingTestInitializeAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingTestMethodAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute";
public const string MicrosoftVisualStudioTestToolsUnitTestingWorkItemAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.WorkItemAttribute";

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 @@ -227,4 +227,13 @@
<data name="UseParallelizeAttributeAnalyzerTitle" xml:space="preserve">
<value>Explicitly enable or disable tests parallelization</value>
</data>
<data name="WorkItemAttributeOnTestMethodAnalyzerDescription" xml:space="preserve">
<value>[WorkItem] can only be set on methods marked with [TestMethod].</value>
</data>
<data name="WorkItemAttributeOnTestMethodAnalyzerMessageFormat" xml:space="preserve">
<value>[WorkItem] can only be set on methods marked with [TestMethod]</value>
</data>
<data name="WorkItemAttributeOnTestMethodAnalyzerTitle" xml:space="preserve">
<value>WorkItemAttribute should be set on TestMethod</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking that maybe we want to go with Invalid usage of [WorkItem].

</data>
</root>
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)]
internal sealed class WorkItemAttributeOnTestMethodAnalyzer : DiagnosticAnalyzer
{
private static readonly LocalizableResourceString Title = new(nameof(Resources.WorkItemAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString Description = new(nameof(Resources.WorkItemAttributeOnTestMethodAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.WorkItemAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));

internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.WorkItemAttributeOnTestMethodRuleId,
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.MicrosoftVisualStudioTestToolsUnitTestingWorkItemAttribute, out var workItemAttributeSymbol))
{
context.RegisterSymbolAction(context => AnalyzeSymbol(context, testMethodAttributeSymbol, workItemAttributeSymbol), SymbolKind.Method);
}
});
}

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

AttributeData? workItemAttribute = null;
bool hasTestMethodAttribute = false;
foreach (var methodAttribute in methodSymbol.GetAttributes())
{
if (methodAttribute.AttributeClass.Inherits(testMethodAttributeSymbol))
{
hasTestMethodAttribute = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth adding a continue; here so we don't also do the next check.

}

if (SymbolEqualityComparer.Default.Equals(methodAttribute.AttributeClass, workItemAttributeSymbol))
{
workItemAttribute = methodAttribute;
}
}

if (workItemAttribute is not null && !hasTestMethodAttribute)
{
if (workItemAttribute.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 @@ -190,6 +190,21 @@
<target state="translated">Explicitně povolit nebo zakázat paralelizaci testů</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
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 @@ -190,6 +190,21 @@
<target state="translated">Parallelisierung von Tests explizit aktivieren oder deaktivieren</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
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 @@ -190,6 +190,21 @@
<target state="translated">Habilitar o deshabilitar explícitamente la paralelización de pruebas</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
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 @@ -190,6 +190,21 @@
<target state="translated">Activer ou désactiver explicitement la parallélisation des tests</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
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 @@ -190,6 +190,21 @@
<target state="translated">Abilitare o disabilitare in modo esplicito la parallelizzazione dei test</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
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 @@ -191,6 +191,21 @@
<target state="translated">テストの並列化を明示的に有効または無効にする</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
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 @@ -191,6 +191,21 @@
<target state="translated">테스트 병렬 처리를 명시적으로 사용하거나 사용하지 않도록 설정</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@
<target state="translated">Jawne włączanie lub wyłączanie równoległości testów</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@
<target state="translated">Habilitar ou desabilitar explicitamente a paralelização de testes</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
15 changes: 15 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@
<target state="translated">Явное включение или отключение параллелизации тестов</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerDescription">
<source>[WorkItem] can only be set on methods marked with [TestMethod].</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod].</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerMessageFormat">
<source>[WorkItem] can only be set on methods marked with [TestMethod]</source>
<target state="new">[WorkItem] can only be set on methods marked with [TestMethod]</target>
<note />
</trans-unit>
<trans-unit id="WorkItemAttributeOnTestMethodAnalyzerTitle">
<source>WorkItemAttribute should be set on TestMethod</source>
<target state="new">WorkItemAttribute should be set on TestMethod</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
Loading
Loading