|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | + |
| 6 | +using Analyzer.Utilities.Extensions; |
| 7 | + |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 10 | + |
| 11 | +using MSTest.Analyzers.Helpers; |
| 12 | + |
| 13 | +namespace MSTest.Analyzers; |
| 14 | + |
| 15 | +[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] |
| 16 | +public sealed class TestMethodShouldNotBeIgnoredAnalyzer : DiagnosticAnalyzer |
| 17 | +{ |
| 18 | + private static readonly LocalizableResourceString Title = new(nameof(Resources.TestMethodShouldNotBeIgnoredAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); |
| 19 | + private static readonly LocalizableResourceString Description = new(nameof(Resources.TestMethodShouldNotBeIgnoredAnalyzerDescription), Resources.ResourceManager, typeof(Resources)); |
| 20 | + private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.TestMethodShouldNotBeIgnoredAnalyzerFormat), Resources.ResourceManager, typeof(Resources)); |
| 21 | + |
| 22 | + internal static readonly DiagnosticDescriptor TestMethodShouldNotBeIgnoredRule = DiagnosticDescriptorHelper.Create( |
| 23 | + DiagnosticIds.TestMethodShouldNotBeIgnoredRuleId, |
| 24 | + Title, |
| 25 | + MessageFormat, |
| 26 | + Description, |
| 27 | + Category.Design, |
| 28 | + DiagnosticSeverity.Info, |
| 29 | + isEnabledByDefault: true); |
| 30 | + |
| 31 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } |
| 32 | + = ImmutableArray.Create(TestMethodShouldNotBeIgnoredRule); |
| 33 | + |
| 34 | + public override void Initialize(AnalysisContext context) |
| 35 | + { |
| 36 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 37 | + context.EnableConcurrentExecution(); |
| 38 | + context.RegisterCompilationStartAction(context => |
| 39 | + { |
| 40 | + if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingIgnoreAttribute, out var ignoreAttributeSymbol) |
| 41 | + && context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestMethodAttribute, out var testMethodAttributeSymbol)) |
| 42 | + { |
| 43 | + context.RegisterSymbolAction( |
| 44 | + context => AnalyzeSymbol(context, ignoreAttributeSymbol, testMethodAttributeSymbol), |
| 45 | + SymbolKind.Method); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol ignoreAttributeSymbol, INamedTypeSymbol testMethodAttributeSymbol) |
| 51 | + { |
| 52 | + var methodSymbol = (IMethodSymbol)context.Symbol; |
| 53 | + var methodAttributes = methodSymbol.GetAttributes(); |
| 54 | + bool isTestMethod = false; |
| 55 | + bool isMethodIgnored = false; |
| 56 | + foreach (var methodAttribute in methodAttributes) |
| 57 | + { |
| 58 | + // Current method should be a test method or should inherit from the TestMethod attribute. |
| 59 | + if (methodAttribute.AttributeClass.Inherits(testMethodAttributeSymbol)) |
| 60 | + { |
| 61 | + isTestMethod = true; |
| 62 | + } |
| 63 | + |
| 64 | + if (SymbolEqualityComparer.Default.Equals(methodAttribute.AttributeClass, ignoreAttributeSymbol)) |
| 65 | + { |
| 66 | + isMethodIgnored = true; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (!isTestMethod || !isMethodIgnored) |
| 71 | + { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + context.ReportDiagnostic(methodSymbol.CreateDiagnostic(TestMethodShouldNotBeIgnoredRule, methodSymbol.Name)); |
| 76 | + } |
| 77 | +} |
0 commit comments