Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify source generator diagnostics #8338

Merged
merged 2 commits into from
Mar 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Diagnostic>[]
{
// 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""
// <auto-generated/>
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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""
// <auto-generated/>
#pragma warning disable 1591
Expand Down Expand Up @@ -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<Diagnostic>[]
{
// 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""
// <auto-generated/>
#pragma warning disable 1591
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -2457,12 +2475,15 @@ private static async ValueTask<GeneratorDriver> 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<Diagnostic>[] 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(
Expand Down