-
Notifications
You must be signed in to change notification settings - Fork 261
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am thinking that maybe we want to go with |
||
</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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably worth adding a |
||
} | ||
|
||
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)); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?