-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
91 additions
and
26 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
4 changes: 2 additions & 2 deletions
4
NUnit.Analyzers/TestFieldIsNotReadonly/TestFieldIsNotReadonlyConstants.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
50 changes: 50 additions & 0 deletions
50
NUnit.Analyzers/TestUsesSetupMethods/TestUsesSetupMethodsAnalyzer.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,50 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
using NUnit.Analyzers.Constants; | ||
using NUnit.Analyzers.Extensions; | ||
|
||
namespace NUnit.Analyzers.TestUsesSetupMethods | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class TestUsesSetupMethodsAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterSymbolAction(AnalyzeMethod, SymbolKind.Method); | ||
} | ||
|
||
private static void AnalyzeMethod(SymbolAnalysisContext context) | ||
{ | ||
var methodSymbol = (IMethodSymbol)context.Symbol; | ||
if (IsSetUpTearDownMethod(context.Compilation, methodSymbol)) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create(testUsesSetupMethods, methodSymbol.Locations[0])); | ||
} | ||
} | ||
|
||
private static bool IsSetUpTearDownMethod(Compilation compilation, IMethodSymbol methodSymbol) | ||
{ | ||
return methodSymbol.GetAttributes().Any(a => a.IsSetUpOrTearDownMethodAttribute(compilation)); | ||
} | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | ||
ImmutableArray.Create(testUsesSetupMethods); | ||
|
||
private static readonly DiagnosticDescriptor testUsesSetupMethods = new DiagnosticDescriptor( | ||
AnalyzerIdentifiers.TestUsesSetupAttributes, | ||
Check warning on line 40 in NUnit.Analyzers/TestUsesSetupMethods/TestUsesSetupMethodsAnalyzer.cs GitHub Actions / test
Check warning on line 40 in NUnit.Analyzers/TestUsesSetupMethods/TestUsesSetupMethodsAnalyzer.cs GitHub Actions / test
Check warning on line 40 in NUnit.Analyzers/TestUsesSetupMethods/TestUsesSetupMethodsAnalyzer.cs GitHub Actions / test
Check warning on line 40 in NUnit.Analyzers/TestUsesSetupMethods/TestUsesSetupMethodsAnalyzer.cs GitHub Actions / test
|
||
TestUsesSetupMethodsConstants.TestUsesSetupMethodsTitle, | ||
TestUsesSetupMethodsConstants.TestUsesSetupMethodsMessage, | ||
Categories.ParallelExecution, | ||
DiagnosticSeverity.Warning, | ||
true, | ||
TestUsesSetupMethodsConstants.TestUsesSetupMethodsDescription, | ||
TestUsesSetupMethodsConstants.TestUsesSetupMethodsUri | ||
); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
NUnit.Analyzers/TestUsesSetupMethods/TestUsesSetupMethodsConstants.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,10 @@ | ||
namespace NUnit.Analyzers.TestUsesSetupMethods | ||
{ | ||
internal class TestUsesSetupMethodsConstants | ||
{ | ||
internal const string TestUsesSetupMethodsTitle = "Test uses setup methods"; | ||
internal const string TestUsesSetupMethodsMessage = "Setup methods are considered harmful"; | ||
internal const string TestUsesSetupMethodsDescription = "If you require a similar object or state for your tests, prefer a helper method than using Setup and Teardown attributes."; | ||
internal const string TestUsesSetupMethodsUri = "https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices#prefer-helper-methods-to-setup-and-teardown"; | ||
} | ||
} |
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