Skip to content
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

MSTEST0024: Do not store TestContext in static members #2597

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ MSTEST0020 | Design | Disabled | PreferConstructorOverTestInitializeAnalyzer, [D
MSTEST0021 | Design | Disabled | PreferDisposeOverTestCleanupAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0021)
MSTEST0022 | Design | Disabled | PreferTestCleanupOverDisposeAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0022)
MSTEST0023 | Usage | Info | DoNotNegateBooleanAssertionAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0023)
MSTEST0024 | Usage | Info | DoNotStoreStaticTestContextAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0024)
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 Microsoft.CodeAnalysis.Operations;

using MSTest.Analyzers.Helpers;

namespace MSTest.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class DoNotStoreStaticTestContextAnalyzer : DiagnosticAnalyzer
{
private static readonly LocalizableResourceString Title = new(nameof(Resources.DoNotStoreStaticTestContextAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.DoNotStoreStaticTestContextAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));

internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.DoNotStoreStaticTestContextAnalyzerRuleId,
Title,
MessageFormat,
null,
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.MicrosoftVisualStudioTestToolsUnitTestingTestContext, out var testContextSymbol))
{
context.RegisterOperationAction(context => AnalyzeOperation(context, testContextSymbol), OperationKind.SimpleAssignment);
}
});
}

private static void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol testContextSymbol)
{
ISimpleAssignmentOperation assignmentOperation = (ISimpleAssignmentOperation)context.Operation;

if (assignmentOperation.Target is IMemberReferenceOperation memberReferenceOperation
&& memberReferenceOperation.Instance is null
&& assignmentOperation.Value is IParameterReferenceOperation parameterReferenceOperation
&& SymbolEqualityComparer.Default.Equals(parameterReferenceOperation.Type, testContextSymbol))
{
context.ReportDiagnostic(assignmentOperation.CreateDiagnostic(Rule));
}
}
}
1 change: 1 addition & 0 deletions src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ internal static class DiagnosticIds
public const string PreferDisposeOverTestCleanupRuleId = "MSTEST0021";
public const string PreferTestCleanupOverDisposeRuleId = "MSTEST0022";
public const string DoNotNegateBooleanAssertionRuleId = "MSTEST0023";
public const string DoNotStoreStaticTestContextAnalyzerRuleId = "MSTEST0024";
}
18 changes: 18 additions & 0 deletions src/Analyzers/MSTest.Analyzers/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Analyzers/MSTest.Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@
<data name="DoNotNegateBooleanAssertionTitle" xml:space="preserve">
<value>Do not negate boolean assertions</value>
</data>
<data name="DoNotStoreStaticTestContextAnalyzerMessageFormat" xml:space="preserve">
<value>Do not store TestContext in a static member</value>
</data>
<data name="DoNotStoreStaticTestContextAnalyzerTitle" xml:space="preserve">
<value>Do not store TestContext in a static member</value>
</data>
<data name="PreferConstructorOverTestInitializeMessageFormat" xml:space="preserve">
<value>Prefer constructors over TestInitialize methods</value>
</data>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
10 changes: 10 additions & 0 deletions src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
<target state="new">Do not negate boolean assertions</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerMessageFormat">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="DoNotStoreStaticTestContextAnalyzerTitle">
<source>Do not store TestContext in a static member</source>
<target state="new">Do not store TestContext in a static member</target>
<note />
</trans-unit>
<trans-unit id="PreferConstructorOverTestInitializeMessageFormat">
<source>Prefer constructors over TestInitialize methods</source>
<target state="new">Prefer constructors over TestInitialize methods</target>
Expand Down
Loading
Loading