Skip to content

Commit

Permalink
#177: xUnit1030 should not trigger inside lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jan 2, 2024
1 parent 397e6d5 commit ee7e5d9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
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 @@ -185,27 +207,6 @@ public async Task TestMethod() {{

await Verify.VerifyAnalyzer(source, expected);
}

[Fact]
public async void False_DoesNotTriggerInNestedTask()
{
var source = @"
using System.Threading.Tasks;
using Xunit;
public class TestClass {
[Fact]
public async Task TestMethod() {
var t = Task.Run(async () => {
await Task.Delay(1).ConfigureAwait(false);
});
await t;
}
}";

await Verify.VerifyAnalyzer(source);
}
}

#if NETCOREAPP
Expand Down Expand Up @@ -260,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 ee7e5d9

Please sign in to comment.