-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers; | ||
|
||
internal static class AttributeHelpers | ||
{ | ||
public static bool IsIgnored(ICustomAttributeProvider type, out string? ignoreMessage) | ||
{ | ||
IEnumerable<ConditionalTestBaseAttribute> attributes = ReflectHelper.Instance.GetDerivedAttributes<ConditionalTestBaseAttribute>(type, inherit: false); | ||
foreach (ConditionalTestBaseAttribute attribute in attributes) | ||
{ | ||
if (attribute.ShouldIgnore) | ||
{ | ||
ignoreMessage = attribute.ConditionalIgnoreMessage; | ||
return true; | ||
} | ||
} | ||
|
||
ignoreMessage = null; | ||
return false; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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; | ||
|
||
/// <summary> | ||
/// MSTEST0041: <inheritdoc cref="Resources.UseConditionalTestBaseWithTestClassTitle"/>. | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
public sealed class UseConditionalTestBaseWithTestClassAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly LocalizableResourceString Title = new(nameof(Resources.UseConditionalTestBaseWithTestClassTitle), Resources.ResourceManager, typeof(Resources)); | ||
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.UseConditionalTestBaseWithTestClassMessageFormat), Resources.ResourceManager, typeof(Resources)); | ||
|
||
internal static readonly DiagnosticDescriptor UseConditionalTestBaseWithTestClassRule = DiagnosticDescriptorHelper.Create( | ||
DiagnosticIds.UseConditionalTestBaseWithTestClassRuleId, | ||
Title, | ||
MessageFormat, | ||
null, | ||
Category.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
= ImmutableArray.Create(UseConditionalTestBaseWithTestClassRule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
|
||
context.RegisterCompilationStartAction(context => | ||
{ | ||
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute, out INamedTypeSymbol? testClassAttributeSymbol) && | ||
context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingConditionalTestBaseAttribute, out INamedTypeSymbol? conditionalTestBaseAttributeSymbol)) | ||
{ | ||
context.RegisterSymbolAction( | ||
context => AnalyzeSymbol(context, testClassAttributeSymbol, conditionalTestBaseAttributeSymbol), | ||
SymbolKind.NamedType); | ||
} | ||
}); | ||
} | ||
|
||
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testClassAttributeSymbol, INamedTypeSymbol conditionalTestBaseAttributeSymbol) | ||
{ | ||
INamedTypeSymbol? conditionalTestBaseAttribute = null; | ||
bool isTestClass = false; | ||
foreach (AttributeData attribute in context.Symbol.GetAttributes()) | ||
{ | ||
if (attribute.AttributeClass.Inherits(testClassAttributeSymbol)) | ||
{ | ||
isTestClass = true; | ||
} | ||
else if (attribute.AttributeClass.Inherits(conditionalTestBaseAttributeSymbol)) | ||
{ | ||
conditionalTestBaseAttribute = attribute.AttributeClass; | ||
} | ||
} | ||
|
||
if (conditionalTestBaseAttribute is not null && !isTestClass) | ||
{ | ||
context.ReportDiagnostic(context.Symbol.CreateDiagnostic(UseConditionalTestBaseWithTestClassRule, conditionalTestBaseAttribute.Name)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
/// <summary> | ||
/// This attribute is used to ignore a test class or a test method, based on a condition and using an optional message. | ||
/// </summary> | ||
/// <remarks> | ||
/// This attribute isn't inherited. Applying it to a base class will not cause derived classes to be ignored. | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false)] | ||
public abstract class ConditionalTestBaseAttribute : Attribute | ||
Check failure on line 13 in src/TestFramework/TestFramework/Attributes/TestMethod/ConditionalTestBaseAttribute.cs
|
||
{ | ||
/// <summary> | ||
/// Gets the ignore message (in case <see cref="ShouldIgnore"/> returns <see langword="true"/>) indicating | ||
/// the reason for ignoring the test method or test class. | ||
/// </summary> | ||
public abstract string? ConditionalIgnoreMessage { get; } | ||
Check failure on line 19 in src/TestFramework/TestFramework/Attributes/TestMethod/ConditionalTestBaseAttribute.cs
|
||
|
||
/// <summary> | ||
/// Gets a value indicating whether the test method or test class should be ignored. | ||
/// </summary> | ||
public abstract bool ShouldIgnore { get; } | ||
Check failure on line 24 in src/TestFramework/TestFramework/Attributes/TestMethod/ConditionalTestBaseAttribute.cs
|
||
} |