Skip to content

Add failing test for missing source-branch config #2348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/GitVersionCore.Tests/Configuration/ConfigProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@ public void SourceBranchIsRequired()
"See https://gitversion.net/docs/configuration/ for more info");
}

[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.")]
public void SourceBranchesValidationShouldFailWhenMatchingBranchConfigurationIsMissing()
{
const string text = @"
branches:
bug:
regex: 'bug[/-]'
tag: bugfix
source-branches: [notconfigured]";
SetupConfigFileContent(text);
var ex = Should.Throw<ConfigurationException>(() => configProvider.Provide(repoPath));
ex.Message.ShouldBe($"Branch configuration 'bug' defines these 'source-branches' that are not configured: '[notconfigured]'{Environment.NewLine}" +
"See https://gitversion.net/docs/configuration/ for more info");
}

[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.")]
[TestCase(Config.MasterBranchKey)]
[TestCase(Config.DevelopBranchKey)]
public void SourceBranchesValidationShouldSucceedForWellKnownBranches(string wellKnownBranchKey)
{
var text = $@"
branches:
bug:
regex: 'bug[/-]'
tag: bugfix
source-branches: [{wellKnownBranchKey}]";
SetupConfigFileContent(text);
var config = configProvider.Provide(repoPath);

config.Branches["bug"].SourceBranches.ShouldBe(new List<string> { wellKnownBranchKey });
}

[Test]
public void CanProvideConfigForNewBranch()
{
Expand Down
5 changes: 5 additions & 0 deletions src/GitVersionCore/Configuration/ConfigurationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GitVersion.Extensions;
using GitVersion.Model.Configuration;
using GitVersion.VersionCalculation;
Expand Down Expand Up @@ -162,6 +163,10 @@ private static void ValidateConfiguration(Config config)
{
throw new ConfigurationException($"Branch configuration '{name}' is missing required configuration 'source-branches'{System.Environment.NewLine}" + "See https://gitversion.net/docs/configuration/ for more info");
}

var missingSourceBranches = sourceBranches.Where(sb => !config.Branches.ContainsKey(sb)).ToArray();
if (missingSourceBranches.Any())
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");
}
}

Expand Down