From 6ff23d2749a879a75027384bf21b8a50647b7292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Sat, 20 Jun 2026 11:57:11 +0200 Subject: [PATCH] Simplify the metadata references In the past (#107 and #110) I experienced issues in the unit test project because if missing references in the C# compilation. It was really hard to pinpoint which references were missing. Using the Basic.Reference.Assemblies.Net100 NuGet package and adding all the metadata references ensures that all references are available and that the compilation will work as expected. --- Directory.Packages.props | 1 + tests/Generator.Tests/Generator.Tests.csproj | 1 + tests/Generator.Tests/TestHelper.cs | 55 ++++---------------- 3 files changed, 12 insertions(+), 45 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 1c04406..dd74681 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,5 +1,6 @@ + diff --git a/tests/Generator.Tests/Generator.Tests.csproj b/tests/Generator.Tests/Generator.Tests.csproj index ad4db8a..72dc52f 100644 --- a/tests/Generator.Tests/Generator.Tests.csproj +++ b/tests/Generator.Tests/Generator.Tests.csproj @@ -17,6 +17,7 @@ + all diff --git a/tests/Generator.Tests/TestHelper.cs b/tests/Generator.Tests/TestHelper.cs index 7cc9da3..65f6aa6 100644 --- a/tests/Generator.Tests/TestHelper.cs +++ b/tests/Generator.Tests/TestHelper.cs @@ -1,8 +1,5 @@ -using System.ComponentModel; -using System.Data.Common; -using System.Diagnostics; +using Basic.Reference.Assemblies; using System.Text.RegularExpressions; -using System.Xml; using Zomp.SyncMethodGenerator; using static Generator.Tests.ModuleInitializer; @@ -88,46 +85,6 @@ namespace Test; var syntaxTree = CSharpSyntaxTree.ParseText(source, parseOptions); var globalUsings = CSharpSyntaxTree.ParseText(GlobalUsingsSource, parseOptions); - // SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source); - var linqAssembly = typeof(Enumerable).Assembly.Location; - var locations = new List - { - typeof(IAsyncEnumerable<>).Assembly.Location, - typeof(DataReceivedEventHandler).Assembly.Location, - typeof(DbDataReader).Assembly.Location, - typeof(ValueTask<>).Assembly.Location, - typeof(System.Drawing.Point).Assembly.Location, - typeof(object).Assembly.Location, - typeof(Console).Assembly.Location, - typeof(Memory<>).Assembly.Location, - typeof(System.Buffers.MemoryPool<>).Assembly.Location, - typeof(Queue<>).Assembly.Location, - typeof(LinkedListNode<>).Assembly.Location, - typeof(XmlReader).Assembly.Location, - typeof(IQueryable).Assembly.Location, - typeof(System.Net.HttpStatusCode).Assembly.Location, - typeof(IListSource).Assembly.Location, - typeof(Regex).Assembly.Location, - typeof(Queryable).Assembly.Location, -#if NET8_0_OR_GREATER - typeof(AsyncEnumerable).Assembly.Location, - typeof(Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions).Assembly.Location, -#endif - linqAssembly, - }; - - var directory = Path.GetDirectoryName(linqAssembly); - var runtimeLocation = directory is null ? null : Path.Combine(directory, "System.Runtime.dll"); - if (runtimeLocation is not null && File.Exists(runtimeLocation)) - { - locations.Add(runtimeLocation); - } - - var distinct = locations.Distinct().ToArray(); - - var references = distinct - .Select(l => MetadataReference.CreateFromFile(l)); - List syntaxTrees = [syntaxTree]; if (languageVersion >= LanguageVersion.CSharp10) @@ -135,11 +92,19 @@ namespace Test; syntaxTrees.Add(globalUsings); } +#if NET8_0_OR_GREATER + var assemblies = new[] + { + typeof(Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions).Assembly, + }; +#else + IEnumerable assemblies = []; +#endif var compilation = CSharpCompilation.Create( assemblyName: "Tests", options: new(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: languageVersion >= LanguageVersion.CSharp8 ? NullableContextOptions.Enable : NullableContextOptions.Disable), syntaxTrees: syntaxTrees, - references: references); + references: Net100.References.All.Concat(assemblies.Select(a => MetadataReference.CreateFromFile(a.Location)))); var generator = new SyncMethodSourceGenerator();