Skip to content

Commit 2642ef1

Browse files
committed
Updated the regex to include spaces around null propagation operator for readability, updated the tests & docs
1 parent 9f78366 commit 2642ef1

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

docs/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ attributes. Valid values: `MajorMinorPatchTag`, `MajorMinorPatch`, `MajorMinor`,
7575
Set this to any of the available [variables](/more-info/variables) in combination (but not necessary) with
7676
a process scoped environment variable. It overwrites the value of `assembly-file-versioning-scheme`. To reference
7777
an environment variable, use `env:`
78-
Example Syntax #1: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER??fallback_string}'`. Uses `JENKINS_BUILD_NUMBER`
78+
Example Syntax #1: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER ?? fallback_string}'`. Uses `JENKINS_BUILD_NUMBER`
7979
if available in the environment otherwise the `fallback_string`
8080
Example Syntax #2: `'{Major}.{Minor}.{Patch}.{env:JENKINS_BUILD_NUMBER}'`. Uses `JENKINS_BUILD_NUMBER`
8181
if available in the environment otherwise the parsing fails.

src/GitVersionCore.Tests/StringFormatWithExtensionTests.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void FormatWith_EnvVarTokenWithFallback()
5555
{
5656
Environment.SetEnvironmentVariable("GIT_VERSION_TEST_VAR", "Env Var Value");
5757
var propertyObject = new { };
58-
var target = "{env:GIT_VERSION_TEST_VAR??fallback}";
58+
var target = "{env:GIT_VERSION_TEST_VAR ?? fallback}";
5959
var expected = "Env Var Value";
6060
var actual = target.FormatWith(propertyObject);
6161
Assert.AreEqual(expected, actual);
@@ -66,7 +66,7 @@ public void FormatWith_UnsetEnvVarTokenWithFallback()
6666
{
6767
Environment.SetEnvironmentVariable("GIT_VERSION_UNSET_TEST_VAR", null);
6868
var propertyObject = new { };
69-
var target = "{env:GIT_VERSION_UNSET_TEST_VAR??fallback}";
69+
var target = "{env:GIT_VERSION_UNSET_TEST_VAR ?? fallback}";
7070
var expected = "fallback";
7171
var actual = target.FormatWith(propertyObject);
7272
Assert.AreEqual(expected, actual);
@@ -89,7 +89,7 @@ public void FormatWith_MultipleEnvChars()
8989
{
9090
var propertyObject = new { };
9191
//Test the greediness of the regex in matching env: char
92-
var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR??fallback}";
92+
var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR ?? fallback}";
9393
var expected = "{env:env:GIT_VERSION_TEST_VAR_1} and fallback";
9494
var actual = target.FormatWith(propertyObject);
9595
Assert.AreEqual(expected, actual);
@@ -100,7 +100,7 @@ public void FormatWith_MultipleFallbackChars()
100100
{
101101
var propertyObject = new { };
102102
//Test the greediness of the regex in matching env: and ?? chars
103-
var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR???fallback}";
103+
var target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR ??? fallback}";
104104
var actual = target.FormatWith(propertyObject);
105105
Assert.AreEqual(target, actual);
106106
}
@@ -111,9 +111,19 @@ public void FormatWith_SingleFallbackChar()
111111
Environment.SetEnvironmentVariable("DUMMY_ENV_VAR", "Dummy-Val");
112112
var propertyObject = new { };
113113
//Test the sanity of the regex when there is a grammar mismatch
114-
var target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR?fallback}";
114+
var target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR??fallback}";
115115
var actual = target.FormatWith(propertyObject);
116116
Assert.AreEqual(target, actual);
117117
}
118+
119+
[Test]
120+
public void FormatWIth_NullPropagationWithMultipleSpaces()
121+
{
122+
var propertyObject = new { SomeProperty = "Some Value" };
123+
var target = "{SomeProperty} and {env:DUMMY_ENV_VAR ?? fallback}";
124+
var expected = "Some Value and fallback";
125+
var actual = target.FormatWith(propertyObject);
126+
Assert.AreEqual(expected, actual);
127+
}
118128
}
119129
}

src/GitVersionCore/StringFormatWith.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GitVersion
88

99
static class StringFormatWithExtension
1010
{
11-
private static readonly Regex TokensRegex = new Regex(@"{(env:)??\w+(\?\?\w+)??}", RegexOptions.Compiled);
11+
private static readonly Regex TokensRegex = new Regex(@"{(env:)??\w+(\s+(\?\?)??\s+\w+)??}", RegexOptions.Compiled);
1212

1313
/// <summary>
1414
/// Formats a string template with the given source object.
@@ -43,8 +43,8 @@ public static string FormatWith<T>(this string template, T source)
4343
string[] components = (memberAccessExpression.Contains("??")) ? memberAccessExpression.Split(new string[] { "??" }, StringSplitOptions.None) : null;
4444
if (components != null)
4545
{
46-
envVar = components[0];
47-
fallback = components[1];
46+
envVar = components[0].Trim();
47+
fallback = components[1].Trim();
4848
}
4949

5050
propertyValue = Helpers.EnvironmentHelper.GetEnvironmentVariableForProcess(envVar);

0 commit comments

Comments
 (0)