From e0c882d6919033a7cda57ce0272e3ed8afd5f0ab Mon Sep 17 00:00:00 2001 From: Artur Date: Tue, 28 Feb 2023 17:39:51 +0100 Subject: [PATCH] #2484 - show warning if the configuration file is not found --- .../ArgumentParserTests.cs | 19 +++++++++++++++++-- src/GitVersion.App/ArgumentParser.cs | 19 +++++++++++++++++++ .../TrackReleaseBranchesVersionStrategy.cs | 2 -- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/GitVersion.App.Tests/ArgumentParserTests.cs b/src/GitVersion.App.Tests/ArgumentParserTests.cs index 7f31c81f21..c3ed7dc332 100644 --- a/src/GitVersion.App.Tests/ArgumentParserTests.cs +++ b/src/GitVersion.App.Tests/ArgumentParserTests.cs @@ -531,8 +531,8 @@ private static IEnumerable OverrideConfigWithSingleOptionTestData( public void OverrideConfigWithMultipleOptions(string options, GitVersionConfiguration expected) { var arguments = this.argumentParser.ParseArguments(options); - ConfigurationHelper configruationHelper = new(arguments.OverrideConfiguration); - configruationHelper.Configuration.ShouldBeEquivalentTo(expected); + ConfigurationHelper configurationHelper = new(arguments.OverrideConfiguration); + configurationHelper.Configuration.ShouldBeEquivalentTo(expected); } private static IEnumerable OverrideConfigWithMultipleOptionsTestData() @@ -740,4 +740,19 @@ public void EnsureFormatIsSet() var arguments = this.argumentParser.ParseArguments("-format {Major}.{Minor}.{Patch}"); arguments.Format.ShouldBe("{Major}.{Minor}.{Patch}"); } + + [TestCase("custom-config.yaml")] + [TestCase(@"c:\custom-config.yaml")] + public void ThrowIfConfigurationFileDoesNotExist(string configFile) => + Should.Throw(() => _ = this.argumentParser.ParseArguments($"-config {configFile}")); + + [Test] + public void EnsureConfigurationFileIsSet() + { + var configFile = Path.GetTempPath() + Guid.NewGuid() + ".yaml"; + File.WriteAllText(configFile, "next-version: 1.0.0"); + var arguments = this.argumentParser.ParseArguments($"-config {configFile}"); + arguments.ConfigurationFile.ShouldBe(configFile); + File.Delete(configFile); + } } diff --git a/src/GitVersion.App/ArgumentParser.cs b/src/GitVersion.App/ArgumentParser.cs index 60c0e79f1f..7e18f30a0a 100644 --- a/src/GitVersion.App/ArgumentParser.cs +++ b/src/GitVersion.App/ArgumentParser.cs @@ -106,9 +106,28 @@ public Arguments ParseArguments(string[] commandLineArguments) if (!arguments.EnsureAssemblyInfo) arguments.UpdateAssemblyInfoFileName = ResolveFiles(arguments.TargetPath, arguments.UpdateAssemblyInfoFileName).ToHashSet(); arguments.NoFetch = arguments.NoFetch || this.buildAgent.PreventFetch(); + ValidateConfigurationFile(arguments); + return arguments; } + private static void ValidateConfigurationFile(Arguments arguments) + { + if (arguments.ConfigurationFile.IsNullOrWhiteSpace()) return; + + if (Path.IsPathRooted(arguments.ConfigurationFile)) + { + if (!File.Exists(arguments.ConfigurationFile)) throw new WarningException($"Could not find config file at '{arguments.ConfigurationFile}'"); + arguments.ConfigurationFile = Path.GetFullPath(arguments.ConfigurationFile); + } + else + { + var configFilePath = Path.GetFullPath(Path.Combine(arguments.TargetPath!, arguments.ConfigurationFile)); + if (!File.Exists(configFilePath)) throw new WarningException($"Could not find config file at '{configFilePath}'"); + arguments.ConfigurationFile = configFilePath; + } + } + private void ParseSwitchArguments(Arguments arguments, NameValueCollection switchesAndValues, int i) { var name = switchesAndValues.AllKeys[i]; diff --git a/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/TrackReleaseBranchesVersionStrategy.cs b/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/TrackReleaseBranchesVersionStrategy.cs index d2030ede50..1d6f6609af 100644 --- a/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/TrackReleaseBranchesVersionStrategy.cs +++ b/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/TrackReleaseBranchesVersionStrategy.cs @@ -23,7 +23,6 @@ namespace GitVersion.VersionCalculation; public class TrackReleaseBranchesVersionStrategy : VersionStrategyBase { private readonly VersionInBranchNameVersionStrategy releaseVersionStrategy; - private readonly TaggedCommitVersionStrategy taggedCommitVersionStrategy; private readonly IRepositoryStore repositoryStore; @@ -32,7 +31,6 @@ public TrackReleaseBranchesVersionStrategy(IRepositoryStore repositoryStore, Laz { this.repositoryStore = repositoryStore.NotNull(); this.releaseVersionStrategy = new VersionInBranchNameVersionStrategy(repositoryStore, versionContext); - this.taggedCommitVersionStrategy = new TaggedCommitVersionStrategy(repositoryStore, versionContext); } public override IEnumerable GetBaseVersions(EffectiveBranchConfiguration configuration) =>