-
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.
MSTEST0005: TestContext should be valid (#2019)
- Loading branch information
1 parent
580a088
commit 78efbdc
Showing
27 changed files
with
1,028 additions
and
7 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
12 changes: 12 additions & 0 deletions
12
src/Analyzers/MSTest.Analyzers/Helpers/ApplicationStateGuard.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,12 @@ | ||
// 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.Runtime.CompilerServices; | ||
|
||
namespace MSTest.Analyzers.Helpers; | ||
|
||
internal static class ApplicationStateGuard | ||
{ | ||
public static InvalidOperationException Unreachable([CallerFilePath] string? path = null, [CallerLineNumber] int line = 0) | ||
=> new($"This program location is thought to be unreachable. File='{path}' Line={line}"); | ||
} |
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
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
151 changes: 151 additions & 0 deletions
151
src/Analyzers/MSTest.Analyzers/TestContextShouldBeValidAnalyzer.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,151 @@ | ||
// 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 TestContextShouldBeValidAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly LocalizableResourceString Title = new(nameof(Resources.TestContextShouldBeValidTitle), Resources.ResourceManager, typeof(Resources)); | ||
private static readonly LocalizableResourceString Description = new(nameof(Resources.TestContextShouldBeValidDescription), Resources.ResourceManager, typeof(Resources)); | ||
|
||
private static readonly LocalizableResourceString NotFieldMessageFormat = new(nameof(Resources.TestContextShouldBeValidMessageFormat_NotField), Resources.ResourceManager, typeof(Resources)); | ||
internal static readonly DiagnosticDescriptor NotFieldRule = new( | ||
DiagnosticIds.TestContextShouldBeValidRuleId, | ||
Title, | ||
NotFieldMessageFormat, | ||
Categories.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
Description, | ||
$"https://github.com/microsoft/testfx/blob/main/docs/analyzers/{DiagnosticIds.TestContextShouldBeValidRuleId}.md"); | ||
|
||
private static readonly LocalizableResourceString PublicMessageFormat = new(nameof(Resources.TestContextShouldBeValidMessageFormat_Public), Resources.ResourceManager, typeof(Resources)); | ||
internal static readonly DiagnosticDescriptor PublicRule = new( | ||
DiagnosticIds.TestContextShouldBeValidRuleId, | ||
Title, | ||
PublicMessageFormat, | ||
Categories.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
Description, | ||
$"https://github.com/microsoft/testfx/blob/main/docs/analyzers/{DiagnosticIds.TestContextShouldBeValidRuleId}.md"); | ||
|
||
private static readonly LocalizableResourceString PublicOrInternalMessageFormat = new(nameof(Resources.TestContextShouldBeValidMessageFormat_PublicOrInternal), Resources.ResourceManager, typeof(Resources)); | ||
internal static readonly DiagnosticDescriptor PublicOrInternalRule = new( | ||
DiagnosticIds.TestContextShouldBeValidRuleId, | ||
Title, | ||
PublicOrInternalMessageFormat, | ||
Categories.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
Description, | ||
$"https://github.com/microsoft/testfx/blob/main/docs/analyzers/{DiagnosticIds.TestContextShouldBeValidRuleId}.md"); | ||
|
||
private static readonly LocalizableResourceString NotStaticMessageFormat = new(nameof(Resources.TestContextShouldBeValidMessageFormat_NotStatic), Resources.ResourceManager, typeof(Resources)); | ||
internal static readonly DiagnosticDescriptor NotStaticRule = new( | ||
DiagnosticIds.TestContextShouldBeValidRuleId, | ||
Title, | ||
NotStaticMessageFormat, | ||
Categories.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
Description, | ||
$"https://github.com/microsoft/testfx/blob/main/docs/analyzers/{DiagnosticIds.TestContextShouldBeValidRuleId}.md"); | ||
|
||
private static readonly LocalizableResourceString NotReadonlyMessageFormat = new(nameof(Resources.TestContextShouldBeValidMessageFormat_NotReadonly), Resources.ResourceManager, typeof(Resources)); | ||
internal static readonly DiagnosticDescriptor NotReadonlyRule = new( | ||
DiagnosticIds.TestContextShouldBeValidRuleId, | ||
Title, | ||
NotReadonlyMessageFormat, | ||
Categories.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
Description, | ||
$"https://github.com/microsoft/testfx/blob/main/docs/analyzers/{DiagnosticIds.TestContextShouldBeValidRuleId}.md"); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
= ImmutableArray.Create(PublicRule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
|
||
context.RegisterCompilationStartAction(context => | ||
{ | ||
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestContext, out var testContextSymbol)) | ||
{ | ||
bool canDiscoverInternals = context.Compilation.CanDiscoverInternals(); | ||
context.RegisterSymbolAction(context => AnalyzeSymbol(context, testContextSymbol, canDiscoverInternals), SymbolKind.Field, SymbolKind.Property); | ||
} | ||
}); | ||
} | ||
|
||
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testContextSymbol, bool canDiscoverInternals) | ||
{ | ||
if (context.Symbol is IFieldSymbol fieldSymbol) | ||
{ | ||
AnalyzeFieldSymbol(context, fieldSymbol, testContextSymbol); | ||
return; | ||
} | ||
|
||
if (context.Symbol is IPropertySymbol propertySymbol) | ||
{ | ||
AnalyzePropertySymbol(context, testContextSymbol, canDiscoverInternals, propertySymbol); | ||
return; | ||
} | ||
|
||
throw ApplicationStateGuard.Unreachable(); | ||
} | ||
|
||
private static void AnalyzePropertySymbol(SymbolAnalysisContext context, INamedTypeSymbol testContextSymbol, bool canDiscoverInternals, IPropertySymbol propertySymbol) | ||
{ | ||
if (propertySymbol.GetMethod is null | ||
|| !string.Equals(propertySymbol.Name, "TestContext", StringComparison.OrdinalIgnoreCase) | ||
|| !SymbolEqualityComparer.Default.Equals(testContextSymbol, propertySymbol.GetMethod.ReturnType)) | ||
{ | ||
return; | ||
} | ||
|
||
if (propertySymbol.GetResultantVisibility() is { } resultantVisibility) | ||
{ | ||
if (!canDiscoverInternals && resultantVisibility != SymbolVisibility.Public) | ||
{ | ||
context.ReportDiagnostic(propertySymbol.CreateDiagnostic(PublicRule)); | ||
} | ||
else if (canDiscoverInternals && resultantVisibility == SymbolVisibility.Private) | ||
{ | ||
context.ReportDiagnostic(propertySymbol.CreateDiagnostic(PublicOrInternalRule)); | ||
} | ||
} | ||
|
||
if (propertySymbol.IsStatic) | ||
{ | ||
context.ReportDiagnostic(propertySymbol.CreateDiagnostic(NotStaticRule)); | ||
} | ||
|
||
if (propertySymbol.SetMethod is null) | ||
{ | ||
context.ReportDiagnostic(propertySymbol.CreateDiagnostic(NotReadonlyRule)); | ||
} | ||
} | ||
|
||
private static void AnalyzeFieldSymbol(SymbolAnalysisContext context, IFieldSymbol fieldSymbol, INamedTypeSymbol testContextSymbol) | ||
{ | ||
if (string.Equals(fieldSymbol.Name, "TestContext", StringComparison.OrdinalIgnoreCase) | ||
&& SymbolEqualityComparer.Default.Equals(testContextSymbol, fieldSymbol.Type)) | ||
{ | ||
context.ReportDiagnostic(fieldSymbol.CreateDiagnostic(NotFieldRule)); | ||
} | ||
} | ||
} |
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.