-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Fixer for xUnit1051 using TestContext.Current.CancellationToken (#…
…188)
- Loading branch information
Showing
5 changed files
with
226 additions
and
6 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
src/xunit.analyzers.fixes/X1000/UseCancellationTokenFixer.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,107 @@ | ||
using System.Collections.Generic; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace Xunit.Analyzers.Fixes; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public class UseCancellationTokenFixer : BatchedCodeFixProvider | ||
{ | ||
public const string Key_UseCancellationTokenArgument = "xUnit1051_UseCancellationTokenArgument"; | ||
|
||
public UseCancellationTokenFixer() : | ||
base(Descriptors.X1051_UseCancellationToken.Id) | ||
{ } | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); | ||
if (semanticModel is null) | ||
return; | ||
|
||
var testContextType = TypeSymbolFactory.TestContext_V3(semanticModel.Compilation); | ||
if (testContextType is null) | ||
return; | ||
|
||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
if (root is null) | ||
return; | ||
|
||
var diagnostic = context.Diagnostics.FirstOrDefault(); | ||
if (diagnostic is null) | ||
return; | ||
|
||
if (!diagnostic.Properties.TryGetValue(Constants.Properties.ParameterName, out var parameterName)) | ||
return; | ||
if (parameterName is null) | ||
return; | ||
|
||
if (!diagnostic.Properties.TryGetValue(Constants.Properties.ParameterIndex, out var parameterIndexText)) | ||
return; | ||
if (!int.TryParse(parameterIndexText, out var parameterIndex)) | ||
return; | ||
|
||
if (root.FindNode(diagnostic.Location.SourceSpan) is not InvocationExpressionSyntax invocation) | ||
return; | ||
|
||
var arguments = new List<ArgumentSyntax>(invocation.ArgumentList.Arguments); | ||
|
||
for (var argumentIndex = 0; argumentIndex < arguments.Count; argumentIndex++) | ||
{ | ||
if (arguments[argumentIndex].NameColon?.Name.Identifier.Text == parameterName) | ||
{ | ||
parameterIndex = argumentIndex; | ||
break; | ||
} | ||
} | ||
|
||
context.RegisterCodeFix( | ||
XunitCodeAction.Create( | ||
async ct => | ||
{ | ||
var editor = await DocumentEditor.CreateAsync(context.Document, ct).ConfigureAwait(false); | ||
|
||
var testContextCancellationTokenExpression = (ExpressionSyntax)editor.Generator.MemberAccessExpression( | ||
editor.Generator.MemberAccessExpression( | ||
editor.Generator.TypeExpression(testContextType), | ||
"Current" | ||
), | ||
"CancellationToken" | ||
); | ||
|
||
if (parameterIndex < arguments.Count) | ||
{ | ||
arguments[parameterIndex] = arguments[parameterIndex].WithExpression(testContextCancellationTokenExpression); | ||
} | ||
else | ||
{ | ||
var argument = Argument(testContextCancellationTokenExpression); | ||
if (parameterIndex > arguments.Count || arguments.Any(arg => arg.NameColon is not null)) | ||
{ | ||
argument = argument.WithNameColon(NameColon(parameterName)); | ||
} | ||
arguments.Add(argument); | ||
} | ||
|
||
editor.ReplaceNode( | ||
invocation, | ||
invocation | ||
.WithArgumentList(ArgumentList(SeparatedList(arguments))) | ||
); | ||
|
||
return editor.GetChangedDocument(); | ||
}, | ||
Key_UseCancellationTokenArgument, | ||
"{0} TestContext.Current.CancellationToken", parameterIndex < arguments.Count ? "Use" : "Add" | ||
), | ||
context.Diagnostics | ||
); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/xunit.analyzers.tests/Fixes/X1000/UseCancellationTokenFixerTests.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,99 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Analyzers.Fixes; | ||
using Verify = CSharpVerifier<Xunit.Analyzers.UseCancellationToken>; | ||
|
||
public class UseCancellationTokenFixerTests | ||
{ | ||
[Fact] | ||
public async Task UseCancellationTokenArgument() | ||
{ | ||
var before = /* lang=c#-test */ """ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
public class TestClass { | ||
[Fact] | ||
public void TestMethod() | ||
{ | ||
[|FunctionWithOverload(42)|]; | ||
[|FunctionWithOverload(42, default(CancellationToken))|]; | ||
[|FunctionWithDefaults()|]; | ||
[|FunctionWithDefaults(42)|]; | ||
[|FunctionWithDefaults(cancellationToken: default(CancellationToken))|]; | ||
[|FunctionWithDefaults(42, cancellationToken: default(CancellationToken))|]; | ||
} | ||
void FunctionWithOverload(int _) { } | ||
void FunctionWithOverload(int _1, CancellationToken _2) { } | ||
void FunctionWithDefaults(int _1 = 2112, CancellationToken cancellationToken = default(CancellationToken)) { } | ||
} | ||
"""; | ||
var after = /* lang=c#-test */ """ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
public class TestClass { | ||
[Fact] | ||
public void TestMethod() | ||
{ | ||
FunctionWithOverload(42, TestContext.Current.CancellationToken); | ||
FunctionWithOverload(42, TestContext.Current.CancellationToken); | ||
FunctionWithDefaults(cancellationToken: TestContext.Current.CancellationToken); | ||
FunctionWithDefaults(42, TestContext.Current.CancellationToken); | ||
FunctionWithDefaults(cancellationToken: TestContext.Current.CancellationToken); | ||
FunctionWithDefaults(42, cancellationToken: TestContext.Current.CancellationToken); | ||
} | ||
void FunctionWithOverload(int _) { } | ||
void FunctionWithOverload(int _1, CancellationToken _2) { } | ||
void FunctionWithDefaults(int _1 = 2112, CancellationToken cancellationToken = default(CancellationToken)) { } | ||
} | ||
"""; | ||
|
||
await Verify.VerifyCodeFixV3(before, after, UseCancellationTokenFixer.Key_UseCancellationTokenArgument); | ||
} | ||
|
||
[Fact] | ||
public async Task UseCancellationTokenArgument_AliasTestContext() | ||
{ | ||
var before = /* lang=c#-test */ """ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MyContext = Xunit.TestContext; | ||
public class TestClass { | ||
[Xunit.Fact] | ||
public void TestMethod() | ||
{ | ||
[|Function()|]; | ||
} | ||
void Function(CancellationToken token = default(CancellationToken)) { } | ||
} | ||
"""; | ||
var after = /* lang=c#-test */ """ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MyContext = Xunit.TestContext; | ||
public class TestClass { | ||
[Xunit.Fact] | ||
public void TestMethod() | ||
{ | ||
Function(MyContext.Current.CancellationToken); | ||
} | ||
void Function(CancellationToken token = default(CancellationToken)) { } | ||
} | ||
"""; | ||
|
||
await Verify.VerifyCodeFixV3(before, after, UseCancellationTokenFixer.Key_UseCancellationTokenArgument); | ||
} | ||
} |
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
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