Skip to content

Commit

Permalink
More closely emulate NuGet package import and add DebugType tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKotsenas committed Aug 27, 2024
1 parent 8010934 commit b195e98
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DotNet.ReproducibleBuilds.Tests;

internal static class FileSystemInfoExtensions
{
public static string Combine(this FileSystemInfo info, params string[] paths)
{
return Path.Combine([info.FullName, ..paths]);
}
}
20 changes: 13 additions & 7 deletions tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ 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)
public static ProjectCreator ReproducibleBuildProject(this ProjectCreatorTemplates templates, DirectoryInfo path)
{
ProjectCreator template = ProjectCreator.Templates
.SdkCsproj(path: Path.Combine(directory, "test.csproj"), targetFramework: "net8.0")
.Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.props"));
FileInfo project = new(path.Combine("test.csproj"));

configure(template);
_ = ProjectCreator
.Create(path: path.Combine("obj", $"{project.Name}.tests.g.props"))
.Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.props"))
.Save();

return template
.Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.targets"));
_ = ProjectCreator
.Create(path: path.Combine("obj", $"{project.Name}.tests.g.targets"))
.Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.targets"))
.Save();

return templates
.SdkCsproj(path: project.FullName, targetFramework: "net8.0");
}
}
26 changes: 20 additions & 6 deletions tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@ public class SourceLinkTests : TestBase
[InlineData(true, true)]
public void PublishRepositoryUrlIsSet(bool? publishRepositoryUrl, bool expected)
{
ProjectCreator.Templates.ReproducibleBuildProject(TestRootPath, project =>
{
project
.PropertyGroup()
.Property("PublishRepositoryUrl", publishRepositoryUrl.ToLowerInvariant());
}).Project
ProjectCreator.Templates
.ReproducibleBuildProject(TestRootPath)
.PropertyGroup()
.Property("PublishRepositoryUrl", publishRepositoryUrl.ToLowerInvariant())
.Project
.GetPropertyValue("PublishRepositoryUrl")
.Should().Be(expected.ToLowerInvariant());
}

[Theory]
[InlineData(null, "embedded")]
[InlineData("embedded", "embedded")]
[InlineData("portable", "portable")]
public void DebugTypeIsSet(string? debugType, string expected)
{
ProjectCreator.Templates
.ReproducibleBuildProject(TestRootPath)
.PropertyGroup()
.Property("DebugType", debugType)
.Project
.GetPropertyValue("DebugType")
.Should().Be(expected);
}
}
22 changes: 6 additions & 16 deletions tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,30 @@ public abstract class TestBase : IDisposable
{
protected TestBase()
{
TestRootPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())).FullName;
TestRootPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
}

public string TestRootPath { get; }
public DirectoryInfo TestRootPath { get; }

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool isDisposing)
{
if (Directory.Exists(TestRootPath))
TestRootPath.Refresh();
if (TestRootPath.Exists)
{
try
{
Directory.Delete(TestRootPath, recursive: true);
TestRootPath.Delete(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 b195e98

Please sign in to comment.