Skip to content

Commit

Permalink
Merge pull request #153 from leancodepl/enable-out-of-repo-tests
Browse files Browse the repository at this point in the history
Add ability to not rely on custom build of `LeanCode.Contracts` in tests
  • Loading branch information
jakubfijalkowski authored Sep 27, 2023
2 parents c0227b1 + 785b3d2 commit e253815
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 19 deletions.
20 changes: 20 additions & 0 deletions examples/project/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project>

<!--
To enable out-of-this-repository tests, we need to rely on internally-built contracts only during our tests.
If anyone else wants to run these, they should be able to do so with a public NuGet package.
Since we don't really want to change generator API just for tests (which would be quite painful even if we wanted
to), we rely on environment variables that mark that we are running in the inner test suite and we have the
`LeanCode.Contracts` package already built.
-->

<ItemGroup Condition=" '$(UseTestBuildOfContracts)' == 'true' ">
<PackageReference Update="LeanCode.Contracts" Version="9.9.9.9-internal" />
</ItemGroup>

<ItemGroup Condition=" '$(UseTestBuildOfContracts)' != 'true' ">
<PackageReference Update="LeanCode.Contracts" Version="2.0.0-preview.3" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions examples/project/aggregated/A/A.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LeanCode.Contracts" Version="9.9.9.9-internal" />
<PackageReference Include="LeanCode.Contracts" />
</ItemGroup>

</Project>
</Project>
4 changes: 2 additions & 2 deletions examples/project/aggregated/B/B.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LeanCode.Contracts" Version="9.9.9.9-internal" />
<PackageReference Include="LeanCode.Contracts" />
</ItemGroup>

</Project>
</Project>
4 changes: 2 additions & 2 deletions examples/project/aggregated/C/C.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LeanCode.Contracts" Version="9.9.9.9-internal" />
<PackageReference Include="LeanCode.Contracts" />
</ItemGroup>

</Project>
</Project>
2 changes: 1 addition & 1 deletion examples/project/implicitusings/implicitusings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LeanCode.Contracts" Version="9.9.9.9-internal" />
<PackageReference Include="LeanCode.Contracts" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions examples/project/packagereference/packagereference.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LeanCode.Contracts" Version="9.9.9.9-internal" />
<PackageReference Include="LeanCode.Contracts" />
<PackageReference Include="Dapper" Version="2.0.123" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</Target>

<ItemGroup>
<PackageReference Include="LeanCode.Contracts" Version="9.9.9.9-internal" />
<PackageReference Include="LeanCode.Contracts" />
<PackageReference Include="embedded" Version="8.8.8.8-internal" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions examples/project/single/single.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LeanCode.Contracts" Version="9.9.9.9-internal" />
<PackageReference Include="LeanCode.Contracts" />
</ItemGroup>

</Project>
</Project>
12 changes: 11 additions & 1 deletion src/LeanCode.ContractsGenerator.Tests/ExampleBasedHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
using System.Collections.Immutable;
using LeanCode.ContractsGenerator.Compilation;
using Microsoft.Extensions.FileSystemGlobbing;

namespace LeanCode.ContractsGenerator.Tests;

public static class ExampleBasedHelpers
{
private static readonly ImmutableDictionary<string, string> TestProjectProperties =
ImmutableDictionary.CreateRange(
new Dictionary<string, string>
{
// 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));
Expand Down Expand Up @@ -32,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(
Expand Down
14 changes: 12 additions & 2 deletions src/LeanCode.ContractsGenerator/Compilation/ContractsCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,22 @@ private static bool IsWantedDefaultAssembly(CompilationLibrary cl)
.Select(path => MetadataReference.CreateFromFile(path))
.ToImmutableList();

public static Task<(CompiledContracts Compiled, List<Export> External)> CompileProjectsAsync(
IEnumerable<string> projectPaths
)
{
return CompileProjectsAsync(projectPaths, ImmutableDictionary<string, string>.Empty);
}

public static async Task<(
CompiledContracts Compiled,
List<Export> External
)> CompileProjectsAsync(IEnumerable<string> projectPaths)
)> CompileProjectsAsync(
IEnumerable<string> projectPaths,
ImmutableDictionary<string, string> properties
)
{
using var loader = new ProjectLoader();
using var loader = new ProjectLoader(properties);
await loader.LoadProjectsAsync(projectPaths);
var compilations = await loader.CompileAsync();
var compiledContracts = Compile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ static MSBuildHelper()
}
}

public static MSBuildWorkspace CreateWorkspace()
public static MSBuildWorkspace CreateWorkspace(ImmutableDictionary<string, string> properties)
{
return MSBuildWorkspace.Create(GlobalProperties);
var finalProps = properties.AddRange(GlobalProperties);
return MSBuildWorkspace.Create(finalProps);
}

public static int RestoreProjects(IReadOnlyCollection<string> projectPaths)
Expand Down
9 changes: 7 additions & 2 deletions src/LeanCode.ContractsGenerator/Compilation/ProjectLoader.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,8 +8,13 @@ namespace LeanCode.ContractsGenerator.Compilation;

public sealed class ProjectLoader : IDisposable
{
private readonly MSBuildWorkspace msbuildWorkspace = MSBuildHelper.CreateWorkspace();
private readonly List<Project> projects = new();
private readonly MSBuildWorkspace msbuildWorkspace;

public ProjectLoader(ImmutableDictionary<string, string> properties)
{
msbuildWorkspace = MSBuildHelper.CreateWorkspace(properties);
}

public async Task LoadProjectsAsync(IEnumerable<string> projectPaths)
{
Expand Down

0 comments on commit e253815

Please sign in to comment.