Skip to content

Commit

Permalink
xUnit1030 should not trigger inside lambda (#177)
Browse files Browse the repository at this point in the history
Co-authored-by: Brad Wilson <[email protected]>
  • Loading branch information
MartyIX and bradwilson authored Jan 2, 2024
1 parent 336423e commit 62a2325
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ public async Task TestMethod() {
"booleanVar", // Reference value (we don't do lookup)
};

[Theory]
[MemberData(nameof(InvalidValues))]
public async void InvalidValue_InsideLambda_DoesNotTrigger(string argumentValue)
{
var source = @$"
using System.Threading.Tasks;
using Xunit;
public class TestClass {{
[Fact]
public async Task TestMethod() {{
var booleanVar = true;
var t = Task.Run(async () => {{
await Task.Delay(1).ConfigureAwait({argumentValue});
}});
await t;
}}
}}";

await Verify.VerifyAnalyzer(source);
}

[Theory]
[MemberData(nameof(InvalidValues))]
public async void InvalidValue_TaskWithAwait_Triggers(string argumentValue)
Expand Down Expand Up @@ -239,7 +261,29 @@ public async Task TestMethod() {{

[Theory]
[MemberData(nameof(InvalidValues))]
public async void InvalidValue_TaskWithAwait_DoesNotTrigger(string enumValue)
public async void InvalidValue_InsideLambda_DoesNotTrigger(string argumentValue)
{
var source = @$"
using System.Threading.Tasks;
using Xunit;
public class TestClass {{
[Fact]
public async Task TestMethod() {{
var enumVar = ConfigureAwaitOptions.ContinueOnCapturedContext;
var t = Task.Run(async () => {{
await Task.Delay(1).ConfigureAwait({argumentValue});
}});
await t;
}}
}}";

await Verify.VerifyAnalyzer(source);
}

[Theory]
[MemberData(nameof(InvalidValues))]
public async void InvalidValue_TaskWithAwait_Triggers(string enumValue)
{
var source = $@"
using System.Threading.Tasks;
Expand Down
5 changes: 5 additions & 0 deletions src/xunit.analyzers/X1000/DoNotUseConfigureAwait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public override void AnalyzeCompilation(
if (!invocation.IsInTestMethod(xunitContext))
return;

// Ignore anything inside a lambda expression
for (var current = context.Operation; current is not null; current = current.Parent)
if (current is IAnonymousFunctionOperation)
return;

// invocation should be two nodes: "(some other code).ConfigureAwait" and the arguments (like "(false)")
var invocationChildren = invocation.Syntax.ChildNodes().ToList();
if (invocationChildren.Count != 2)
Expand Down

0 comments on commit 62a2325

Please sign in to comment.