-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #599 from manfred-brands/issue595_InstancePer
Do not suppress CA1001 if Fixture uses LifeCycle.InstancePerTestCase
- Loading branch information
Showing
12 changed files
with
169 additions
and
22 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
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
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
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
64 changes: 64 additions & 0 deletions
64
...alyzers/DiagnosticSuppressors/TypesThatOwnDisposableFieldsShouldBeDisposableSuppressor.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,64 @@ | ||
#if !NETSTANDARD1_6 | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Analyzers.Constants; | ||
using NUnit.Analyzers.Extensions; | ||
|
||
namespace NUnit.Analyzers.DiagnosticSuppressors | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class TypesThatOwnDisposableFieldsShouldBeDisposableSuppressor : DiagnosticSuppressor | ||
{ | ||
internal static readonly SuppressionDescriptor TypesThatOwnDisposableFieldsShouldHaveATearDown = new( | ||
id: AnalyzerIdentifiers.TypesThatOwnDisposableFieldsShouldBeDisposable, | ||
suppressedDiagnosticId: "CA1001", | ||
justification: "Field should be Disposed in TearDown or OneTimeTearDown method"); | ||
|
||
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions { get; } = | ||
ImmutableArray.Create(TypesThatOwnDisposableFieldsShouldHaveATearDown); | ||
|
||
public override void ReportSuppressions(SuppressionAnalysisContext context) | ||
{ | ||
foreach (var diagnostic in context.ReportedDiagnostics) | ||
{ | ||
SyntaxTree? sourceTree = diagnostic.Location.SourceTree; | ||
|
||
if (sourceTree is null) | ||
{ | ||
continue; | ||
} | ||
|
||
SyntaxNode node = sourceTree.GetRoot(context.CancellationToken) | ||
.FindNode(diagnostic.Location.SourceSpan); | ||
|
||
if (node is not ClassDeclarationSyntax classDeclaration) | ||
{ | ||
continue; | ||
} | ||
|
||
SemanticModel semanticModel = context.GetSemanticModel(sourceTree); | ||
INamedTypeSymbol? typeSymbol = (INamedTypeSymbol?)semanticModel.GetDeclaredSymbol(classDeclaration, context.CancellationToken); | ||
|
||
if (typeSymbol is not null) | ||
{ | ||
if (typeSymbol.IsInstancePerTestCaseFixture(context.Compilation)) | ||
{ | ||
// If a TestFixture used InstancePerTestCase, it should be IDisposable | ||
continue; | ||
} | ||
|
||
if (typeSymbol.IsTestFixture(context.Compilation)) | ||
{ | ||
context.ReportSuppression(Suppression.Create(TypesThatOwnDisposableFieldsShouldHaveATearDown, diagnostic)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
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