forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationExtensionsTests.cs
More file actions
146 lines (125 loc) · 6.84 KB
/
Copy pathConfigurationExtensionsTests.cs
File metadata and controls
146 lines (125 loc) · 6.84 KB
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
using GitVersion.Core.Tests.Helpers;
using GitVersion.Git;
namespace GitVersion.Configuration.Tests;
[TestFixture]
public class ConfigurationExtensionsTests : TestBase
{
private const string BranchName = "pull-request";
[TestCase("release/2.0.0",
"refs/heads/release/2.0.0", "release/2.0.0", "release/2.0.0",
true, false, false, false, true)]
[TestCase("upstream/release/2.0.0",
"refs/heads/upstream/release/2.0.0", "upstream/release/2.0.0", "upstream/release/2.0.0",
true, false, false, false, false)]
[TestCase("origin/release/2.0.0",
"refs/heads/origin/release/2.0.0", "origin/release/2.0.0", "origin/release/2.0.0",
true, false, false, false, false)]
[TestCase("refs/remotes/upstream/release/2.0.0",
"refs/remotes/upstream/release/2.0.0", "upstream/release/2.0.0", "upstream/release/2.0.0",
false, false, true, false, false)]
[TestCase("refs/remotes/origin/release/2.0.0",
"refs/remotes/origin/release/2.0.0", "origin/release/2.0.0", "release/2.0.0",
false, false, true, false, true)]
public void EnsureIsReleaseBranchWithReferenceNameWorksAsExpected(string branchName, string expectedCanonical, string expectedFriendly, string expectedWithoutOrigin,
bool expectedIsLocalBranch, bool expectedIsPullRequest, bool expectedIsRemoteBranch, bool expectedIsTag, bool expectedIsReleaseBranch)
{
var configuration = GitFlowConfigurationBuilder.New.Build();
var actual = ReferenceName.FromBranchName(branchName);
var isReleaseBranch = configuration.IsReleaseBranch(actual);
actual.Canonical.ShouldBe(expectedCanonical);
actual.Friendly.ShouldBe(expectedFriendly);
actual.WithoutOrigin.ShouldBe(expectedWithoutOrigin);
actual.IsLocalBranch.ShouldBe(expectedIsLocalBranch);
actual.IsPullRequest.ShouldBe(expectedIsPullRequest);
actual.IsRemoteBranch.ShouldBe(expectedIsRemoteBranch);
actual.IsTag.ShouldBe(expectedIsTag);
isReleaseBranch.ShouldBe(expectedIsReleaseBranch);
}
[TestCase("feature/sc-1000/Description", @"^features?[\/-](?<BranchName>.+)", "{BranchName}", "sc-1000-Description")]
[TestCase("feature/sc-1000/Description", @"^features?[\/-](?<StoryNo>sc-\d+)[-\/].+", "{StoryNo}", "sc-1000")]
public void EnsureGetBranchSpecificLabelWorksAsExpected(string branchName, string regularExpression, string label, string expectedLabel)
{
var configuration = GitFlowConfigurationBuilder.New
.WithoutBranches()
.WithBranch(branchName, builder => builder
.WithLabel(label)
.WithRegularExpression(regularExpression))
.Build();
var effectiveConfiguration = configuration.GetEffectiveConfiguration(ReferenceName.FromBranchName(branchName));
var actual = effectiveConfiguration.GetBranchSpecificLabel(ReferenceName.FromBranchName(branchName), null, new TestEnvironment());
actual.ShouldBe(expectedLabel);
}
[Test]
public void EnsureGetBranchSpecificLabelProcessesEnvironmentVariables()
{
var environment = new TestEnvironment();
environment.SetEnvironmentVariable("GITHUB_HEAD_REF", "feature-branch");
var configuration = GitFlowConfigurationBuilder.New
.WithoutBranches()
.WithBranch(BranchName, builder => builder
.WithLabel("pr-{env:GITHUB_HEAD_REF}")
.WithRegularExpression(@"^pull[/-]"))
.Build();
var effectiveConfiguration = configuration.GetEffectiveConfiguration(ReferenceName.FromBranchName(BranchName));
var actual = effectiveConfiguration.GetBranchSpecificLabel(ReferenceName.FromBranchName(BranchName), null, environment);
actual.ShouldBe("pr-feature-branch");
}
[Test]
public void EnsureGetBranchSpecificLabelProcessesEnvironmentVariablesWithFallback()
{
var environment = new TestEnvironment();
// Don't set GITHUB_HEAD_REF to test fallback
var configuration = GitFlowConfigurationBuilder.New
.WithoutBranches()
.WithBranch(BranchName, builder => builder
.WithLabel("pr-{env:GITHUB_HEAD_REF ?? \"unknown\"}")
.WithRegularExpression(@"^pull[/-]"))
.Build();
var effectiveConfiguration = configuration.GetEffectiveConfiguration(ReferenceName.FromBranchName(BranchName));
var actual = effectiveConfiguration.GetBranchSpecificLabel(ReferenceName.FromBranchName(BranchName), null, environment);
actual.ShouldBe("pr-unknown");
}
[Test]
public void EnsureGetBranchSpecificLabelProcessesEnvironmentVariablesAndRegexPlaceholders()
{
var environment = new TestEnvironment();
environment.SetEnvironmentVariable("GITHUB_HEAD_REF", "feature-branch");
var configuration = GitFlowConfigurationBuilder.New
.WithoutBranches()
.WithBranch("feature/test-branch", builder => builder
.WithLabel("{BranchName}-{env:GITHUB_HEAD_REF}")
.WithRegularExpression(@"^features?[\/-](?<BranchName>.+)"))
.Build();
var effectiveConfiguration = configuration.GetEffectiveConfiguration(ReferenceName.FromBranchName("feature/test-branch"));
var actual = effectiveConfiguration.GetBranchSpecificLabel(ReferenceName.FromBranchName("feature/test-branch"), null, environment);
actual.ShouldBe("test-branch-feature-branch");
}
[Test]
public void EnsureGetBranchSpecificLabelWorksWithoutEnvironmentWhenNoEnvPlaceholders()
{
var configuration = GitFlowConfigurationBuilder.New
.WithoutBranches()
.WithBranch("feature/test", builder => builder
.WithLabel("{BranchName}")
.WithRegularExpression(@"^features?[\/-](?<BranchName>.+)"))
.Build();
var effectiveConfiguration = configuration.GetEffectiveConfiguration(ReferenceName.FromBranchName("feature/test"));
var actual = effectiveConfiguration.GetBranchSpecificLabel(ReferenceName.FromBranchName("feature/test"), null, new TestEnvironment());
actual.ShouldBe("test");
}
[Test]
public void EnsureGetBranchSpecificLabelThrowsWhenThrowIfNotFoundAndEnvVarMissing()
{
var environment = new TestEnvironment();
// Do not set MISSING_VAR
var configuration = GitFlowConfigurationBuilder.New
.WithoutBranches()
.WithBranch(BranchName, builder => builder
.WithLabel("pr-{env:MISSING_VAR}")
.WithRegularExpression(@"^pull[/-]"))
.Build();
var effectiveConfiguration = configuration.GetEffectiveConfiguration(ReferenceName.FromBranchName(BranchName));
Should.Throw<ArgumentException>(() =>
effectiveConfiguration.GetBranchSpecificLabel(ReferenceName.FromBranchName(BranchName), null, environment));
}
}