Skip to content

Commit 518ba2d

Browse files
authored
Merge pull request #2051 from mgnslndh/feature/informationalversion
Change the name of the property DefaultInformationalVersion of SemanticVersionFormatValues to InformationalVersion to keep it in sync with VersionVariables and the documentation.
2 parents fe17357 + 80338a3 commit 518ba2d

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

src/GitVersionCore.Tests/VariableProviderTests.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,55 @@
1-
using System;
2-
using NUnit.Framework;
3-
using Shouldly;
1+
using GitVersion;
2+
using GitVersion.Logging;
43
using GitVersion.OutputFormatters;
54
using GitVersion.OutputVariables;
6-
using GitVersion;
75
using GitVersion.VersioningModes;
86
using GitVersionCore.Tests.Helpers;
97
using Microsoft.Extensions.DependencyInjection;
8+
using NUnit.Framework;
9+
using Shouldly;
10+
using System;
11+
using System.Collections.Generic;
1012

1113
namespace GitVersionCore.Tests
1214
{
1315
[TestFixture]
1416
public class VariableProviderTests : TestBase
1517
{
1618
private IVariableProvider variableProvider;
19+
private List<string> logMessages;
1720

1821
[SetUp]
1922
public void Setup()
2023
{
2124
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>();
2225

23-
var sp = ConfigureServices();
26+
logMessages = new List<string>();
27+
28+
var sp = ConfigureServices(services =>
29+
{
30+
var log = new Log(new TestLogAppender(logMessages.Add));
31+
services.AddSingleton<ILog>(log);
32+
});
2433

2534
variableProvider = sp.GetService<IVariableProvider>();
2635
}
2736

37+
[Test]
38+
public void ShouldLogWarningWhenUsingDefaultInformationalVersionInCustomFormat()
39+
{
40+
var semVer = new SemanticVersion
41+
{
42+
Major = 1,
43+
Minor = 2,
44+
Patch = 3,
45+
};
46+
47+
var propertyName = nameof(SemanticVersionFormatValues.DefaultInformationalVersion);
48+
var config = new TestEffectiveConfiguration(assemblyInformationalFormat: $"{{{propertyName}}}");
49+
variableProvider.GetVariablesFor(semVer, config, false);
50+
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");
51+
}
52+
2853
[Test]
2954
[Category("NoMono")]
3055
[Description("Won't run on Mono due to source information not being available for ShouldMatchApproved.")]

src/GitVersionCore/OutputVariables/VariableProvider.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66
using GitVersion.VersioningModes;
77
using GitVersion.Configuration;
88
using GitVersion.Helpers;
9+
using GitVersion.Logging;
910

1011
namespace GitVersion.OutputVariables
1112
{
1213
public class VariableProvider : IVariableProvider
1314
{
1415
private readonly INextVersionCalculator nextVersionCalculator;
1516
private readonly IEnvironment environment;
17+
private readonly ILog log;
1618

17-
public VariableProvider(INextVersionCalculator nextVersionCalculator, IEnvironment environment)
19+
public VariableProvider(INextVersionCalculator nextVersionCalculator, IEnvironment environment, ILog log = default)
1820
{
1921
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
2022
this.environment = environment;
23+
this.log = log ?? new NullLog();
2124
}
2225

2326
public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, EffectiveConfiguration config, bool isCurrentCommitTagged)
@@ -57,7 +60,7 @@ public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, Effecti
5760
var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, config);
5861

5962
var informationalVersion = CheckAndFormatString(config.AssemblyInformationalFormat, semverFormatValues,
60-
environment, semverFormatValues.DefaultInformationalVersion, "AssemblyInformationalVersion");
63+
environment, semverFormatValues.InformationalVersion, "AssemblyInformationalVersion");
6164

6265
var assemblyFileSemVer = CheckAndFormatString(config.AssemblyFileVersioningFormat, semverFormatValues,
6366
environment, semverFormatValues.AssemblyFileSemVer, "AssemblyFileVersioningFormat");
@@ -124,7 +127,7 @@ private static void PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVe
124127
}
125128
}
126129

127-
private static string CheckAndFormatString<T>(string formatString, T source, IEnvironment environment, string defaultValue, string formatVarName)
130+
private string CheckAndFormatString<T>(string formatString, T source, IEnvironment environment, string defaultValue, string formatVarName)
128131
{
129132
string formattedString;
130133

@@ -134,6 +137,8 @@ private static string CheckAndFormatString<T>(string formatString, T source, IEn
134137
}
135138
else
136139
{
140+
WarnIfUsingObsoleteFormatValues(formatString);
141+
137142
try
138143
{
139144
formattedString = formatString.FormatWith(source, environment).RegexReplace("[^0-9A-Za-z-.+]", "-");
@@ -146,5 +151,14 @@ private static string CheckAndFormatString<T>(string formatString, T source, IEn
146151

147152
return formattedString;
148153
}
154+
155+
private void WarnIfUsingObsoleteFormatValues(string formatString)
156+
{
157+
var obsoletePropertyName = nameof(SemanticVersionFormatValues.DefaultInformationalVersion);
158+
if (formatString.Contains($"{{{obsoletePropertyName}}}"))
159+
{
160+
log.Write(LogLevel.Warn, $"Use format variable '{nameof(SemanticVersionFormatValues.InformationalVersion)}' instead of '{obsoletePropertyName}' which is obsolete and will be removed in a future release.");
161+
}
162+
}
149163
}
150164
}

src/GitVersionCore/OutputVariables/VersionVariables.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Globalization;
45
using System.IO;
56
using System.Linq;
67
using YamlDotNet.Serialization;
@@ -111,7 +112,7 @@ public static IEnumerable<string> AvailableVariables
111112
.GetProperties()
112113
.Where(p => !p.GetCustomAttributes(typeof(ReflectionIgnoreAttribute), false).Any())
113114
.Select(p => p.Name)
114-
.OrderBy(a => a);
115+
.OrderBy(a => a, StringComparer.Ordinal);
115116
}
116117
}
117118

src/GitVersionCore/SemanticVersioning/SemanticVersionFormatValues.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Globalization;
23
using GitVersion.Configuration;
34
using GitVersion.Extensions;
@@ -69,7 +70,10 @@ public SemanticVersionFormatValues(SemanticVersion semver, EffectiveConfiguratio
6970

7071
public string NuGetPreReleaseTag => NuGetPreReleaseTagV2;
7172

72-
public string DefaultInformationalVersion => semver.ToString("i");
73+
public string InformationalVersion => semver.ToString("i");
74+
75+
[Obsolete("Use InformationalVersion instead")]
76+
public string DefaultInformationalVersion => InformationalVersion;
7377

7478
public string VersionSourceSha => semver.BuildMetaData.VersionSourceSha;
7579

0 commit comments

Comments
 (0)