-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MSTEST0007: [Owner] can only be set on methods marked with [TestMethod]
- Loading branch information
1 parent
54093ba
commit 89e1728
Showing
20 changed files
with
392 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
--------|----------|----------|------- | ||
MSTEST0007 | Usage | Info | OwnerAttributeShouldBeSetOnlyOnTestMethodAnalyzer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Analyzers/MSTest.Analyzers/OwnerAttributeOnTestMethodAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 OwnerAttributeOnTestMethodAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly LocalizableResourceString Title = new(nameof(Resources.OwnerAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); | ||
private static readonly LocalizableResourceString Description = new(nameof(Resources.OwnerAttributeOnTestMethodAnalyzerDescription), Resources.ResourceManager, typeof(Resources)); | ||
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.OwnerAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); | ||
|
||
internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( | ||
DiagnosticIds.OwnerAttributeOnTestMethodRuleId, | ||
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.MicrosoftVisualStudioTestToolsUnitTestingOwnerAttribute, out var ownerAttributeSymbol)) | ||
{ | ||
context.RegisterSymbolAction(context => AnalyzeSymbol(context, testMethodAttributeSymbol, ownerAttributeSymbol), SymbolKind.Method); | ||
} | ||
}); | ||
} | ||
|
||
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testMethodAttributeSymbol, | ||
INamedTypeSymbol ownerAttributeSymbol) | ||
{ | ||
IMethodSymbol methodSymbol = (IMethodSymbol)context.Symbol; | ||
|
||
AttributeData? ownerAttribute = null; | ||
bool hasTestMethodAttribute = false; | ||
foreach (var methodAttribute in methodSymbol.GetAttributes()) | ||
{ | ||
if (methodAttribute.AttributeClass.Inherits(testMethodAttributeSymbol)) | ||
{ | ||
hasTestMethodAttribute = true; | ||
} | ||
|
||
if (SymbolEqualityComparer.Default.Equals(methodAttribute.AttributeClass, ownerAttributeSymbol)) | ||
{ | ||
ownerAttribute = methodAttribute; | ||
} | ||
} | ||
|
||
if (ownerAttribute is not null && !hasTestMethodAttribute) | ||
{ | ||
if (ownerAttribute.ApplicationSyntaxReference?.GetSyntax() is { } syntax) | ||
{ | ||
context.ReportDiagnostic(syntax.CreateDiagnostic(Rule)); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.