-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix AwaitAssertionCodeFixProvider and make it modify the method signa…
…ture to return an async Task (#1736) * Fix AwaitAssertionCodeFixProvider and make it modify the method signature to return an async Task * Missed project file * Run tests in pipeline * Fix code fix * Fix
- Loading branch information
Showing
132 changed files
with
942 additions
and
539 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
TUnit.Assertions.Analyzers.CodeFixers.Tests/AwaitAssertionCodeFixProviderTests.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,48 @@ | ||
using NUnit.Framework; | ||
using Verifier = TUnit.Assertions.Analyzers.CodeFixers.Tests.Verifiers.CSharpCodeFixVerifier<TUnit.Assertions.Analyzers.AwaitAssertionAnalyzer, TUnit.Assertions.Analyzers.CodeFixers.AwaitAssertionCodeFixProvider>; | ||
#pragma warning disable CS0162 // Unreachable code detected | ||
|
||
namespace TUnit.Assertions.Analyzers.CodeFixers.Tests; | ||
|
||
public class AwaitAssertionCodeFixProviderTests | ||
{ | ||
[Test] | ||
public async Task Void_Changes_To_Async_Task() | ||
{ | ||
return; | ||
// TODO - It's complaining even though it matches: | ||
await Verifier | ||
.VerifyCodeFixAsync( | ||
""" | ||
using System.Threading.Tasks; | ||
using TUnit.Assertions; | ||
using TUnit.Assertions.Extensions; | ||
using TUnit.Core; | ||
public class MyClass | ||
{ | ||
public void MyTest() | ||
{ | ||
{|#0:Assert.That(1)|}.IsEqualTo(1); | ||
} | ||
} | ||
""", | ||
Verifier.Diagnostic(Rules.AwaitAssertion) | ||
.WithLocation(0), | ||
""" | ||
using System.Threading.Tasks; | ||
using TUnit.Assertions; | ||
using TUnit.Assertions.Extensions; | ||
using TUnit.Core; | ||
public class MyClass | ||
{ | ||
public async Task MyTest() | ||
{ | ||
await Assert.That(1).IsEqualTo(1); | ||
} | ||
} | ||
""" | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
....Assertions.Analyzers.CodeFixers.Tests/TUnit.Assertions.Analyzers.CodeFixers.Tests.csproj
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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TUnit.Assertions.Analyzers.CodeFixers\TUnit.Assertions.Analyzers.CodeFixers.csproj" /> | ||
<ProjectReference Include="..\TUnit.Assertions.Analyzers\TUnit.Assertions.Analyzers.csproj" /> | ||
<ProjectReference Include="..\TUnit.Assertions\TUnit.Assertions.csproj" /> | ||
<ProjectReference Include="..\TUnit.Core\TUnit.Core.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeRefactoring.Testing" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" VersionOverride="4.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="NUnit" /> | ||
<PackageReference Include="NUnit.Analyzers"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="NUnit3TestAdapter" /> | ||
</ItemGroup> | ||
</Project> |
49 changes: 49 additions & 0 deletions
49
TUnit.Assertions.Analyzers.CodeFixers.Tests/Verifiers/CSharpCodeFixVerifier.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,49 @@ | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace TUnit.Assertions.Analyzers.CodeFixers.Tests.Verifiers; | ||
|
||
public static partial class CSharpCodeFixVerifier<TAnalyzer, TCodeFix> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
where TCodeFix : CodeFixProvider, new() | ||
{ | ||
public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, DefaultVerifier> | ||
{ | ||
public Test() | ||
{ | ||
SolutionTransforms.Add((solution, projectId) => | ||
{ | ||
var project = solution.GetProject(projectId); | ||
|
||
if (project is null) | ||
{ | ||
return solution; | ||
} | ||
|
||
var compilationOptions = project.CompilationOptions; | ||
|
||
if (compilationOptions is null) | ||
{ | ||
return solution; | ||
} | ||
|
||
var parseOptions = project.ParseOptions as CSharpParseOptions; | ||
|
||
if (parseOptions is null) | ||
{ | ||
return solution; | ||
} | ||
|
||
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); | ||
|
||
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions) | ||
.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(LanguageVersion.Preview)); | ||
|
||
return solution; | ||
}); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
TUnit.Assertions.Analyzers.CodeFixers.Tests/Verifiers/CSharpCodeFixVerifier`2.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,86 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using TUnit.Assertions.AssertionBuilders; | ||
using TUnit.Core; | ||
|
||
namespace TUnit.Assertions.Analyzers.CodeFixers.Tests.Verifiers; | ||
|
||
public static partial class CSharpCodeFixVerifier<TAnalyzer, TCodeFix> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
where TCodeFix : CodeFixProvider, new() | ||
{ | ||
/// <inheritdoc cref="Microsoft.CodeAnalysis.Diagnostic"/> | ||
public static DiagnosticResult Diagnostic() | ||
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.Diagnostic(); | ||
|
||
/// <inheritdoc cref="Microsoft.CodeAnalysis.Diagnostic"/> | ||
public static DiagnosticResult Diagnostic(string diagnosticId) | ||
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.Diagnostic(diagnosticId); | ||
|
||
/// <inheritdoc cref="Microsoft.CodeAnalysis.Diagnostic"/> | ||
public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) | ||
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.Diagnostic(descriptor); | ||
|
||
public static async Task VerifyAnalyzerAsync( | ||
[StringSyntax("c#-test")] string source, | ||
params DiagnosticResult[] expected | ||
) | ||
{ | ||
var test = new Test | ||
{ | ||
TestCode = source, | ||
CodeActionValidationMode = CodeActionValidationMode.SemanticStructure, | ||
ReferenceAssemblies = ReferenceAssemblies.Net.Net90, | ||
TestState = | ||
{ | ||
AdditionalReferences = | ||
{ | ||
typeof(TUnitAttribute).Assembly.Location, | ||
typeof(AssertionBuilder).Assembly.Location, | ||
}, | ||
}, | ||
}; | ||
|
||
test.ExpectedDiagnostics.AddRange(expected); | ||
await test.RunAsync(CancellationToken.None); | ||
} | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, string)"/> | ||
public static async Task VerifyCodeFixAsync([StringSyntax("c#-test")] string source, [StringSyntax("c#-test")] string fixedSource) | ||
=> await VerifyCodeFixAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, DiagnosticResult, string)"/> | ||
public static async Task VerifyCodeFixAsync([StringSyntax("c#-test")] string source, DiagnosticResult expected, [StringSyntax("c#-test")] string fixedSource) | ||
=> await VerifyCodeFixAsync(source, [expected], fixedSource); | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, DiagnosticResult[], string)"/> | ||
public static async Task VerifyCodeFixAsync( | ||
[StringSyntax("c#-test")] string source, | ||
IEnumerable<DiagnosticResult> expected, | ||
[StringSyntax("c#-test")] string fixedSource | ||
) | ||
{ | ||
var test = new Test | ||
{ | ||
TestCode = source, | ||
FixedCode = fixedSource, | ||
ReferenceAssemblies = ReferenceAssemblies.Net.Net90, | ||
TestState = | ||
{ | ||
AdditionalReferences = | ||
{ | ||
typeof(TUnitAttribute).Assembly.Location, | ||
typeof(AssertionBuilder).Assembly.Location, | ||
}, | ||
}, | ||
}; | ||
|
||
test.ExpectedDiagnostics.AddRange(expected); | ||
|
||
await test.RunAsync(CancellationToken.None); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
TUnit.Assertions.Analyzers.CodeFixers.Tests/Verifiers/CSharpCodeRefactoringVerifier.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,47 @@ | ||
using Microsoft.CodeAnalysis.CodeRefactorings; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace TUnit.Assertions.Analyzers.CodeFixers.Tests.Verifiers; | ||
|
||
public static partial class CSharpCodeRefactoringVerifier<TCodeRefactoring> | ||
where TCodeRefactoring : CodeRefactoringProvider, new() | ||
{ | ||
public class Test : CSharpCodeRefactoringTest<TCodeRefactoring, DefaultVerifier> | ||
{ | ||
public Test() | ||
{ | ||
SolutionTransforms.Add((solution, projectId) => | ||
{ | ||
var project = solution.GetProject(projectId); | ||
|
||
if (project is null) | ||
{ | ||
return solution; | ||
} | ||
|
||
var compilationOptions = project.CompilationOptions; | ||
|
||
if (compilationOptions is null) | ||
{ | ||
return solution; | ||
} | ||
|
||
var parseOptions = project.ParseOptions as CSharpParseOptions; | ||
|
||
if (parseOptions is null) | ||
{ | ||
return solution; | ||
} | ||
|
||
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); | ||
|
||
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions) | ||
.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(LanguageVersion.Preview)); | ||
|
||
return solution; | ||
}); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
TUnit.Assertions.Analyzers.CodeFixers.Tests/Verifiers/CSharpCodeRefactoringVerifier`1.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,33 @@ | ||
using Microsoft.CodeAnalysis.CodeRefactorings; | ||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace TUnit.Assertions.Analyzers.CodeFixers.Tests.Verifiers; | ||
|
||
public static partial class CSharpCodeRefactoringVerifier<TCodeRefactoring> | ||
where TCodeRefactoring : CodeRefactoringProvider, new() | ||
{ | ||
/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, string)"/> | ||
public static async Task VerifyRefactoringAsync(string source, string fixedSource) | ||
{ | ||
await VerifyRefactoringAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); | ||
} | ||
|
||
/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, DiagnosticResult, string)"/> | ||
public static async Task VerifyRefactoringAsync(string source, DiagnosticResult expected, string fixedSource) | ||
{ | ||
await VerifyRefactoringAsync(source, [expected], fixedSource); | ||
} | ||
|
||
/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, DiagnosticResult[], string)"/> | ||
public static async Task VerifyRefactoringAsync(string source, DiagnosticResult[] expected, string fixedSource) | ||
{ | ||
var test = new Test | ||
{ | ||
TestCode = source, | ||
FixedCode = fixedSource, | ||
}; | ||
|
||
test.ExpectedDiagnostics.AddRange(expected); | ||
await test.RunAsync(CancellationToken.None); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
TUnit.Assertions.Analyzers.CodeFixers.Tests/Verifiers/CSharpVerifierHelper.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,32 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
namespace TUnit.Assertions.Analyzers.CodeFixers.Tests.Verifiers; | ||
|
||
internal static class CSharpVerifierHelper | ||
{ | ||
/// <summary> | ||
/// By default, the compiler reports diagnostics for nullable reference types at | ||
/// <see cref="DiagnosticSeverity.Warning"/>, and the analyzer test framework defaults to only validating | ||
/// diagnostics at <see cref="DiagnosticSeverity.Error"/>. This map contains all compiler diagnostic IDs | ||
/// related to nullability mapped to <see cref="ReportDiagnostic.Error"/>, which is then used to enable all | ||
/// of these warnings for default validation during analyzer and code fix tests. | ||
/// </summary> | ||
internal static ImmutableDictionary<string, ReportDiagnostic> NullableWarnings { get; } = GetNullableWarningsFromCompiler(); | ||
|
||
private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler() | ||
{ | ||
string[] args = ["/warnaserror:nullable", "-p:LangVersion=preview"]; | ||
var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory); | ||
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions; | ||
|
||
// Workaround for https://github.com/dotnet/roslyn/issues/41610 | ||
nullableWarnings = nullableWarnings | ||
.SetItem("CS8632", ReportDiagnostic.Error) | ||
.SetItem("CS8669", ReportDiagnostic.Error) | ||
.SetItem("CS8652", ReportDiagnostic.Suppress); | ||
|
||
return nullableWarnings; | ||
} | ||
} |
Oops, something went wrong.