Skip to content

Commit

Permalink
Add support for filtering diagnostic severity (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll authored Feb 17, 2024
1 parent 9b4ee4f commit 6bebd17
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/Testing.SourceGenerators/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public static MetadataReference CreateMetadataReference(this CSharpCompilation c
return MetadataReference.CreateFromStream(assemblyStream, MetadataReferenceProperties.Assembly);
}

internal static IEnumerable<Diagnostic> OrderDiagnosticResults(this IEnumerable<Diagnostic> diagnostics)
internal static IEnumerable<Diagnostic> OrderDiagnosticResults(this IEnumerable<Diagnostic> diagnostics, DiagnosticSeverity severity)
{
return diagnostics
.OrderBy(static z => z.Id)
.Where(s => s.Severity >= severity)
.OrderBy(static z => z.Location.GetMappedLineSpan().ToString())
.ThenBy(static z => z.Severity)
.ThenBy(static z => z.Location.SourceSpan.Start)
.ThenBy(static z => z.Location.SourceSpan.End);
.ThenBy(static z => z.Id);
}
}
2 changes: 1 addition & 1 deletion src/Testing.SourceGenerators/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ static Rocket.Surgery.Extensions.Testing.SourceGenerators.GeneratorTestContextBu
static Rocket.Surgery.Extensions.Testing.SourceGenerators.GeneratorTestContextBuilder.CreateWithoutDefaultReferences() -> Rocket.Surgery.Extensions.Testing.SourceGenerators.GeneratorTestContextBuilder!
static Rocket.Surgery.Extensions.Testing.SourceGenerators.GeneratorTestResults.implicit operator Microsoft.CodeAnalysis.CSharp.CSharpCompilation!(Rocket.Surgery.Extensions.Testing.SourceGenerators.GeneratorTestResults! results) -> Microsoft.CodeAnalysis.CSharp.CSharpCompilation!
static Rocket.Surgery.Extensions.Testing.SourceGenerators.GeneratorTestResults.implicit operator System.Reflection.Assembly?(Rocket.Surgery.Extensions.Testing.SourceGenerators.GeneratorTestResults! results) -> System.Reflection.Assembly?
static Rocket.Surgery.Extensions.Testing.SourceGenerators.VerifyGeneratorTextContext.Initialize(bool includeInputs = true, bool includeOptions = true) -> void
static Rocket.Surgery.Extensions.Testing.SourceGenerators.VerifyGeneratorTextContext.Initialize(bool includeInputs = true, bool includeOptions = true, Microsoft.CodeAnalysis.DiagnosticSeverity diagnosticSeverityFilter = Microsoft.CodeAnalysis.DiagnosticSeverity.Warning) -> void
11 changes: 7 additions & 4 deletions src/Testing.SourceGenerators/VerifyGeneratorTextContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ public static class VerifyGeneratorTextContext
{
public static void Initialize(
bool includeInputs = true,
bool includeOptions = true
bool includeOptions = true,
DiagnosticSeverity diagnosticSeverityFilter = DiagnosticSeverity.Warning
)
{
VerifyGeneratorTextContext.includeInputs = includeInputs;
VerifyGeneratorTextContext.includeOptions = includeOptions;
VerifyGeneratorTextContext.diagnosticSeverityFilter = diagnosticSeverityFilter;
VerifySourceGenerators.Initialize();
VerifierSettings.RegisterFileConverter<GeneratorTestResult>(Convert);
VerifierSettings.RegisterFileConverter<GeneratorTestResults>(Convert);
Expand All @@ -25,6 +27,7 @@ public static void Initialize(

private static bool includeInputs;
private static bool includeOptions;
private static DiagnosticSeverity diagnosticSeverityFilter;

private static ConversionResult Convert(GeneratorTestResults target, IReadOnlyDictionary<string, object> context)
{
Expand All @@ -42,7 +45,7 @@ private static ConversionResult Convert(GeneratorTestResults target, IReadOnlyDi
var data = new Dictionary<string, object>();
if (includeInputs)
{
data["InputDiagnostics"] = target.InputDiagnostics.OrderDiagnosticResults();
data["InputDiagnostics"] = target.InputDiagnostics.OrderDiagnosticResults(diagnosticSeverityFilter);
data["InputAdditionalTexts"] = target.InputAdditionalTexts;
}

Expand All @@ -68,10 +71,10 @@ private static ConversionResult Convert(GeneratorTestResults target, IReadOnlyDi
.OrderBy(z => z);
}

data["FinalDiagnostics"] = target.FinalDiagnostics.OrderDiagnosticResults();
data["FinalDiagnostics"] = target.FinalDiagnostics.OrderDiagnosticResults(diagnosticSeverityFilter);
data["GeneratorDiagnostics"] = target.Results.ToDictionary(
z => z.Key.FullName!,
z => z.Value.Diagnostics.OrderDiagnosticResults()
z => z.Value.Diagnostics.OrderDiagnosticResults(diagnosticSeverityFilter)
);

return new(data, targets);
Expand Down

0 comments on commit 6bebd17

Please sign in to comment.