Skip to content

Commit d0f8a3c

Browse files
committed
added tests for pull requests in build agents
1 parent cfff762 commit d0f8a3c

File tree

6 files changed

+189
-125
lines changed

6 files changed

+189
-125
lines changed

src/GitVersionCore.Tests/BuildServers/CodeBuildTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public void SetUp()
3333
[Test]
3434
public void CorrectlyIdentifiesCodeBuildPresence()
3535
{
36-
environment.SetEnvironmentVariable(CodeBuild.HeadRefEnvironmentName, "a value");
36+
environment.SetEnvironmentVariable(CodeBuild.EnvironmentVariableName, "a value");
3737
buildServer.CanApplyToCurrentContext().ShouldBe(true);
3838
}
3939

4040
[Test]
4141
public void PicksUpBranchNameFromEnvironment()
4242
{
43-
environment.SetEnvironmentVariable(CodeBuild.HeadRefEnvironmentName, "refs/heads/master");
43+
environment.SetEnvironmentVariable(CodeBuild.EnvironmentVariableName, "refs/heads/master");
4444
buildServer.GetCurrentBranch(false).ShouldBe("refs/heads/master");
4545
}
4646

src/GitVersionCore/BuildServers/CodeBuild.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace GitVersion.BuildServers
88
{
99
public sealed class CodeBuild : BuildServerBase
1010
{
11-
public const string HeadRefEnvironmentName = "CODEBUILD_WEBHOOK_HEAD_REF";
1211
private string file;
12+
public const string EnvironmentVariableName = "CODEBUILD_WEBHOOK_HEAD_REF";
1313

1414
public CodeBuild(IEnvironment environment, ILog log) : base(environment, log)
1515
{
@@ -21,7 +21,7 @@ public void WithPropertyFile(string propertiesFileName)
2121
file = propertiesFileName;
2222
}
2323

24-
protected override string EnvironmentVariable { get; } = HeadRefEnvironmentName;
24+
protected override string EnvironmentVariable { get; } = EnvironmentVariableName;
2525

2626
public override string GenerateSetVersionMessage(VersionVariables variables)
2727
{
@@ -38,7 +38,7 @@ public override string[] GenerateSetParameterMessage(string name, string value)
3838

3939
public override string GetCurrentBranch(bool usingDynamicRepos)
4040
{
41-
return Environment.GetEnvironmentVariable(HeadRefEnvironmentName);
41+
return Environment.GetEnvironmentVariable(EnvironmentVariableName);
4242
}
4343

4444
public override void WriteIntegration(Action<string> writer, VersionVariables variables)

src/GitVersionCore/Extensions/StringExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public static bool IsValidPath(this string path)
4444

4545
try
4646
{
47-
Path.GetFullPath(path);
47+
_ = Path.GetFullPath(path);
4848
}
4949
catch
5050
{
5151
path = Path.Combine(System.Environment.CurrentDirectory, path);
5252

5353
try
5454
{
55-
Path.GetFullPath(path);
55+
_ = Path.GetFullPath(path);
5656
}
5757
catch
5858
{
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}

src/GitVersionExe.Tests/PullRequestInJenkinsPipelineTest.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/GitVersionExe.Tests/PullRequestInTeamCityTest.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)