-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e622842
commit 6d907a0
Showing
10 changed files
with
194 additions
and
7 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/xunit.analyzers.tests/Suppressors/MakeTypesInternalSuppressorTests.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,66 @@ | ||
#if NETCOREAPP | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Xunit; | ||
using Xunit.Analyzers; | ||
using Verify = CSharpVerifier<Xunit.Suppressors.MakeTypesInternalSuppressor>; | ||
|
||
#if !ROSLYN_3_11 | ||
using System; | ||
#endif | ||
|
||
public sealed class MakeTypesInternalSuppressorTests | ||
{ | ||
[Fact] | ||
public async Task NonTestClass_DoesNotSuppress() | ||
{ | ||
var code = @" | ||
public class NonTestClass { | ||
public void NonTestMethod() { } | ||
}"; | ||
|
||
var expected = | ||
new DiagnosticResult("CA1515", DiagnosticSeverity.Warning) | ||
.WithSpan(2, 14, 2, 26) | ||
.WithIsSuppressed(false); | ||
|
||
await Verify.VerifySuppressor(code, CodeAnalysisNetAnalyzers.CA1515(), expected); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Fact")] | ||
[InlineData("FactAttribute")] | ||
[InlineData("Theory")] | ||
[InlineData("TheoryAttribute")] | ||
[InlineData("CustomFact")] | ||
[InlineData("CustomFactAttribute")] | ||
public async Task TestClass_Suppresses(string attribute) | ||
{ | ||
var code = @$" | ||
using Xunit; | ||
internal class CustomFactAttribute : FactAttribute {{ }} | ||
public class TestClass {{ | ||
[{attribute}] | ||
public void TestMethod() {{ }} | ||
}}"; | ||
|
||
#if ROSLYN_3_11 | ||
// Roslyn 3.11 still surfaces the diagnostic that has been suppressed | ||
var expected = | ||
new DiagnosticResult("CA1515", DiagnosticSeverity.Warning) | ||
.WithSpan(6, 14, 6, 23) | ||
.WithIsSuppressed(true); | ||
#else | ||
// Later versions of Roslyn just removes it from the results | ||
var expected = Array.Empty<DiagnosticResult>(); | ||
#endif | ||
|
||
await Verify.VerifySuppressor(code, CodeAnalysisNetAnalyzers.CA1515(), expected); | ||
} | ||
} | ||
|
||
#endif |
46 changes: 46 additions & 0 deletions
46
src/xunit.analyzers.tests/Utility/CodeAnalysisNetAnalyzers.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,46 @@ | ||
#if NETCOREAPP | ||
|
||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Loader; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Xunit.Analyzers; | ||
|
||
public static class CodeAnalysisNetAnalyzers | ||
{ | ||
public static Lazy<Assembly> assembly = new(LoadAssembly, isThreadSafe: true); | ||
public static Lazy<Type> typeCA1515 = new(() => FindType("Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpMakeTypesInternal"), isThreadSafe: true); | ||
|
||
public static DiagnosticAnalyzer CA1515() => | ||
Activator.CreateInstance(typeCA1515.Value) as DiagnosticAnalyzer ?? throw new InvalidOperationException($"Could not create instance of '{typeCA1515.Value.FullName}'"); | ||
|
||
static Type FindType(string typeName) => | ||
assembly.Value.GetType(typeName) ?? throw new InvalidOperationException($"Could not locate type '{typeName}' from Microsoft.CodeAnalysis.NetAnalyzers"); | ||
|
||
static Assembly LoadAssembly() | ||
{ | ||
var nugetPackagesFolder = Environment.GetEnvironmentVariable("NUGET_PACKAGES"); | ||
if (nugetPackagesFolder is null) | ||
{ | ||
var homeFolder = | ||
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | ||
? Environment.GetEnvironmentVariable("USERPROFILE") | ||
: Environment.GetEnvironmentVariable("HOME"); | ||
|
||
nugetPackagesFolder = Path.Combine(homeFolder ?? throw new InvalidOperationException("Could not determine home folder"), ".nuget", "packages"); | ||
} | ||
|
||
if (!Directory.Exists(nugetPackagesFolder)) | ||
throw new InvalidOperationException($"NuGet package cache folder '{nugetPackagesFolder}' does not exist"); | ||
|
||
var loadContext = AssemblyLoadContext.Default; | ||
loadContext.LoadFromAssemblyPath(Path.Combine(nugetPackagesFolder, "microsoft.codeanalysis.workspaces.common", "3.11.0", "lib", "netcoreapp3.1", "Microsoft.CodeAnalysis.Workspaces.dll")); | ||
loadContext.LoadFromAssemblyPath(Path.Combine(nugetPackagesFolder, "microsoft.codeanalysis.netanalyzers", "9.0.0-preview.24072.1", "analyzers", "dotnet", "cs", "Microsoft.CodeAnalysis.NetAnalyzers.dll")); | ||
return loadContext.LoadFromAssemblyPath(Path.Combine(nugetPackagesFolder, "microsoft.codeanalysis.netanalyzers", "9.0.0-preview.24072.1", "analyzers", "dotnet", "cs", "Microsoft.CodeAnalysis.CSharp.NetAnalyzers.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
31 changes: 31 additions & 0 deletions
31
src/xunit.analyzers/Suppressors/MakeTypesInternalSuppressor.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,31 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Xunit.Analyzers; | ||
|
||
namespace Xunit.Suppressors; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class MakeTypesInternalSuppressor : XunitDiagnosticSuppressor | ||
{ | ||
public MakeTypesInternalSuppressor() : | ||
base(Descriptors.CA1515_Suppression) | ||
{ } | ||
|
||
protected override bool ShouldSuppress( | ||
Diagnostic diagnostic, | ||
SuppressionAnalysisContext context, | ||
XunitContext xunitContext) | ||
{ | ||
if (diagnostic.Location.SourceTree is null) | ||
return false; | ||
|
||
var root = diagnostic.Location.SourceTree.GetRoot(context.CancellationToken); | ||
if (root?.FindNode(diagnostic.Location.SourceSpan) is not ClassDeclarationSyntax classDeclaration) | ||
return false; | ||
|
||
var semanticModel = context.GetSemanticModel(diagnostic.Location.SourceTree); | ||
var classSymbol = semanticModel.GetDeclaredSymbol(classDeclaration) as ITypeSymbol; | ||
return classSymbol.IsTestClass(xunitContext, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Xunit.Analyzers; | ||
|
||
public static partial class Descriptors | ||
{ | ||
public static SuppressionDescriptor CA1515_Suppression { get; } = | ||
Suppression("CA1515", "xUnit.net's test classes must be public."); | ||
} |
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
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