From b18402c5a1ce0392d20a9324c55cec12402c3531 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Mon, 26 Aug 2024 16:34:08 -0700 Subject: [PATCH] Add tests for CI providers to RepositoryBranch --- .../EnvironmentVariableSuppressor.cs | 19 ++++++ .../ProjectTemplates.cs | 8 +-- .../SourceLinkTests.cs | 59 ++++++++++++++++++- .../TestBase.cs | 5 ++ 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 tests/DotNet.ReproducibleBuilds.Tests/EnvironmentVariableSuppressor.cs diff --git a/tests/DotNet.ReproducibleBuilds.Tests/EnvironmentVariableSuppressor.cs b/tests/DotNet.ReproducibleBuilds.Tests/EnvironmentVariableSuppressor.cs new file mode 100644 index 0000000..0f62f24 --- /dev/null +++ b/tests/DotNet.ReproducibleBuilds.Tests/EnvironmentVariableSuppressor.cs @@ -0,0 +1,19 @@ +namespace DotNet.ReproducibleBuilds.Tests; + +internal sealed class EnvironmentVariableSuppressor : IDisposable +{ + private readonly string? _value; + private readonly string _name; + + public EnvironmentVariableSuppressor(string name) + { + _name = name; + _value = Environment.GetEnvironmentVariable(name); + Environment.SetEnvironmentVariable(name, null); + } + + public void Dispose() + { + Environment.SetEnvironmentVariable(_name, _value); + } +} diff --git a/tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs b/tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs index d2f7ff2..d2ebecb 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/ProjectTemplates.cs @@ -6,17 +6,17 @@ internal static class ProjectTemplates { private static readonly string ThisAssemblyDirectory = Path.GetDirectoryName(typeof(ProjectTemplates).Assembly.Location)!; - public static ProjectCreator ReproducibleBuildProject(this ProjectCreatorTemplates templates, DirectoryInfo path) + public static ProjectCreator ReproducibleBuildProject(this ProjectCreatorTemplates templates, FileInfo project) { - FileInfo project = new(path.Combine("test.csproj")); + DirectoryInfo directory = project.Directory ?? throw new ArgumentException("Project's path does not appear to have a parent.", nameof(project)); _ = ProjectCreator - .Create(path: path.Combine("obj", $"{project.Name}.tests.g.props")) + .Create(path: directory.Combine("obj", $"{project.Name}.tests.g.props")) .Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.props")) .Save(); _ = ProjectCreator - .Create(path: path.Combine("obj", $"{project.Name}.tests.g.targets")) + .Create(path: directory.Combine("obj", $"{project.Name}.tests.g.targets")) .Import(Path.Combine(ThisAssemblyDirectory, "DotNet.ReproducibleBuilds.targets")) .Save(); diff --git a/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs b/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs index 337fdbb..d46c7f9 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs @@ -12,7 +12,7 @@ public class SourceLinkTests : TestBase public void PublishRepositoryUrlIsSet(bool? publishRepositoryUrl, bool expected) { ProjectCreator.Templates - .ReproducibleBuildProject(TestRootPath) + .ReproducibleBuildProject(GetRandomFile(".csproj")) .PropertyGroup() .Property("PublishRepositoryUrl", publishRepositoryUrl.ToLowerInvariant()) .Project @@ -27,7 +27,7 @@ public void PublishRepositoryUrlIsSet(bool? publishRepositoryUrl, bool expected) public void DebugTypeIsSet(string? debugType, string expected) { ProjectCreator.Templates - .ReproducibleBuildProject(TestRootPath) + .ReproducibleBuildProject(GetRandomFile(".csproj")) .PropertyGroup() .Property("DebugType", debugType) .Project @@ -42,11 +42,64 @@ public void DebugTypeIsSet(string? debugType, string expected) public void EmbedUntrackedSourcesIsSet(bool? embedUntrackedSources, bool expected) { ProjectCreator.Templates - .ReproducibleBuildProject(TestRootPath) + .ReproducibleBuildProject(GetRandomFile(".csproj")) .PropertyGroup() .Property("PublishRepositoryUrl", embedUntrackedSources.ToLowerInvariant()) .Project .GetPropertyValue("PublishRepositoryUrl") .Should().Be(expected.ToLowerInvariant()); } + + [Theory] + [InlineData("GITHUB_REF", "refs/pull/1234/merge", "pr1234")] + [InlineData("GITHUB_REF", "refs/heads/my-branch", "my-branch")] + [InlineData("GITHUB_REF", "refs/tags/v1.2.3", "v1.2.3")] + + [InlineData("BUILD_SOURCEBRANCH", "refs/heads/my-branch", "my-branch")] + [InlineData("BUILD_SOURCEBRANCH", "refs/tags/v1.2.3", "v1.2.3")] + + [InlineData("APPVEYOR_PULL_REQUEST_NUMBER", "1234", "pr1234")] + [InlineData("APPVEYOR_REPO_TAG_NAME", "refs/tags/v1.2.3", "refs/tags/v1.2.3")] + [InlineData("APPVEYOR_REPO_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")] + + [InlineData("TEAMCITY_BUILD_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")] + + [InlineData("TRAVIS_PULL_REQUEST", "1234", "pr1234")] + [InlineData("TRAVIS_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")] + + [InlineData("CIRCLE_PR_NUMBER", "1234", "pr1234")] + [InlineData("CIRCLE_TAG", "refs/heads/v1.2.3", "refs/heads/v1.2.3")] + [InlineData("CIRCLE_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")] + + [InlineData("CI_COMMIT_TAG", "refs/tags/v1.2.3", "refs/tags/v1.2.3")] + [InlineData("CI_MERGE_REQUEST_IID", "1234", "pr1234")] + [InlineData("CI_COMMIT_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")] + + [InlineData("BUDDY_EXECUTION_PULL_REQUEST_NO", "1234", "pr1234")] + [InlineData("BUDDY_EXECUTION_TAG", "refs/tags/v1.2.3", "refs/tags/v1.2.3")] + [InlineData("BUDDY_EXECUTION_BRANCH", "refs/heads/my-branch", "refs/heads/my-branch")] + public void RepositoryBranchIsSet(string ci, string original, string expected) + { + using EnvironmentVariableSuppressor hostSuppressor = new("BUILD_SOURCEBRANCH"); // Suppress our own CI provider variables (i.e. Azure DevOps) + using EnvironmentVariableSuppressor ciSuppressor = new(ci); // Suppress the mock CI provider (just in case). + + // If RepositoryBranch is set, it should take precedence over the CI provider variables + ProjectCreator.Templates + .ReproducibleBuildProject(GetRandomFile(".csproj")) + .PropertyGroup() + .Property("RepositoryBranch", "explicitly-set") + .Property(ci, original) + .Project + .GetPropertyValue("RepositoryBranch") + .Should().Be("explicitly-set", "because explicitly setting `RepositoryBranch` should always win."); + + // If RepositoryBranch is not set, it should be set from the CI provider property + ProjectCreator.Templates + .ReproducibleBuildProject(GetRandomFile(".csproj")) + .PropertyGroup() + .Property(ci, original) + .Project + .GetPropertyValue("RepositoryBranch") + .Should().Be(expected); + } } diff --git a/tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs b/tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs index acf8df4..b1599f4 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/TestBase.cs @@ -30,4 +30,9 @@ protected virtual void Dispose(bool isDisposing) } } } + + protected FileInfo GetRandomFile(string? extension = null) + { + return new(TestRootPath.Combine($"{Path.GetRandomFileName()}{extension ?? string.Empty}")); + } }