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

MSTEST0021: Prefer Dispose over TestCleanup methods #2585

Merged
merged 2 commits into from
Mar 17, 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 @@ -7,4 +7,5 @@ Rule ID | Category | Severity | Notes
MSTEST0017 | Usage | Info | AssertionArgsShouldBePassedInCorrectOrder, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0017)
MSTEST0019 | Design | Disabled | PreferTestInitializeOverConstructorAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0019)
MSTEST0020 | Design | Disabled | PreferConstructorOverTestInitializeAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0020)
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)
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 @@ -24,5 +24,6 @@ internal static class DiagnosticIds
public const string AssertionArgsShouldBePassedInCorrectOrderRuleId = "MSTEST0017";
public const string PreferTestInitializeOverConstructorRuleId = "MSTEST0019";
public const string PreferConstructorOverTestInitializeRuleId = "MSTEST0020";
public const string PreferDisposeOverTestCleanupRuleId = "MSTEST0021";
public const string PreferTestCleanupOverDisposeRuleId = "MSTEST0022";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 PreferDisposeOverTestCleanupAnalyzer : DiagnosticAnalyzer
{
private static readonly LocalizableResourceString Title = new(nameof(Resources.PreferDisposeOverTestCleanupTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.PreferDisposeOverTestCleanupMessageFormat), Resources.ResourceManager, typeof(Resources));

internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.PreferDisposeOverTestCleanupRuleId,
Title,
MessageFormat,
null,
Category.Design,
DiagnosticSeverity.Info,
isEnabledByDefault: false);

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.MicrosoftVisualStudioTestToolsUnitTestingTestCleanupAttribute, out var testCleanupAttributeSymbol))
{
var iasyncDisposableSymbol = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemIAsyncDisposable);
context.RegisterSymbolAction(context => AnalyzeSymbol(context, testCleanupAttributeSymbol, iasyncDisposableSymbol), SymbolKind.Method);
}
});
}

private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol testCleanupAttributeSymbol,
INamedTypeSymbol? iasyncDisposableSymbol)
{
IMethodSymbol methodSymbol = (IMethodSymbol)context.Symbol;

if (methodSymbol.IsTestCleanupMethod(testCleanupAttributeSymbol))
{
// We want to report only if the TestCleanup method returns void or if IAsyncDisposable is available.
if (iasyncDisposableSymbol is not null
|| methodSymbol.ReturnsVoid)
{
context.ReportDiagnostic(methodSymbol.CreateDiagnostic(Rule));
}
}
}
}
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="PreferConstructorOverTestInitializeTitle" xml:space="preserve">
<value>Prefer constructors over TestInitialize methods</value>
</data>
<data name="PreferDisposeOverTestCleanupMessageFormat" xml:space="preserve">
<value>Prefer 'Dispose' over TestCleanup methods</value>
</data>
<data name="PreferDisposeOverTestCleanupTitle" xml:space="preserve">
<value>Prefer 'Dispose' over TestCleanup methods</value>
</data>
<data name="PreferTestCleanupOverDisposeMessageFormat" xml:space="preserve">
<value>Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' 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">Prefer constructors over TestInitialize methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupMessageFormat">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferDisposeOverTestCleanupTitle">
<source>Prefer 'Dispose' over TestCleanup methods</source>
<target state="new">Prefer 'Dispose' over TestCleanup methods</target>
<note />
</trans-unit>
<trans-unit id="PreferTestCleanupOverDisposeMessageFormat">
<source>Prefer TestCleanup over 'Dispose' methods</source>
<target state="new">Prefer TestCleanup over 'Dispose' methods</target>
Expand Down
Loading
Loading