Skip to content

Commit

Permalink
Add MSBuildProjectCreator test harness
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKotsenas committed Aug 27, 2024
1 parent 97f3184 commit 8010934
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Project>
<PropertyGroup>
<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>

<!-- Default to not packaging a project. Override per-project where dotnet sdk nuget packaging
is desired. -->
<IsPackable Condition="'$(IsPackable)' == ''">false</IsPackable>
Expand Down Expand Up @@ -36,6 +38,7 @@

<Target
Name="PreparePackageReleaseNotesFromFiles"
Condition="'$(IsPackable)' == 'true'"
BeforeTargets="GenerateNuspec">
<PropertyGroup>
<!-- This path will be relative to each executing project -->
Expand Down
6 changes: 6 additions & 0 deletions DotNet.ReproducibleBuilds.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNet.ReproducibleBuilds.Isolated", "src\DotNet.ReproducibleBuilds.Isolated\DotNet.ReproducibleBuilds.Isolated.csproj", "{BD88D2CB-4342-47A3-B0D1-07321E9A92C1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNet.ReproducibleBuilds.Tests", "tests\DotNet.ReproducibleBuilds.Tests\DotNet.ReproducibleBuilds.Tests.csproj", "{72BE4FA4-D190-44AF-B056-23AA79D1553A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -31,6 +33,10 @@ Global
{BD88D2CB-4342-47A3-B0D1-07321E9A92C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD88D2CB-4342-47A3-B0D1-07321E9A92C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD88D2CB-4342-47A3-B0D1-07321E9A92C1}.Release|Any CPU.Build.0 = Release|Any CPU
{72BE4FA4-D190-44AF-B056-23AA79D1553A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72BE4FA4-D190-44AF-B056-23AA79D1553A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72BE4FA4-D190-44AF-B056-23AA79D1553A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72BE4FA4-D190-44AF-B056-23AA79D1553A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ stages:
- script: dotnet nbgv cloud
displayName: Set Version

- script: dotnet test dirs.proj
displayName: Test

- script: dotnet pack dirs.proj
displayName: Create package(s)

Expand Down
9 changes: 9 additions & 0 deletions tests/DotNet.ReproducibleBuilds.Tests/BooleanExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Diagnostics.CodeAnalysis;

namespace DotNet.ReproducibleBuilds.Tests;

internal static class BooleanExtensions
{
public static string ToLowerInvariant(this bool value) => value.ToString().ToLowerInvariant();
public static string? ToLowerInvariant([NotNullIfNotNull(nameof(value))] this bool? value) => value?.ToString().ToLowerInvariant();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="MSBuild.ProjectCreation" Version="12.0.1" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Include="$(RepoRoot)/src/DotNet.ReproducibleBuilds/*.props" CopyToOutputDirectory="PreserveNewest" />
<None Include="$(RepoRoot)/src/DotNet.ReproducibleBuilds/*.targets" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions tests/DotNet.ReproducibleBuilds.Tests/MSBuildModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Build.Utilities.ProjectCreation;
using System.Runtime.CompilerServices;

namespace DotNet.ReproducibleBuilds.Tests;

internal static class MSBuildModuleInitializer
{
[ModuleInitializer]
internal static void InitializeMSBuild()
{
MSBuildAssemblyResolver.Register();
}
}
20 changes: 20 additions & 0 deletions tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Build.Utilities.ProjectCreation;

namespace DotNet.ReproducibleBuilds.Tests;

internal static class ProjectTemplates
{
private static readonly string ThisAssemblyDirectory = Path.GetDirectoryName(typeof(ProjectTemplates).Assembly.Location)!;

public static ProjectCreator ReproducibleBuildProject(this ProjectCreatorTemplates templates, string directory, Action<ProjectCreator> configure)
{
ProjectCreator template = ProjectCreator.Templates
.SdkCsproj(path: Path.Combine(directory, "test.csproj"), targetFramework: "net8.0")
.Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.props"));

configure(template);

return template
.Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.targets"));
}
}
23 changes: 23 additions & 0 deletions tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FluentAssertions;
using Microsoft.Build.Utilities.ProjectCreation;

namespace DotNet.ReproducibleBuilds.Tests;

public class SourceLinkTests : TestBase
{
[Theory]
[InlineData(null, true)]
[InlineData(false, false)]
[InlineData(true, true)]
public void PublishRepositoryUrlIsSet(bool? publishRepositoryUrl, bool expected)
{
ProjectCreator.Templates.ReproducibleBuildProject(TestRootPath, project =>
{
project
.PropertyGroup()
.Property("PublishRepositoryUrl", publishRepositoryUrl.ToLowerInvariant());
}).Project
.GetPropertyValue("PublishRepositoryUrl")
.Should().Be(expected.ToLowerInvariant());
}
}
43 changes: 43 additions & 0 deletions tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace DotNet.ReproducibleBuilds.Tests;

public abstract class TestBase : IDisposable
{
protected TestBase()
{
TestRootPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())).FullName;
}

public string TestRootPath { get; }

public void Dispose()
{
Dispose(true);
}

protected virtual void Dispose(bool isDisposing)
{
if (Directory.Exists(TestRootPath))
{
try
{
Directory.Delete(TestRootPath, recursive: true);
}
catch (Exception)
{
// Ignored
}
}
}

protected string GetTempFileName(string? extension = null)
{
return Path.Combine(TestRootPath, $"{Path.GetRandomFileName()}{extension ?? string.Empty}");
}

protected string GetTempProjectPath(string? extension = null)
{
DirectoryInfo tempDirectoryInfo = Directory.CreateDirectory(Path.Combine(TestRootPath, Path.GetRandomFileName()));

return Path.Combine(tempDirectoryInfo.FullName, $"{Path.GetRandomFileName()}{extension ?? string.Empty}");
}
}

0 comments on commit 8010934

Please sign in to comment.