-
Notifications
You must be signed in to change notification settings - Fork 653
/
PullRequestInBuildAgentTest.cs
202 lines (176 loc) · 7.46 KB
/
PullRequestInBuildAgentTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using GitTools.Testing;
using GitVersion.BuildAgents;
using GitVersion.Core.Tests.Helpers;
using LibGit2Sharp;
using NUnit.Framework;
using Shouldly;
namespace GitVersion.App.Tests;
[TestFixture]
public class PullRequestInBuildAgentTest
{
private const string PullRequestBranchName = "PR-5";
private static readonly string[] PrMergeRefs =
{
"refs/pull-requests/5/merge",
"refs/pull/5/merge",
"refs/heads/pull/5/head",
"refs/remotes/pull/5/merge",
"refs/remotes/pull-requests/5/merge"
};
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyAzurePipelinesPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ AzurePipelines.EnvironmentVariableName, "true" },
{ "BUILD_SOURCEBRANCH", PullRequestBranchName }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyCodeBuildPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ CodeBuild.WebHookEnvironmentVariableName, PullRequestBranchName }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyContinuaCIPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ ContinuaCi.EnvironmentVariableName, "true" }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyDronePullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ Drone.EnvironmentVariableName, "true" },
{ "DRONE_PULL_REQUEST", PullRequestBranchName }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyGitHubActionsPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ GitHubActions.EnvironmentVariableName, "true" },
{ "GITHUB_REF", PullRequestBranchName }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyGitLabCIPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ GitLabCi.EnvironmentVariableName, "true" },
{ "CI_COMMIT_REF_NAME", PullRequestBranchName }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyJenkinsPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ Jenkins.EnvironmentVariableName, "url" },
{
"BRANCH_NAME", PullRequestBranchName }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyMyGetPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ MyGet.EnvironmentVariableName, "MyGet" }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyTeamCityPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ TeamCity.EnvironmentVariableName, "8.0.0" }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyTravisCIPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ TravisCi.EnvironmentVariableName, "true" },
{ "CI", "true" }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
[TestCaseSource(nameof(PrMergeRefs))]
public async Task VerifyBitBucketPipelinesPullRequest(string pullRequestRef)
{
var env = new Dictionary<string, string>
{
{ BitBucketPipelines.EnvironmentVariableName, "MyWorkspace" },
{ BitBucketPipelines.PullRequestEnvironmentVariableName, pullRequestRef }
};
await VerifyPullRequestVersionIsCalculatedProperly(pullRequestRef, env);
}
private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pullRequestRef, Dictionary<string, string> env)
{
using var fixture = new EmptyRepositoryFixture("main");
var remoteRepositoryPath = ExecutableHelper.GetTempPath();
RepositoryFixtureBase.Init(remoteRepositoryPath, "main");
using (var remoteRepository = new Repository(remoteRepositoryPath))
{
remoteRepository.Config.Set("user.name", "Test");
remoteRepository.Config.Set("user.email", "test@email.com");
fixture.Repository.Network.Remotes.Add("origin", remoteRepositoryPath);
Console.WriteLine("Created git repository at {0}", remoteRepositoryPath);
remoteRepository.MakeATaggedCommit("1.0.3");
var branch = remoteRepository.CreateBranch("FeatureBranch");
Commands.Checkout(remoteRepository, branch);
remoteRepository.MakeCommits(2);
Commands.Checkout(remoteRepository, remoteRepository.Head.Tip.Sha);
//Emulate merge commit
var mergeCommitSha = remoteRepository.MakeACommit().Sha;
Commands.Checkout(remoteRepository, TestBase.MainBranch); // HEAD cannot be pointing at the merge commit
remoteRepository.Refs.Add(pullRequestRef, new ObjectId(mergeCommitSha));
// Checkout PR commit
Commands.Fetch((Repository)fixture.Repository, "origin", Array.Empty<string>(), new FetchOptions(), null);
Commands.Checkout(fixture.Repository, mergeCommitSha);
}
var programFixture = new ProgramFixture(fixture.RepositoryPath);
programFixture.WithEnv(env.ToArray());
var result = await programFixture.Run();
result.ExitCode.ShouldBe(0);
result.OutputVariables.ShouldNotBeNull();
result.OutputVariables.FullSemVer.ShouldBe("1.0.4-PullRequest0005.3");
// Cleanup repository files
DirectoryHelper.DeleteDirectory(remoteRepositoryPath);
}
private static readonly object[] PrMergeRefInputs =
{
new object[] { "refs/pull-requests/5/merge", "refs/pull-requests/5/merge", false, true, false },
new object[] { "refs/pull/5/merge", "refs/pull/5/merge", false, true, false},
new object[] { "refs/heads/pull/5/head", "pull/5/head", true, false, false },
new object[] { "refs/remotes/pull/5/merge", "pull/5/merge", false, true, true },
};
[TestCaseSource(nameof(PrMergeRefInputs))]
public void VerifyPullRequestInput(string pullRequestRef, string friendly, bool isBranch, bool isPullRequest, bool isRemote)
{
var refName = new ReferenceName(pullRequestRef);
Assert.AreEqual(friendly, refName.Friendly);
Assert.AreEqual(isBranch, refName.IsBranch);
Assert.AreEqual(isPullRequest, refName.IsPullRequest);
Assert.AreEqual(isRemote, refName.IsRemoteBranch);
}
}