From d78c95eb03a5bb0b099901b55e5bfde021ba3dca Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Mon, 27 Feb 2023 16:52:10 +0100 Subject: [PATCH] Verify source generator diagnostics --- .../RazorSourceGeneratorTests.cs | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs b/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs index b2c2204cca5..d59e1df0924 100644 --- a/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs +++ b/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs @@ -512,8 +512,11 @@ public class Person }", Encoding.UTF8)).Project; compilation = await project.GetCompilationAsync(); - result = RunGenerator(compilation!, ref driver) - .VerifyOutputsMatch(result); + result = RunGenerator(compilation!, ref driver, + // Person.cs(4,19): warning CS8618: Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. + d => Assert.Equal(("SourceFile(Person.cs[44..48))", "CS8618"), (d.Location.ToString(), d.Id))); + + result.VerifyOutputsMatch(result); Assert.Empty(result.Diagnostics); Assert.Equal(2, result.GeneratedSources.Length); @@ -544,7 +547,13 @@ public class Person var compilation = await project.GetCompilationAsync(); var (driver, additionalTexts) = await GetDriverWithAdditionalTextAsync(project); - var result = RunGenerator(compilation!, ref driver) + var expectedDiagnostics = new Action[] + { + // Person.cs(4,19): warning CS8618: Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. + d => Assert.Equal(("SourceFile(Person.cs[44..48))", "CS8618"), (d.Location.ToString(), d.Id)) + }; + + var result = RunGenerator(compilation!, ref driver, expectedDiagnostics) .VerifyPageOutput( @"#pragma checksum ""Pages/Index.razor"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""6b5db227a6aa2228c777b0771108b184b1fc5df3"" // @@ -598,7 +607,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. eventListener.Events.Clear(); - result = RunGenerator(compilation!, ref driver) + result = RunGenerator(compilation!, ref driver, expectedDiagnostics) .VerifyOutputsMatch(result); Assert.Empty(result.Diagnostics); @@ -612,7 +621,7 @@ public class Person }", Encoding.UTF8)).Project; compilation = await project.GetCompilationAsync(); - result = RunGenerator(compilation!, ref driver) + result = RunGenerator(compilation!, ref driver, expectedDiagnostics) .VerifyOutputsMatch(result); Assert.Empty(result.Diagnostics); @@ -1009,8 +1018,11 @@ public async Task IncrementalCompilation_RazorFiles_WhenProjectReferencesChange( var compilation = await project.GetCompilationAsync(); var (driver, additionalTexts) = await GetDriverWithAdditionalTextAsync(project); - var result = RunGenerator(compilation!, ref driver) - .VerifyPageOutput( + var result = RunGenerator(compilation!, ref driver, + // Pages/Index.razor(2,7): error CS0246: The type or namespace name 'SurveyPromptRootNamspace' could not be found (are you missing a using directive or an assembly reference?) + d => Assert.Equal(("SourceFile(Microsoft.NET.Sdk.Razor.SourceGenerators\\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\\Pages_Index_razor.g.cs[433..457))", "CS0246"), (d.Location.ToString(), d.Id))); + + result.VerifyPageOutput( @"#pragma checksum ""Pages/Index.razor"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""4745828f8a0ab77b58022ed5d1095a0242f2a7ee"" // #pragma warning disable 1591 @@ -1573,7 +1585,13 @@ public class Person var compilation = await project.GetCompilationAsync(); var (driver, additionalTexts) = await GetDriverWithAdditionalTextAsync(project); - var result = RunGenerator(compilation!, ref driver) + var expectedDiagnostics = new Action[] + { + // Person.cs(4,19): warning CS8618: Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. + d => Assert.Equal(("SourceFile(Person.cs[44..48))", "CS8618"), (d.Location.ToString(), d.Id)) + }; + + var result = RunGenerator(compilation!, ref driver, expectedDiagnostics) .VerifyPageOutput(@"#pragma checksum ""Pages/Index.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""6b5db227a6aa2228c777b0771108b184b1fc5df3"" // #pragma warning disable 1591 @@ -1680,7 +1698,7 @@ internal sealed class Views_Shared__Layout : global::Microsoft.AspNetCore.Mvc.Ra eventListener.Events.Clear(); - result = RunGenerator(compilation!, ref driver) + result = RunGenerator(compilation!, ref driver, expectedDiagnostics) .VerifyOutputsMatch(result); Assert.Empty(result.Diagnostics); @@ -1694,7 +1712,7 @@ public class Person }", Encoding.UTF8)).Project; compilation = await project.GetCompilationAsync(); - result = RunGenerator(compilation!, ref driver) + result = RunGenerator(compilation!, ref driver, expectedDiagnostics) .VerifyOutputsMatch(result); Assert.Empty(result.Diagnostics); @@ -2457,12 +2475,15 @@ private static async ValueTask GetDriverAsync(Project project) return (driver, additionalTexts, optionsProvider); } - private static GeneratorRunResult RunGenerator(Compilation compilation, ref GeneratorDriver driver) + private static GeneratorRunResult RunGenerator(Compilation compilation, ref GeneratorDriver driver, params Action[] expectedDiagnostics) { - driver = driver.RunGenerators(compilation); + driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out compilation, out _); + + var actualDiagnostics = compilation.GetDiagnostics().Where(d => d.Severity != DiagnosticSeverity.Hidden); + Assert.Collection(actualDiagnostics, expectedDiagnostics); var result = driver.GetRunResult(); - return result.Results[0]; + return result.Results.Single(); } private static Project CreateTestProject(