diff --git a/tests/DotNet.ReproducibleBuilds.Tests/FileSystemInfoExtensions.cs b/tests/DotNet.ReproducibleBuilds.Tests/FileSystemInfoExtensions.cs new file mode 100644 index 0000000..7abcbb7 --- /dev/null +++ b/tests/DotNet.ReproducibleBuilds.Tests/FileSystemInfoExtensions.cs @@ -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]); + } +} diff --git a/tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs b/tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs index ad9e15a..d2f7ff2 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs @@ -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 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"); } } diff --git a/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs b/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs index 44c9747..8084dab 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs @@ -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); + } } diff --git a/tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs b/tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs index 2c0003b..acf8df4 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs @@ -4,23 +4,25 @@ 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) { @@ -28,16 +30,4 @@ protected virtual void Dispose(bool isDisposing) } } } - - 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}"); - } }