diff --git a/src/DotNet.ReproducibleBuilds/DotNet.ReproducibleBuilds.targets b/src/DotNet.ReproducibleBuilds/DotNet.ReproducibleBuilds.targets index b80ace4..0e7acc3 100644 --- a/src/DotNet.ReproducibleBuilds/DotNet.ReproducibleBuilds.targets +++ b/src/DotNet.ReproducibleBuilds/DotNet.ReproducibleBuilds.targets @@ -15,10 +15,9 @@ - pr$(GITHUB_REF.Replace('refs/pull/', '').Replace('/merge', '')) - $(GITHUB_REF.Replace('refs/heads/', '').Replace('refs/tags/', '')) + $(GITHUB_REF) - $(BUILD_SOURCEBRANCH.Replace('refs/heads/', '').Replace('refs/tags/', '')) + $(BUILD_SOURCEBRANCH) pr$(APPVEYOR_PULL_REQUEST_NUMBER) $(APPVEYOR_REPO_TAG_NAME) @@ -26,21 +25,21 @@ $(TEAMCITY_BUILD_BRANCH) - pr$(TRAVIS_PULL_REQUEST) $(TRAVIS_BRANCH) + pr$(TRAVIS_PULL_REQUEST) - pr$(CIRCLE_PR_NUMBER) $(CIRCLE_TAG) $(CIRCLE_BRANCH) + pr$(CIRCLE_PR_NUMBER) $(CI_COMMIT_TAG) + $(CI_COMMIT_BRANCH) pr$(CI_MERGE_REQUEST_IID) pr$(CI_EXTERNAL_PULL_REQUEST_IID) - $(CI_COMMIT_BRANCH) - pr$(BUDDY_EXECUTION_PULL_REQUEST_NO) $(BUDDY_EXECUTION_TAG) $(BUDDY_EXECUTION_BRANCH) + pr$(BUDDY_EXECUTION_PULL_REQUEST_NO) diff --git a/tests/DotNet.ReproducibleBuilds.Tests/CollectionExtensions.cs b/tests/DotNet.ReproducibleBuilds.Tests/CollectionExtensions.cs new file mode 100644 index 0000000..404b0f0 --- /dev/null +++ b/tests/DotNet.ReproducibleBuilds.Tests/CollectionExtensions.cs @@ -0,0 +1,6 @@ +namespace DotNet.ReproducibleBuilds.Tests; + +internal static class CollectionExtensions +{ + public static IDisposable ToDisposable(this IEnumerable disposables) => new DisposableCollection(disposables); +} diff --git a/tests/DotNet.ReproducibleBuilds.Tests/ContinuousIntegrationTests.cs b/tests/DotNet.ReproducibleBuilds.Tests/ContinuousIntegrationTests.cs index b9873a0..a6ac54a 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/ContinuousIntegrationTests.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/ContinuousIntegrationTests.cs @@ -26,7 +26,7 @@ public void RespectsSetValue(bool? value, string expected) [Theory] [MemberData(nameof(MemberData))] - public void RespectsGlobalProperites(Dictionary envVars) + public void RespectsGlobalProperties(Dictionary envVars) { using EnvironmentVariableSuppressor hostSuppressor = new("TF_BUILD"); // Suppress our own CI provider variables (i.e. Azure DevOps) diff --git a/tests/DotNet.ReproducibleBuilds.Tests/DisposableCollection.cs b/tests/DotNet.ReproducibleBuilds.Tests/DisposableCollection.cs new file mode 100644 index 0000000..5da02cf --- /dev/null +++ b/tests/DotNet.ReproducibleBuilds.Tests/DisposableCollection.cs @@ -0,0 +1,18 @@ +namespace DotNet.ReproducibleBuilds.Tests; + +internal sealed class DisposableCollection : IDisposable +{ + private readonly List _disposables = []; + + public DisposableCollection(IEnumerable disposables) => _disposables.AddRange(disposables); + + public void Add(IDisposable disposable) => _disposables.Add(disposable); + + public void Dispose() + { + foreach (IDisposable disposable in _disposables) + { + disposable.Dispose(); + } + } +} diff --git a/tests/DotNet.ReproducibleBuilds.Tests/ProjectCreatorExtensions.cs b/tests/DotNet.ReproducibleBuilds.Tests/ProjectCreatorExtensions.cs index d486a60..9d27a14 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/ProjectCreatorExtensions.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/ProjectCreatorExtensions.cs @@ -11,4 +11,14 @@ public static Project ProjectWithGlobalProperties(this ProjectCreator creator, I return project; } + + public static ProjectCreator Properties(this ProjectCreator creator, IEnumerable> properties) + { + foreach (KeyValuePair property in properties) + { + creator.Property(property.Key, property.Value); + } + + return creator; + } } diff --git a/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs b/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs index 75554f7..f95f68b 100644 --- a/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs +++ b/tests/DotNet.ReproducibleBuilds.Tests/SourceLinkTests.cs @@ -51,42 +51,16 @@ public void EmbedUntrackedSourcesIsSet(bool? embedUntrackedSources, bool expecte } [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) + [MemberData(nameof(RepositoryBranchData))] + public void RepositoryBranchIsSet(Dictionary env, 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) + using IDisposable ciSuppressors = env.Select(kvp => new EnvironmentVariableSuppressor(kvp.Key)).ToDisposable(); // Suppress the mock CI provider (just in case) ProjectCreator project = ProjectCreator.Templates .ReproducibleBuildProject(GetRandomFile(".csproj")) .PropertyGroup() - .Property(ci, original); + .Properties(env); // If RepositoryBranch is not set, it should be set from the CI provider property project.Project @@ -99,4 +73,65 @@ public void RepositoryBranchIsSet(string ci, string original, string expected) .GetPropertyValue("RepositoryBranch") .Should().Be("explicitly-set", "because explicitly setting `RepositoryBranch` should always win."); } + + public static TheoryData, string> RepositoryBranchData() + { + TheoryData, string> data = new() + { + { new() { ["GITHUB_REF"] = "refs/pull/1234/merge" }, "refs/pull/1234/merge" }, + { new() { ["GITHUB_REF"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["GITHUB_REF"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + + { new() { ["BUILD_SOURCEBRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["BUILD_SOURCEBRANCH"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + + { new() { ["APPVEYOR_PULL_REQUEST_NUMBER"] = "1234" }, "pr1234" }, + { new() { ["APPVEYOR_REPO_TAG_NAME"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + { new() { ["APPVEYOR_REPO_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["APPVEYOR_PULL_REQUEST_NUMBER"] = "1234" , ["APPVEYOR_REPO_BRANCH"] = "refs/heads/my-branch" }, "pr1234" }, + { new() { ["APPVEYOR_REPO_TAG_NAME"] = "refs/tags/v1.2.3" , ["APPVEYOR_REPO_BRANCH"] = "refs/heads/my-branch" }, "refs/tags/v1.2.3" }, + { new() { ["APPVEYOR_PULL_REQUEST_NUMBER"] = "1234", ["APPVEYOR_REPO_TAG_NAME"] = "refs/tags/v1.2.3", ["APPVEYOR_REPO_BRANCH"] = "refs/heads/my-branch" }, "pr1234" }, + + { new() { ["TEAMCITY_BUILD_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + + { new() { ["TRAVIS_PULL_REQUEST"] = "1234" }, "pr1234" }, + { new() { ["TRAVIS_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["TRAVIS_PULL_REQUEST"] = "1234", ["TRAVIS_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["TRAVIS_PULL_REQUEST"] = "false", ["TRAVIS_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + + { new() { ["CIRCLE_PR_NUMBER"] = "1234" }, "pr1234" }, + { new() { ["CIRCLE_TAG"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + { new() { ["CIRCLE_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["CIRCLE_PR_NUMBER"] = "1234", ["CIRCLE_TAG"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + { new() { ["CIRCLE_PR_NUMBER"] = "1234", ["CIRCLE_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["CIRCLE_TAG"] = "refs/tags/v1.2.3", ["CIRCLE_BRANCH"] = "refs/heads/my-branch" }, "refs/tags/v1.2.3" }, + { new() { ["CIRCLE_PR_NUMBER"] = "1234", ["CIRCLE_TAG"] = "refs/tags/v1.2.3", ["CIRCLE_BRANCH"] = "refs/heads/my-branch" }, "refs/tags/v1.2.3" }, + + { new() { ["CI_COMMIT_TAG"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + { new() { ["CI_MERGE_REQUEST_IID"] = "1234" }, "pr1234" }, + { new() { ["CI_EXTERNAL_PULL_REQUEST_IID"] = "5678" }, "pr5678" }, + { new() { ["CI_COMMIT_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["CI_COMMIT_TAG"] = "refs/tags/v1.2.3", ["CI_MERGE_REQUEST_IID"] = "1234" }, "refs/tags/v1.2.3" }, + { new() { ["CI_COMMIT_TAG"] = "refs/tags/v1.2.3", ["CI_EXTERNAL_PULL_REQUEST_IID"] = "5678" }, "refs/tags/v1.2.3" }, + { new() { ["CI_COMMIT_TAG"] = "refs/tags/v1.2.3", ["CI_COMMIT_BRANCH"] = "refs/heads/my-branch" }, "refs/tags/v1.2.3" }, + { new() { ["CI_MERGE_REQUEST_IID"] = "1234", ["CI_EXTERNAL_PULL_REQUEST_IID"] = "5678" }, "pr1234" }, + { new() { ["CI_MERGE_REQUEST_IID"] = "1234", ["CI_COMMIT_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["CI_EXTERNAL_PULL_REQUEST_IID"] = "5678", ["CI_COMMIT_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["CI_COMMIT_TAG"] = "refs/tags/v1.2.3", ["CI_MERGE_REQUEST_IID"] = "1234", ["CI_EXTERNAL_PULL_REQUEST_IID"] = "5678" }, "refs/tags/v1.2.3" }, + { new() { ["CI_COMMIT_TAG"] = "refs/tags/v1.2.3", ["CI_MERGE_REQUEST_IID"] = "1234", ["CI_COMMIT_BRANCH"] = "refs/heads/my-branch" }, "refs/tags/v1.2.3" }, + { new() { ["CI_COMMIT_TAG"] = "refs/tags/v1.2.3", ["CI_EXTERNAL_PULL_REQUEST_IID"] = "5678", ["CI_COMMIT_BRANCH"] = "refs/heads/my-branch" }, "refs/tags/v1.2.3" }, + { new() { ["CI_MERGE_REQUEST_IID"] = "1234", ["CI_EXTERNAL_PULL_REQUEST_IID"] = "5678", ["CI_COMMIT_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["CI_COMMIT_TAG"] = "refs/tags/v1.2.3", ["CI_MERGE_REQUEST_IID"] = "1234", ["CI_EXTERNAL_PULL_REQUEST_IID"] = "5678", ["CI_COMMIT_BRANCH"] = "refs/heads/my-branch" }, "refs/tags/v1.2.3" }, + + { new() { ["BUDDY_EXECUTION_PULL_REQUEST_NO"] = "1234" }, "pr1234" }, + { new() { ["BUDDY_EXECUTION_TAG"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + { new() { ["BUDDY_EXECUTION_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["BUDDY_EXECUTION_PULL_REQUEST_NO"] = "1234", ["BUDDY_EXECUTION_TAG"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + { new() { ["BUDDY_EXECUTION_PULL_REQUEST_NO"] = "1234", ["BUDDY_EXECUTION_BRANCH"] = "refs/heads/my-branch" }, "refs/heads/my-branch" }, + { new() { ["BUDDY_EXECUTION_TAG"] = "refs/tags/v1.2.3", ["BUDDY_EXECUTION_BRANCH"] = "refs/heads/my-branch" }, "refs/tags/v1.2.3" }, + { new() { ["BUDDY_EXECUTION_PULL_REQUEST_NO"] = "1234", ["BUDDY_EXECUTION_BRANCH"] = "refs/heads/my-branch", ["BUDDY_EXECUTION_TAG"] = "refs/tags/v1.2.3" }, "refs/tags/v1.2.3" }, + }; + + return data; + } }