Skip to content
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
14 changes: 14 additions & 0 deletions docs/Rules/MA0137.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ Task FooAsync() => Task.CompletedTask;
// non-compliant
Task Foo() => Task.CompletedTask;
````

# Configuration

By default, test methods (methods decorated with test framework attributes such as `[Fact]`, `[Test]`, `[TestMethod]`, etc.) are excluded from this rule. You can change this behavior using the `.editorconfig` file:

````editorconfig
MA0137.exclude_test_methods = true # default value
````

Set to `false` to also report diagnostics on test methods:

````editorconfig
MA0137.exclude_test_methods = false
````
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using Meziantou.Analyzer.Configurations;
using Meziantou.Analyzer.Internals;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void AnalyzeSymbol(SymbolAnalysisContext context)
if (method.IsEqualTo(context.Compilation.GetEntryPoint(context.CancellationToken)))
return;

if (MustIgnoreSymbol(method))
if (MustIgnoreSymbol(context.Options, method))
return;

var hasAsyncSuffix = method.Name.EndsWith("Async", StringComparison.Ordinal);
Expand Down Expand Up @@ -145,12 +146,13 @@ public void AnalyzeLocalFunction(OperationAnalysisContext context)
}
}

private bool MustIgnoreSymbol(IMethodSymbol symbol)
private bool MustIgnoreSymbol(AnalyzerOptions options, IMethodSymbol symbol)
{
if (symbol.HasAttribute(_benchmarkSymbol))
return true;

if (symbol.IsUnitTestMethod())
var excludeTestMethods = options.GetConfigurationValue(symbol, "MA0137.exclude_test_methods", defaultValue: true);
if (excludeTestMethods && symbol.IsUnitTestMethod())
return true;

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,32 @@ class TypeName
""")
.AddXUnitApi()
.ValidateAsync();

[Fact]
public Task IgnoreTestMethods_ExcludeTestMethodsTrue()
=> CreateProjectBuilder()
.WithSourceCode("""
class TypeName
{
[Xunit.Fact]
System.Threading.Tasks.Task Foo() => throw null;
}
""")
.AddXUnitApi()
.AddAnalyzerConfiguration("MA0137.exclude_test_methods", "true")
.ValidateAsync();

[Fact]
public Task IgnoreTestMethods_ExcludeTestMethodsFalse()
=> CreateProjectBuilder()
.WithSourceCode("""
class TypeName
{
[Xunit.Fact]
System.Threading.Tasks.Task {|MA0137:Foo|}() => throw null;
}
""")
.AddXUnitApi()
.AddAnalyzerConfiguration("MA0137.exclude_test_methods", "false")
.ValidateAsync();
}
Loading