-
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.
add deep inheritance chain for tests analyzer
- Loading branch information
Showing
4 changed files
with
80 additions
and
1 deletion.
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
60 changes: 60 additions & 0 deletions
60
NUnit.Analyzers/TestDeepInheritanceChain/TestDeepInheritanceChainAnalyzer.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,60 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
using SkbKontur.NUnit.Analyzers.Constants; | ||
using SkbKontur.NUnit.Analyzers.Extensions; | ||
|
||
namespace SkbKontur.NUnit.Analyzers.TestDeepInheritanceChain | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class TestDeepInheritanceChainAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterSymbolAction(AnalyzeType, SymbolKind.NamedType); | ||
} | ||
|
||
private static void AnalyzeType(SymbolAnalysisContext context) | ||
{ | ||
var typeSymbol = (INamedTypeSymbol)context.Symbol; | ||
|
||
var hasTests = typeSymbol | ||
.GetMembers() | ||
.OfType<IMethodSymbol>() | ||
.Any(x => x.MethodKind == MethodKind.Ordinary && x.IsTestRelatedMethod(context.Compilation)); | ||
|
||
if (!hasTests) | ||
return; | ||
|
||
var testBase = typeSymbol.BaseType?.BaseType; | ||
if (testBase != null && !knownRoots.Any(r => testBase.IsType(r, context.Compilation))) | ||
context.ReportDiagnostic(Diagnostic.Create(testDeepInheritanceChain, typeSymbol.Locations[0])); | ||
} | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | ||
ImmutableArray.Create(testDeepInheritanceChain); | ||
|
||
private static readonly DiagnosticDescriptor testDeepInheritanceChain = new DiagnosticDescriptor( | ||
AnalyzerIdentifiers.TestDeepInheritanceChain, | ||
Check warning on line 43 in NUnit.Analyzers/TestDeepInheritanceChain/TestDeepInheritanceChainAnalyzer.cs GitHub Actions / test
Check warning on line 43 in NUnit.Analyzers/TestDeepInheritanceChain/TestDeepInheritanceChainAnalyzer.cs GitHub Actions / test
Check warning on line 43 in NUnit.Analyzers/TestDeepInheritanceChain/TestDeepInheritanceChainAnalyzer.cs GitHub Actions / test
Check warning on line 43 in NUnit.Analyzers/TestDeepInheritanceChain/TestDeepInheritanceChainAnalyzer.cs GitHub Actions / test
|
||
TestDeepInheritanceChainConstants.TestDeepInheritanceChainTitle, | ||
TestDeepInheritanceChainConstants.TestDeepInheritanceChainMessage, | ||
Categories.ParallelExecution, | ||
DiagnosticSeverity.Warning, | ||
true, | ||
TestDeepInheritanceChainConstants.TestDeepInheritanceChainDescription, | ||
TestDeepInheritanceChainConstants.TestDeepInheritanceChainUri | ||
); | ||
|
||
private static readonly string[] knownRoots = | ||
{ | ||
"System.Object", | ||
"SkbKontur.NUnit.Middlewares.SimpleTestBase", | ||
"SkbKontur.NUnit.Middlewares.SimpleSuiteBase", | ||
}; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
NUnit.Analyzers/TestDeepInheritanceChain/TestDeepInheritanceChainConstants.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 SkbKontur.NUnit.Analyzers.TestDeepInheritanceChain | ||
{ | ||
internal static class TestDeepInheritanceChainConstants | ||
{ | ||
internal const string TestDeepInheritanceChainTitle = "Test base inheritance chain is too deep"; | ||
internal const string TestDeepInheritanceChainMessage = "Test base inheritance chain is too deep"; | ||
internal const string TestDeepInheritanceChainDescription = "Test fixture class should have only one base class for clarity."; | ||
internal const string TestDeepInheritanceChainUri = "https://en.wikipedia.org/wiki/Composition_over_inheritance"; | ||
} | ||
} |
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