From 03b1a9077ce9ba6b0736278e01fa9671a32a5424 Mon Sep 17 00:00:00 2001 From: "fabien.menager" Date: Sun, 1 Mar 2026 09:10:48 +0100 Subject: [PATCH] Check source generator output compilation --- .../ProjectionExpressionGeneratorTests.cs | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs index e5b07d2..0270c86 100644 --- a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs +++ b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -134,8 +135,8 @@ public Task SimpleProjectableComputedInNestedClassProperty() using System; using EntityFrameworkCore.Projectables; namespace Foo { - class C { - class D { + public class C { + public class D { public int Bar { get; set; } [Projectable] @@ -2540,7 +2541,7 @@ public record Entity #region Helpers - Compilation CreateCompilation(string source, bool expectedToCompile = true) + Compilation CreateCompilation([StringSyntax("csharp")] string source, bool expectedToCompile = true) { var references = Basic.Reference.Assemblies. #if NET10_0 @@ -2592,7 +2593,7 @@ private GeneratorDriverRunResult RunGenerator(Compilation compilation) var subject = new ProjectionExpressionGenerator(); var driver = CSharpGeneratorDriver .Create(subject) - .RunGenerators(compilation); + .RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out _); var result = driver.GetRunResult(); @@ -2602,11 +2603,11 @@ private GeneratorDriverRunResult RunGenerator(Compilation compilation) } else { - _testOutputHelper.WriteLine($"Diagnostics produced:"); + _testOutputHelper.WriteLine("Diagnostics produced:"); foreach (var diagnostic in result.Diagnostics) { - _testOutputHelper.WriteLine($" > " + diagnostic.ToString()); + _testOutputHelper.WriteLine(" > " + diagnostic); } } @@ -2616,7 +2617,30 @@ private GeneratorDriverRunResult RunGenerator(Compilation compilation) _testOutputHelper.WriteLine(newSyntaxTree.GetText().ToString()); } - return driver.GetRunResult(); + // Verify that the generated code compiles without errors + var hasGeneratorErrors = result.Diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error); + if (!hasGeneratorErrors && result.GeneratedTrees.Length > 0) + { + _testOutputHelper.WriteLine("Checking that generated code compiles..."); + + var compilationErrors = outputCompilation + .GetDiagnostics() + .Where(d => d.Severity == DiagnosticSeverity.Error) + .ToList(); + + if (compilationErrors.Count > 0) + { + _testOutputHelper.WriteLine("Generated code produced compilation errors:"); + foreach (var error in compilationErrors) + { + _testOutputHelper.WriteLine(" > " + error); + } + } + + Assert.Empty(compilationErrors); + } + + return result; } #endregion