Skip to content

Commit 9f8f5a5

Browse files
committed
feat: honor configuration version in overrides and cache
1 parent 01ef9f0 commit 9f8f5a5

7 files changed

Lines changed: 310 additions & 39 deletions

File tree

src/GitVersion.App.Tests/ArgumentParserTests.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,102 @@
99
namespace GitVersion.App.Tests;
1010

1111
[TestFixture]
12+
[NonParallelizable]
1213
public class ArgumentParserTests : TestBase
1314
{
1415
private IEnvironment environment = null!;
1516
private IArgumentParser argumentParser = null!;
1617
private IFileSystem fileSystem = null!;
18+
private string? originalConfigurationVersion;
1719

1820
[SetUp]
1921
public void SetUp()
2022
{
23+
this.originalConfigurationVersion = System.Environment.GetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName);
24+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, "v6");
2125
var sp = ConfigureServices(services => services.AddModule(new GitVersionAppModule()));
2226
this.environment = sp.GetRequiredService<IEnvironment>();
2327
this.argumentParser = sp.GetRequiredService<IArgumentParser>();
2428
this.fileSystem = sp.GetRequiredService<IFileSystem>();
2529
}
2630

31+
[TearDown]
32+
public void TearDown() =>
33+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, this.originalConfigurationVersion);
34+
35+
[Test]
36+
public void OverrideConfigSupportsNestedV7RootAndBranchPaths()
37+
{
38+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, "v7");
39+
40+
var arguments = this.argumentParser.ParseArguments(
41+
"--override-config calculation.tag-prefix=custom- " +
42+
"--override-config calculation.branches.main.increment=Major " +
43+
"--override-config output.branches.main.pre-release-weight=42");
44+
45+
var normalized = ConfigurationDocumentMapper.Normalize(
46+
arguments.OverrideConfiguration!, ConfigurationVersion.V7, "test override");
47+
ConfigurationHelper configurationHelper = new(normalized);
48+
var configuration = configurationHelper.Configuration;
49+
configuration.TagPrefixPattern.ShouldBe("custom-");
50+
configuration.Branches["main"].Increment.ShouldBe(IncrementStrategy.Major);
51+
configuration.Branches["main"].PreReleaseWeight.ShouldBe(42);
52+
}
53+
54+
[Test]
55+
public void OverrideConfigRejectsV6PathInV7WithReplacement()
56+
{
57+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, "v7");
58+
59+
var exception = Should.Throw<WarningException>(() =>
60+
this.argumentParser.ParseArguments("--override-config tag-prefix=custom-"));
61+
62+
exception.Message.ShouldContain("calculation.tag-prefix");
63+
exception.Message.ShouldContain("config migrate");
64+
}
65+
66+
[Test]
67+
public void OverrideConfigRejectsPropertyInWrongV7SectionWithReplacement()
68+
{
69+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, "v7");
70+
71+
var exception = Should.Throw<WarningException>(() =>
72+
this.argumentParser.ParseArguments("--override-config calculation.update-build-number=false"));
73+
74+
exception.Message.ShouldContain("output.update-build-number");
75+
}
76+
77+
[Test]
78+
public void OverrideConfigSupportsV6BranchPath()
79+
{
80+
var arguments = this.argumentParser.ParseArguments("--override-config branches.main.increment=Major");
81+
82+
ConfigurationHelper configurationHelper = new(arguments.OverrideConfiguration);
83+
configurationHelper.Configuration.Branches["main"].Increment.ShouldBe(IncrementStrategy.Major);
84+
}
85+
86+
[Test]
87+
public void OverrideConfigRejectsV7PathInV6WithReplacement()
88+
{
89+
var exception = Should.Throw<WarningException>(() =>
90+
this.argumentParser.ParseArguments("--override-config output.update-build-number=false"));
91+
92+
exception.Message.ShouldContain("update-build-number");
93+
exception.Message.ShouldContain("config migrate");
94+
}
95+
96+
[Test]
97+
public void OverrideConfigBatchValidationDoesNotApplyAnyValues()
98+
{
99+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, "v7");
100+
var parser = new OverrideConfigurationOptionParser();
101+
102+
Should.Throw<WarningException>(() => parser.SetValues(
103+
["calculation.tag-prefix=custom-", "tag-prefix=legacy"], "--override-config"));
104+
105+
parser.GetOverrideConfiguration().ShouldBeEmpty();
106+
}
107+
27108
[Test]
28109
public void EmptyMeansUseCurrentDirectory()
29110
{

src/GitVersion.App.Tests/LegacyArgumentParserTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,60 @@
99
namespace GitVersion.App.Tests;
1010

1111
[TestFixture]
12+
[NonParallelizable]
1213
public class LegacyArgumentParserTests : TestBase
1314
{
1415
private IEnvironment environment = null!;
1516
private IArgumentParser argumentParser = null!;
1617
private IFileSystem fileSystem = null!;
18+
private string? originalConfigurationVersion;
1719

1820
[SetUp]
1921
public void SetUp()
2022
{
23+
this.originalConfigurationVersion = System.Environment.GetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName);
24+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, "v6");
2125
var sp = ConfigureServices(services => services.AddModule(new GitVersionAppModule(useLegacyParser: true)));
2226
this.environment = sp.GetRequiredService<IEnvironment>();
2327
this.argumentParser = sp.GetRequiredService<IArgumentParser>();
2428
this.fileSystem = sp.GetRequiredService<IFileSystem>();
2529
}
2630

31+
[TearDown]
32+
public void TearDown() =>
33+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, this.originalConfigurationVersion);
34+
35+
[Test]
36+
public void OverrideConfigSupportsNestedV7RootAndBranchPaths()
37+
{
38+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, "v7");
39+
40+
var arguments = this.argumentParser.ParseArguments(
41+
"/overrideconfig calculation.tag-prefix=custom- " +
42+
"/overrideconfig calculation.branches.main.increment=Major " +
43+
"/overrideconfig output.branches.main.pre-release-weight=42");
44+
45+
var normalized = ConfigurationDocumentMapper.Normalize(
46+
arguments.OverrideConfiguration!, ConfigurationVersion.V7, "test override");
47+
ConfigurationHelper configurationHelper = new(normalized);
48+
var configuration = configurationHelper.Configuration;
49+
configuration.TagPrefixPattern.ShouldBe("custom-");
50+
configuration.Branches["main"].Increment.ShouldBe(IncrementStrategy.Major);
51+
configuration.Branches["main"].PreReleaseWeight.ShouldBe(42);
52+
}
53+
54+
[Test]
55+
public void OverrideConfigRejectsV6PathInV7WithReplacement()
56+
{
57+
System.Environment.SetEnvironmentVariable(ConfigurationVersionSelector.EnvironmentVariableName, "v7");
58+
59+
var exception = Should.Throw<WarningException>(() =>
60+
this.argumentParser.ParseArguments("/overrideconfig tag-prefix=custom-"));
61+
62+
exception.Message.ShouldContain("calculation.tag-prefix");
63+
exception.Message.ShouldContain("config migrate");
64+
}
65+
2766
[Test]
2867
public void EmptyMeansUseCurrentDirectory()
2968
{

src/GitVersion.App/ArgumentParser.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -591,24 +591,7 @@ private static void ParseOverrideConfig(Arguments arguments, IReadOnlyCollection
591591
}
592592

593593
var parser = new OverrideConfigurationOptionParser();
594-
595-
foreach (var keyValueOption in values)
596-
{
597-
var keyAndValue = QuotedStringHelpers.SplitUnquoted(keyValueOption, '=');
598-
if (keyAndValue.Length != 2)
599-
{
600-
throw new WarningException($"Could not parse --override-config option: {keyValueOption}. Ensure it is in format 'key=value'.");
601-
}
602-
603-
var optionKey = keyAndValue[0].ToLowerInvariant();
604-
if (!OverrideConfigurationOptionParser.SupportedProperties.Contains(optionKey))
605-
{
606-
throw new WarningException($"Could not parse --override-config option: {keyValueOption}. Unsupported key '{optionKey}'.");
607-
}
608-
609-
parser.SetValue(optionKey, keyAndValue[1]);
610-
}
611-
594+
parser.SetValues(values, "--override-config");
612595
arguments.OverrideConfiguration = parser.GetOverrideConfiguration();
613596
}
614597

src/GitVersion.App/LegacyArgumentParser.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -511,25 +511,7 @@ private static void ParseOverrideConfig(Arguments arguments, IReadOnlyCollection
511511
}
512512

513513
var parser = new OverrideConfigurationOptionParser();
514-
515-
// key=value
516-
foreach (var keyValueOption in values)
517-
{
518-
var keyAndValue = QuotedStringHelpers.SplitUnquoted(keyValueOption, '=');
519-
if (keyAndValue.Length != 2)
520-
{
521-
throw new WarningException($"Could not parse /overrideconfig option: {keyValueOption}. Ensure it is in format 'key=value'.");
522-
}
523-
524-
var optionKey = keyAndValue[0].ToLowerInvariant();
525-
if (!OverrideConfigurationOptionParser.SupportedProperties.Contains(optionKey))
526-
{
527-
throw new WarningException($"Could not parse /overrideconfig option: {keyValueOption}. Unsupported key '{optionKey}'.");
528-
}
529-
530-
parser.SetValue(optionKey, keyAndValue[1]);
531-
}
532-
514+
parser.SetValues(values, "/overrideconfig");
533515
arguments.OverrideConfiguration = parser.GetOverrideConfiguration();
534516
}
535517

0 commit comments

Comments
 (0)