generated from dotnet/new-repo
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MSBuildProjectCreator test harness
- Loading branch information
1 parent
97f3184
commit 8010934
Showing
9 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/DotNet.ReproducibleBuilds.Tests/DotNet.ReproducibleBuilds.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
tests/DotNet.ReproducibleBuilds.Tests/MSBuildModuleInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |