-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xunit/xunit#2978: Add a diagnostic suppressor for VSTHRD200
- Loading branch information
1 parent
b404e78
commit 4f2297b
Showing
6 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/xunit.analyzers.tests/Suppressors/UseAsyncSuffixForAsyncMethodsSuppressorTests.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,38 @@ | ||
#if NETCOREAPP // System.Collections.Immutable 1.6.0 conflicts with 6.0.0 in NetFx | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Xunit; | ||
using Xunit.Analyzers; | ||
using Verify = CSharpVerifier<Xunit.Suppressors.UseAsyncSuffixForAsyncMethodsSuppressor>; | ||
|
||
public class UseAsyncSuffixForAsyncMethodsSuppressorTests | ||
{ | ||
[Fact] | ||
public async Task AcceptanceTest() | ||
{ | ||
var code = /* lang=c#-test */ """ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
public class NonTestClass { | ||
public async Task {|#0:NonTestMethod|}() { } | ||
} | ||
|
||
public class TestClass { | ||
[Fact] | ||
public async Task {|#1:TestMethod|]() { } | ||
} | ||
"""; | ||
var expected = new[] | ||
{ | ||
new DiagnosticResult("VSTHRD200", DiagnosticSeverity.Warning).WithLocation(0), | ||
new DiagnosticResult("VSTHRD200", DiagnosticSeverity.Warning).WithLocation(1).WithIsSuppressed(true), | ||
}; | ||
|
||
await Verify.VerifySuppressor(code, VsThreadingAnalyzers.VSTHRD200(), expected); | ||
} | ||
} | ||
|
||
#endif |
26 changes: 26 additions & 0 deletions
26
src/xunit.analyzers.tests/Utility/3rdPartyAnalyzers/VsThreadingAnalyzers.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,26 @@ | ||
#if NETCOREAPP // System.Collections.Immutable 1.6.0 conflicts with 6.0.0 in NetFx | ||
|
||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Xunit.Analyzers; | ||
|
||
public class VsThreadingAnalyzers : AnalyzerLoaderBase | ||
{ | ||
static readonly Lazy<Assembly> assemblyVsThreading = new(LoadVsThreadingAnalyzers, isThreadSafe: true); | ||
static readonly Lazy<Type> typeVSTHRD200 = new(() => FindType(assemblyVsThreading, "Microsoft.VisualStudio.Threading.Analyzers.VSTHRD200UseAsyncNamingConventionAnalyzer"), isThreadSafe: true); | ||
|
||
public static DiagnosticAnalyzer VSTHRD200() => | ||
Activator.CreateInstance(typeVSTHRD200.Value) as DiagnosticAnalyzer ?? throw new InvalidOperationException($"Could not create instance of '{typeVSTHRD200.Value.FullName}'"); | ||
|
||
static Assembly LoadVsThreadingAnalyzers() | ||
{ | ||
LoadAssembly(Path.Combine(NuGetPackagesFolder, "system.collections.immutable", "6.0.0", "lib", "net6.0", "System.Collections.Immutable.dll")); | ||
LoadAssembly(Path.Combine(NuGetPackagesFolder, "microsoft.codeanalysis.workspaces.common", "3.11.0", "lib", "netcoreapp3.1", "Microsoft.CodeAnalysis.Workspaces.dll")); | ||
return LoadAssembly(Path.Combine(NuGetPackagesFolder, "microsoft.visualstudio.threading.analyzers", "17.10.48", "analyzers", "cs", "Microsoft.VisualStudio.Threading.Analyzers.dll")); | ||
} | ||
} | ||
|
||
#endif |
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
34 changes: 34 additions & 0 deletions
34
src/xunit.analyzers/Suppressors/UseAsyncSuffixForAsyncMethodsSuppressor.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,34 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Xunit.Analyzers; | ||
|
||
namespace Xunit.Suppressors; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class UseAsyncSuffixForAsyncMethodsSuppressor : XunitDiagnosticSuppressor | ||
{ | ||
public UseAsyncSuffixForAsyncMethodsSuppressor() : | ||
base(Descriptors.VSTHRD200_Suppression) | ||
{ } | ||
|
||
protected override bool ShouldSuppress( | ||
Diagnostic diagnostic, | ||
SuppressionAnalysisContext context, | ||
XunitContext xunitContext) | ||
{ | ||
var attributeUsageType = TypeSymbolFactory.AttributeUsageAttribute(context.Compilation); | ||
if (attributeUsageType is null) | ||
return false; | ||
|
||
if (diagnostic?.Location.SourceTree is null) | ||
return false; | ||
|
||
if (diagnostic.Location.SourceTree.GetRoot().FindNode(diagnostic.Location.SourceSpan) is not MethodDeclarationSyntax methodDeclaration) | ||
return false; | ||
|
||
var semanticModel = context.GetSemanticModel(diagnostic.Location.SourceTree); | ||
var methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration) as IMethodSymbol; | ||
return methodSymbol.IsTestMethod(xunitContext, attributeUsageType, strict: false); | ||
} | ||
} |
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