From e2f5ec12637933704a72d832ef4998ed9aa49cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Fija=C5=82kowski?= Date: Tue, 26 Sep 2023 17:13:04 +0200 Subject: [PATCH 1/2] Add ability to not rely on custom build of `LeanCode.Contracts` in tests --- examples/project/Directory.Build.targets | 20 +++++++++++++++++++ examples/project/aggregated/A/A.csproj | 4 ++-- examples/project/aggregated/B/B.csproj | 4 ++-- examples/project/aggregated/C/C.csproj | 4 ++-- .../implicitusings/implicitusings.csproj | 2 +- .../packagereference/packagereference.csproj | 4 ++-- .../referencetoembedded.csproj | 2 +- examples/project/single/single.csproj | 4 ++-- .../ExampleBasedHelpers.cs | 3 +++ 9 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 examples/project/Directory.Build.targets diff --git a/examples/project/Directory.Build.targets b/examples/project/Directory.Build.targets new file mode 100644 index 0000000..263cc40 --- /dev/null +++ b/examples/project/Directory.Build.targets @@ -0,0 +1,20 @@ + + + + + + + + + + + + + diff --git a/examples/project/aggregated/A/A.csproj b/examples/project/aggregated/A/A.csproj index c40addb..5ec8e50 100644 --- a/examples/project/aggregated/A/A.csproj +++ b/examples/project/aggregated/A/A.csproj @@ -5,7 +5,7 @@ - + - \ No newline at end of file + diff --git a/examples/project/aggregated/B/B.csproj b/examples/project/aggregated/B/B.csproj index c40addb..5ec8e50 100644 --- a/examples/project/aggregated/B/B.csproj +++ b/examples/project/aggregated/B/B.csproj @@ -5,7 +5,7 @@ - + - \ No newline at end of file + diff --git a/examples/project/aggregated/C/C.csproj b/examples/project/aggregated/C/C.csproj index c40addb..5ec8e50 100644 --- a/examples/project/aggregated/C/C.csproj +++ b/examples/project/aggregated/C/C.csproj @@ -5,7 +5,7 @@ - + - \ No newline at end of file + diff --git a/examples/project/implicitusings/implicitusings.csproj b/examples/project/implicitusings/implicitusings.csproj index 2f7b07b..c537117 100644 --- a/examples/project/implicitusings/implicitusings.csproj +++ b/examples/project/implicitusings/implicitusings.csproj @@ -6,7 +6,7 @@ - + diff --git a/examples/project/packagereference/packagereference.csproj b/examples/project/packagereference/packagereference.csproj index 29408d0..658effb 100644 --- a/examples/project/packagereference/packagereference.csproj +++ b/examples/project/packagereference/packagereference.csproj @@ -5,8 +5,8 @@ - + - \ No newline at end of file + diff --git a/examples/project/referencetoembedded/referencetoembedded.csproj b/examples/project/referencetoembedded/referencetoembedded.csproj index d42ed68..5ac281f 100644 --- a/examples/project/referencetoembedded/referencetoembedded.csproj +++ b/examples/project/referencetoembedded/referencetoembedded.csproj @@ -10,7 +10,7 @@ - + diff --git a/examples/project/single/single.csproj b/examples/project/single/single.csproj index c40addb..5ec8e50 100644 --- a/examples/project/single/single.csproj +++ b/examples/project/single/single.csproj @@ -5,7 +5,7 @@ - + - \ No newline at end of file + diff --git a/src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs b/src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs index 512133e..c086a1f 100644 --- a/src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs +++ b/src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs @@ -24,6 +24,9 @@ public static AssertedErrors AnalyzeFails(this string path) public static AssertedExport ProjectCompiles(this string path) { + // See `/examples/Directory.Build.targets` for an explanation why we need to set the variable + Environment.SetEnvironmentVariable("UseTestBuildOfContracts", "true"); + return ProjectsCompile(path); } From 785b3d2f4c0aac5d9d442a7b93ccc3eb9d136f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Fija=C5=82kowski?= Date: Wed, 27 Sep 2023 10:09:57 +0200 Subject: [PATCH 2/2] Switch from env variables to proper MSBuild properties --- .../ExampleBasedHelpers.cs | 15 +++++++++++---- .../Compilation/ContractsCompiler.cs | 14 ++++++++++++-- .../Compilation/MSBuild/MSBuildHelper.cs | 5 +++-- .../Compilation/ProjectLoader.cs | 9 +++++++-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs b/src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs index c086a1f..5669640 100644 --- a/src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs +++ b/src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs @@ -1,3 +1,4 @@ +using System.Collections.Immutable; using LeanCode.ContractsGenerator.Compilation; using Microsoft.Extensions.FileSystemGlobbing; @@ -5,6 +6,15 @@ namespace LeanCode.ContractsGenerator.Tests; public static class ExampleBasedHelpers { + private static readonly ImmutableDictionary TestProjectProperties = + ImmutableDictionary.CreateRange( + new Dictionary + { + // See `/examples/project/Directory.Build.targets` for an explanation why we need to set the variable + ["UseTestBuildOfContracts"] = "true", + } + ); + public static AssertedExport Compiles(this string path) { var code = File.ReadAllText(Path.Join("examples", path)); @@ -24,9 +34,6 @@ public static AssertedErrors AnalyzeFails(this string path) public static AssertedExport ProjectCompiles(this string path) { - // See `/examples/Directory.Build.targets` for an explanation why we need to set the variable - Environment.SetEnvironmentVariable("UseTestBuildOfContracts", "true"); - return ProjectsCompile(path); } @@ -35,7 +42,7 @@ public static AssertedExport ProjectsCompile(params string[] paths) var projectPaths = paths.Select(p => Path.Join("examples", p)); // HACK: The sync execution results in much cleaner tests var (compiled, external) = ContractsCompiler - .CompileProjectsAsync(projectPaths) + .CompileProjectsAsync(projectPaths, TestProjectProperties) .GetAwaiter() .GetResult(); return new( diff --git a/src/LeanCode.ContractsGenerator/Compilation/ContractsCompiler.cs b/src/LeanCode.ContractsGenerator/Compilation/ContractsCompiler.cs index 8c5bbda..f013533 100644 --- a/src/LeanCode.ContractsGenerator/Compilation/ContractsCompiler.cs +++ b/src/LeanCode.ContractsGenerator/Compilation/ContractsCompiler.cs @@ -80,12 +80,22 @@ private static bool IsWantedDefaultAssembly(CompilationLibrary cl) .Select(path => MetadataReference.CreateFromFile(path)) .ToImmutableList(); + public static Task<(CompiledContracts Compiled, List External)> CompileProjectsAsync( + IEnumerable projectPaths + ) + { + return CompileProjectsAsync(projectPaths, ImmutableDictionary.Empty); + } + public static async Task<( CompiledContracts Compiled, List External - )> CompileProjectsAsync(IEnumerable projectPaths) + )> CompileProjectsAsync( + IEnumerable projectPaths, + ImmutableDictionary properties + ) { - using var loader = new ProjectLoader(); + using var loader = new ProjectLoader(properties); await loader.LoadProjectsAsync(projectPaths); var compilations = await loader.CompileAsync(); var compiledContracts = Compile( diff --git a/src/LeanCode.ContractsGenerator/Compilation/MSBuild/MSBuildHelper.cs b/src/LeanCode.ContractsGenerator/Compilation/MSBuild/MSBuildHelper.cs index f9d3149..eac3f30 100644 --- a/src/LeanCode.ContractsGenerator/Compilation/MSBuild/MSBuildHelper.cs +++ b/src/LeanCode.ContractsGenerator/Compilation/MSBuild/MSBuildHelper.cs @@ -50,9 +50,10 @@ static MSBuildHelper() } } - public static MSBuildWorkspace CreateWorkspace() + public static MSBuildWorkspace CreateWorkspace(ImmutableDictionary properties) { - return MSBuildWorkspace.Create(GlobalProperties); + var finalProps = properties.AddRange(GlobalProperties); + return MSBuildWorkspace.Create(finalProps); } public static int RestoreProjects(IReadOnlyCollection projectPaths) diff --git a/src/LeanCode.ContractsGenerator/Compilation/ProjectLoader.cs b/src/LeanCode.ContractsGenerator/Compilation/ProjectLoader.cs index 95f793f..c18a920 100644 --- a/src/LeanCode.ContractsGenerator/Compilation/ProjectLoader.cs +++ b/src/LeanCode.ContractsGenerator/Compilation/ProjectLoader.cs @@ -1,5 +1,5 @@ +using System.Collections.Immutable; using LeanCode.ContractsGenerator.Compilation.MSBuild; -using Microsoft.Build.Logging; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.MSBuild; @@ -8,8 +8,13 @@ namespace LeanCode.ContractsGenerator.Compilation; public sealed class ProjectLoader : IDisposable { - private readonly MSBuildWorkspace msbuildWorkspace = MSBuildHelper.CreateWorkspace(); private readonly List projects = new(); + private readonly MSBuildWorkspace msbuildWorkspace; + + public ProjectLoader(ImmutableDictionary properties) + { + msbuildWorkspace = MSBuildHelper.CreateWorkspace(properties); + } public async Task LoadProjectsAsync(IEnumerable projectPaths) {