|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using GitTools.Testing; |
| 4 | +using GitVersion.BuildServers; |
| 5 | +using LibGit2Sharp; |
| 6 | +using NUnit.Framework; |
| 7 | +using Shouldly; |
| 8 | +using GitVersionCore.Tests.Helpers; |
| 9 | + |
| 10 | +namespace GitVersionExe.Tests |
| 11 | +{ |
| 12 | + [TestFixture] |
| 13 | + public class PullRequestInBuildAgentTest |
| 14 | + { |
| 15 | + private const string PullRequestBranchName = "PR-5"; |
| 16 | + |
| 17 | + [TestCase("refs/pull-requests/5/merge")] |
| 18 | + [TestCase("refs/pull/5/merge")] |
| 19 | + [TestCase("refs/heads/pull/5/head")] |
| 20 | + public void VerifyAzurePipelinesPullRequest(string pullRequestRef) |
| 21 | + { |
| 22 | + var env = new[] |
| 23 | + { |
| 24 | + new KeyValuePair<string, string>(AzurePipelines.EnvironmentVariableName, "true"), |
| 25 | + new KeyValuePair<string, string>("BUILD_SOURCEBRANCH", PullRequestBranchName) |
| 26 | + }; |
| 27 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 28 | + } |
| 29 | + |
| 30 | + [TestCase("refs/pull-requests/5/merge")] |
| 31 | + [TestCase("refs/pull/5/merge")] |
| 32 | + [TestCase("refs/heads/pull/5/head")] |
| 33 | + public void VerifyCodeBuildPullRequest(string pullRequestRef) |
| 34 | + { |
| 35 | + var env = new[] |
| 36 | + { |
| 37 | + new KeyValuePair<string, string>(CodeBuild.EnvironmentVariableName, PullRequestBranchName), |
| 38 | + }; |
| 39 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 40 | + } |
| 41 | + |
| 42 | + [TestCase("refs/pull-requests/5/merge")] |
| 43 | + [TestCase("refs/pull/5/merge")] |
| 44 | + [TestCase("refs/heads/pull/5/head")] |
| 45 | + public void VerifyContinuaCIPullRequest(string pullRequestRef) |
| 46 | + { |
| 47 | + var env = new[] |
| 48 | + { |
| 49 | + new KeyValuePair<string, string>(ContinuaCi.EnvironmentVariableName, "true"), |
| 50 | + }; |
| 51 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + [TestCase("refs/pull-requests/5/merge")] |
| 56 | + [TestCase("refs/pull/5/merge")] |
| 57 | + [TestCase("refs/heads/pull/5/head")] |
| 58 | + public void VerifyDronePullRequest(string pullRequestRef) |
| 59 | + { |
| 60 | + var env = new[] |
| 61 | + { |
| 62 | + new KeyValuePair<string, string>(Drone.EnvironmentVariableName, "true"), |
| 63 | + new KeyValuePair<string, string>("DRONE_PULL_REQUEST", PullRequestBranchName), |
| 64 | + }; |
| 65 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 66 | + } |
| 67 | + |
| 68 | + [TestCase("refs/pull-requests/5/merge")] |
| 69 | + [TestCase("refs/pull/5/merge")] |
| 70 | + [TestCase("refs/heads/pull/5/head")] |
| 71 | + public void VerifyGitHubActionsPullRequest(string pullRequestRef) |
| 72 | + { |
| 73 | + var env = new[] |
| 74 | + { |
| 75 | + new KeyValuePair<string, string>(GitHubActions.EnvironmentVariableName, "true"), |
| 76 | + new KeyValuePair<string, string>("GITHUB_REF", PullRequestBranchName), |
| 77 | + }; |
| 78 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 79 | + } |
| 80 | + |
| 81 | + [TestCase("refs/pull-requests/5/merge")] |
| 82 | + [TestCase("refs/pull/5/merge")] |
| 83 | + [TestCase("refs/heads/pull/5/head")] |
| 84 | + public void VerifyGitLabCIPullRequest(string pullRequestRef) |
| 85 | + { |
| 86 | + var env = new[] |
| 87 | + { |
| 88 | + new KeyValuePair<string, string>(GitLabCi.EnvironmentVariableName, "true"), |
| 89 | + new KeyValuePair<string, string>("CI_COMMIT_REF_NAME", PullRequestBranchName), |
| 90 | + }; |
| 91 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 92 | + } |
| 93 | + |
| 94 | + [TestCase("refs/pull-requests/5/merge")] |
| 95 | + [TestCase("refs/pull/5/merge")] |
| 96 | + [TestCase("refs/heads/pull/5/head")] |
| 97 | + public void VerifyJenkinsPullRequest(string pullRequestRef) |
| 98 | + { |
| 99 | + var env = new[] |
| 100 | + { |
| 101 | + new KeyValuePair<string, string>(Jenkins.EnvironmentVariableName, "url"), |
| 102 | + new KeyValuePair<string, string>("BRANCH_NAME", PullRequestBranchName) |
| 103 | + }; |
| 104 | + |
| 105 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 106 | + } |
| 107 | + |
| 108 | + [TestCase("refs/pull-requests/5/merge")] |
| 109 | + [TestCase("refs/pull/5/merge")] |
| 110 | + [TestCase("refs/heads/pull/5/head")] |
| 111 | + public void VerifyMyGetPullRequest(string pullRequestRef) |
| 112 | + { |
| 113 | + var env = new[] |
| 114 | + { |
| 115 | + new KeyValuePair<string, string>(MyGet.EnvironmentVariableName, "MyGet"), |
| 116 | + }; |
| 117 | + |
| 118 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 119 | + } |
| 120 | + |
| 121 | + [TestCase("refs/pull-requests/5/merge")] |
| 122 | + [TestCase("refs/pull/5/merge")] |
| 123 | + [TestCase("refs/heads/pull/5/head")] |
| 124 | + public void VerifyTeamCityPullRequest(string pullRequestRef) |
| 125 | + { |
| 126 | + var env = new[] |
| 127 | + { |
| 128 | + new KeyValuePair<string, string>(TeamCity.EnvironmentVariableName, "8.0.0"), |
| 129 | + }; |
| 130 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 131 | + } |
| 132 | + |
| 133 | + [TestCase("refs/pull-requests/5/merge")] |
| 134 | + [TestCase("refs/pull/5/merge")] |
| 135 | + [TestCase("refs/heads/pull/5/head")] |
| 136 | + public void VerifyTravisCIPullRequest(string pullRequestRef) |
| 137 | + { |
| 138 | + var env = new[] |
| 139 | + { |
| 140 | + new KeyValuePair<string, string>(TravisCi.EnvironmentVariableName, "true"), |
| 141 | + new KeyValuePair<string, string>("CI", "true"), |
| 142 | + }; |
| 143 | + VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env); |
| 144 | + } |
| 145 | + |
| 146 | + private static void VerifyPullRequestVersionIsCalculatedProperly(string pullRequestRef, params KeyValuePair<string, string>[] env) |
| 147 | + { |
| 148 | + using var fixture = new EmptyRepositoryFixture(); |
| 149 | + var remoteRepositoryPath = PathHelper.GetTempPath(); |
| 150 | + Repository.Init(remoteRepositoryPath); |
| 151 | + using (var remoteRepository = new Repository(remoteRepositoryPath)) |
| 152 | + { |
| 153 | + remoteRepository.Config.Set("user.name", "Test"); |
| 154 | + remoteRepository.Config.Set("user.email", "[email protected]"); |
| 155 | + fixture.Repository.Network.Remotes.Add("origin", remoteRepositoryPath); |
| 156 | + Console.WriteLine("Created git repository at {0}", remoteRepositoryPath); |
| 157 | + remoteRepository.MakeATaggedCommit("1.0.3"); |
| 158 | + |
| 159 | + var branch = remoteRepository.CreateBranch("FeatureBranch"); |
| 160 | + Commands.Checkout(remoteRepository, branch); |
| 161 | + remoteRepository.MakeCommits(2); |
| 162 | + Commands.Checkout(remoteRepository, remoteRepository.Head.Tip.Sha); |
| 163 | + //Emulate merge commit |
| 164 | + var mergeCommitSha = remoteRepository.MakeACommit().Sha; |
| 165 | + Commands.Checkout(remoteRepository, "master"); // HEAD cannot be pointing at the merge commit |
| 166 | + remoteRepository.Refs.Add(pullRequestRef, new ObjectId(mergeCommitSha)); |
| 167 | + |
| 168 | + // Checkout PR commit |
| 169 | + Commands.Fetch((Repository) fixture.Repository, "origin", new string[0], new FetchOptions(), null); |
| 170 | + Commands.Checkout(fixture.Repository, mergeCommitSha); |
| 171 | + } |
| 172 | + |
| 173 | + var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, environments: env); |
| 174 | + |
| 175 | + result.ExitCode.ShouldBe(0); |
| 176 | + result.OutputVariables.FullSemVer.ShouldBe("1.0.4-PullRequest0005.3"); |
| 177 | + |
| 178 | + // Cleanup repository files |
| 179 | + DirectoryHelper.DeleteDirectory(remoteRepositoryPath); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments