Skip to content

Commit f1869c0

Browse files
authored
Merge pull request #2348 from michaelnoonan/michaelnoonan/missing-source-branch
Add failing test for missing source-branch config
2 parents 0976842 + 1b41013 commit f1869c0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/GitVersionCore.Tests/Configuration/ConfigProviderTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,38 @@ public void SourceBranchIsRequired()
117117
"See https://gitversion.net/docs/configuration/ for more info");
118118
}
119119

120+
[Test(Description = "This test proves the configuration validation will fail early with a helpful message when a branch listed in source-branches has no configuration.")]
121+
public void SourceBranchesValidationShouldFailWhenMatchingBranchConfigurationIsMissing()
122+
{
123+
const string text = @"
124+
branches:
125+
bug:
126+
regex: 'bug[/-]'
127+
tag: bugfix
128+
source-branches: [notconfigured]";
129+
SetupConfigFileContent(text);
130+
var ex = Should.Throw<ConfigurationException>(() => configProvider.Provide(repoPath));
131+
ex.Message.ShouldBe($"Branch configuration 'bug' defines these 'source-branches' that are not configured: '[notconfigured]'{Environment.NewLine}" +
132+
"See https://gitversion.net/docs/configuration/ for more info");
133+
}
134+
135+
[Test(Description = "Well-known branches may not be present in the configuration file. This test confirms the validation check succeeds when the source-branches configuration contain these well-known branches.")]
136+
[TestCase(Config.MasterBranchKey)]
137+
[TestCase(Config.DevelopBranchKey)]
138+
public void SourceBranchesValidationShouldSucceedForWellKnownBranches(string wellKnownBranchKey)
139+
{
140+
var text = $@"
141+
branches:
142+
bug:
143+
regex: 'bug[/-]'
144+
tag: bugfix
145+
source-branches: [{wellKnownBranchKey}]";
146+
SetupConfigFileContent(text);
147+
var config = configProvider.Provide(repoPath);
148+
149+
config.Branches["bug"].SourceBranches.ShouldBe(new List<string> { wellKnownBranchKey });
150+
}
151+
120152
[Test]
121153
public void CanProvideConfigForNewBranch()
122154
{

src/GitVersionCore/Configuration/ConfigurationBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using GitVersion.Extensions;
45
using GitVersion.Model.Configuration;
56
using GitVersion.VersionCalculation;
@@ -162,6 +163,10 @@ private static void ValidateConfiguration(Config config)
162163
{
163164
throw new ConfigurationException($"Branch configuration '{name}' is missing required configuration 'source-branches'{System.Environment.NewLine}" + "See https://gitversion.net/docs/configuration/ for more info");
164165
}
166+
167+
var missingSourceBranches = sourceBranches.Where(sb => !config.Branches.ContainsKey(sb)).ToArray();
168+
if (missingSourceBranches.Any())
169+
throw new ConfigurationException($"Branch configuration '{name}' defines these 'source-branches' that are not configured: '[{string.Join(",", missingSourceBranches)}]'{System.Environment.NewLine}" + "See https://gitversion.net/docs/configuration/ for more info");
165170
}
166171
}
167172

0 commit comments

Comments
 (0)