diff --git a/src/GitVersionCore.Tests/VariableProviderTests.cs b/src/GitVersionCore.Tests/VariableProviderTests.cs index 5bd04c7672..7556b8837c 100644 --- a/src/GitVersionCore.Tests/VariableProviderTests.cs +++ b/src/GitVersionCore.Tests/VariableProviderTests.cs @@ -1,12 +1,14 @@ -using System; -using NUnit.Framework; -using Shouldly; +using GitVersion; +using GitVersion.Logging; using GitVersion.OutputFormatters; using GitVersion.OutputVariables; -using GitVersion; using GitVersion.VersioningModes; using GitVersionCore.Tests.Helpers; using Microsoft.Extensions.DependencyInjection; +using NUnit.Framework; +using Shouldly; +using System; +using System.Collections.Generic; namespace GitVersionCore.Tests { @@ -14,17 +16,40 @@ namespace GitVersionCore.Tests public class VariableProviderTests : TestBase { private IVariableProvider variableProvider; + private List logMessages; [SetUp] public void Setup() { ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute(); - var sp = ConfigureServices(); + logMessages = new List(); + + var sp = ConfigureServices(services => + { + var log = new Log(new TestLogAppender(logMessages.Add)); + services.AddSingleton(log); + }); variableProvider = sp.GetService(); } + [Test] + public void ShouldLogWarningWhenUsingDefaultInformationalVersionInCustomFormat() + { + var semVer = new SemanticVersion + { + Major = 1, + Minor = 2, + Patch = 3, + }; + + var propertyName = nameof(SemanticVersionFormatValues.DefaultInformationalVersion); + var config = new TestEffectiveConfiguration(assemblyInformationalFormat: $"{{{propertyName}}}"); + variableProvider.GetVariablesFor(semVer, config, false); + logMessages.ShouldContain(message => message.Trim().StartsWith("WARN") && message.Contains(propertyName), 1, $"Expected a warning to be logged when using the variable {propertyName} in a configuration format template"); + } + [Test] [Category("NoMono")] [Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")] diff --git a/src/GitVersionCore/OutputVariables/VariableProvider.cs b/src/GitVersionCore/OutputVariables/VariableProvider.cs index 827f7a3da1..4a9fadc0b1 100644 --- a/src/GitVersionCore/OutputVariables/VariableProvider.cs +++ b/src/GitVersionCore/OutputVariables/VariableProvider.cs @@ -6,6 +6,7 @@ using GitVersion.VersioningModes; using GitVersion.Configuration; using GitVersion.Helpers; +using GitVersion.Logging; namespace GitVersion.OutputVariables { @@ -13,11 +14,13 @@ public class VariableProvider : IVariableProvider { private readonly INextVersionCalculator nextVersionCalculator; private readonly IEnvironment environment; + private readonly ILog log; - public VariableProvider(INextVersionCalculator nextVersionCalculator, IEnvironment environment) + public VariableProvider(INextVersionCalculator nextVersionCalculator, IEnvironment environment, ILog log = default) { this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator)); this.environment = environment; + this.log = log ?? new NullLog(); } public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, EffectiveConfiguration config, bool isCurrentCommitTagged) @@ -57,7 +60,7 @@ public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, Effecti var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config); var informationalVersion = CheckAndFormatString(config.AssemblyInformationalFormat, semverFormatValues, - environment, semverFormatValues.DefaultInformationalVersion, "AssemblyInformationalVersion"); + environment, semverFormatValues.InformationalVersion, "AssemblyInformationalVersion"); var assemblyFileSemVer = CheckAndFormatString(config.AssemblyFileVersioningFormat, semverFormatValues, environment, semverFormatValues.AssemblyFileSemVer, "AssemblyFileVersioningFormat"); @@ -124,7 +127,7 @@ private static void PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVe } } - private static string CheckAndFormatString(string formatString, T source, IEnvironment environment, string defaultValue, string formatVarName) + private string CheckAndFormatString(string formatString, T source, IEnvironment environment, string defaultValue, string formatVarName) { string formattedString; @@ -134,6 +137,8 @@ private static string CheckAndFormatString(string formatString, T source, IEn } else { + WarnIfUsingObsoleteFormatValues(formatString); + try { formattedString = formatString.FormatWith(source, environment).RegexReplace("[^0-9A-Za-z-.+]", "-"); @@ -146,5 +151,14 @@ private static string CheckAndFormatString(string formatString, T source, IEn return formattedString; } + + private void WarnIfUsingObsoleteFormatValues(string formatString) + { + var obsoletePropertyName = nameof(SemanticVersionFormatValues.DefaultInformationalVersion); + if (formatString.Contains($"{{{obsoletePropertyName}}}")) + { + log.Write(LogLevel.Warn, $"Use format variable '{nameof(SemanticVersionFormatValues.InformationalVersion)}' instead of '{obsoletePropertyName}' which is obsolete and will be removed in a future release."); + } + } } } diff --git a/src/GitVersionCore/OutputVariables/VersionVariables.cs b/src/GitVersionCore/OutputVariables/VersionVariables.cs index fcc5bcc426..e5a81db130 100644 --- a/src/GitVersionCore/OutputVariables/VersionVariables.cs +++ b/src/GitVersionCore/OutputVariables/VersionVariables.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using YamlDotNet.Serialization; @@ -111,7 +112,7 @@ public static IEnumerable AvailableVariables .GetProperties() .Where(p => !p.GetCustomAttributes(typeof(ReflectionIgnoreAttribute), false).Any()) .Select(p => p.Name) - .OrderBy(a => a); + .OrderBy(a => a, StringComparer.Ordinal); } } diff --git a/src/GitVersionCore/SemanticVersioning/SemanticVersionFormatValues.cs b/src/GitVersionCore/SemanticVersioning/SemanticVersionFormatValues.cs index 9ba80b4be7..85236096ba 100644 --- a/src/GitVersionCore/SemanticVersioning/SemanticVersionFormatValues.cs +++ b/src/GitVersionCore/SemanticVersioning/SemanticVersionFormatValues.cs @@ -1,3 +1,4 @@ +using System; using System.Globalization; using GitVersion.Configuration; using GitVersion.Extensions; @@ -69,7 +70,10 @@ public SemanticVersionFormatValues(SemanticVersion semver, EffectiveConfiguratio public string NuGetPreReleaseTag => NuGetPreReleaseTagV2; - public string DefaultInformationalVersion => semver.ToString("i"); + public string InformationalVersion => semver.ToString("i"); + + [Obsolete("Use InformationalVersion instead")] + public string DefaultInformationalVersion => InformationalVersion; public string VersionSourceSha => semver.BuildMetaData.VersionSourceSha;