Skip to content
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
35 changes: 30 additions & 5 deletions src/GitVersionCore.Tests/VariableProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@
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
{
[TestFixture]
public class VariableProviderTests : TestBase
{
private IVariableProvider variableProvider;
private List<string> logMessages;

[SetUp]
public void Setup()
{
ShouldlyConfiguration.ShouldMatchApprovedDefaults.LocateTestMethodUsingAttribute<TestAttribute>();

var sp = ConfigureServices();
logMessages = new List<string>();

var sp = ConfigureServices(services =>
{
var log = new Log(new TestLogAppender(logMessages.Add));
services.AddSingleton<ILog>(log);
});

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

[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.")]
Expand Down
20 changes: 17 additions & 3 deletions src/GitVersionCore/OutputVariables/VariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
using GitVersion.VersioningModes;
using GitVersion.Configuration;
using GitVersion.Helpers;
using GitVersion.Logging;

namespace GitVersion.OutputVariables
{
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)
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -124,7 +127,7 @@ private static void PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVe
}
}

private static string CheckAndFormatString<T>(string formatString, T source, IEnvironment environment, string defaultValue, string formatVarName)
private string CheckAndFormatString<T>(string formatString, T source, IEnvironment environment, string defaultValue, string formatVarName)
{
string formattedString;

Expand All @@ -134,6 +137,8 @@ private static string CheckAndFormatString<T>(string formatString, T source, IEn
}
else
{
WarnIfUsingObsoleteFormatValues(formatString);

try
{
formattedString = formatString.FormatWith(source, environment).RegexReplace("[^0-9A-Za-z-.+]", "-");
Expand All @@ -146,5 +151,14 @@ private static string CheckAndFormatString<T>(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.");
}
}
}
}
3 changes: 2 additions & 1 deletion src/GitVersionCore/OutputVariables/VersionVariables.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -111,7 +112,7 @@ public static IEnumerable<string> AvailableVariables
.GetProperties()
.Where(p => !p.GetCustomAttributes(typeof(ReflectionIgnoreAttribute), false).Any())
.Select(p => p.Name)
.OrderBy(a => a);
.OrderBy(a => a, StringComparer.Ordinal);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Globalization;
using GitVersion.Configuration;
using GitVersion.Extensions;
Expand Down Expand Up @@ -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;

Expand Down