Skip to content

Commit

Permalink
bug: Use * as part of a label string
Browse files Browse the repository at this point in the history
Fixes #61

(cherry picked from commit 1c6a52a)

# Conflicts:
#	src/SimpleVersion.Core/Rules/HeightRule.cs
#	test/SimpleVersion.Core.Tests/Pipeline/Formatting/Semver2FormatProcessFixture.cs
  • Loading branch information
Kieranties committed Apr 30, 2019
1 parent 9c548f4 commit 19f4a56
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 26 deletions.
48 changes: 48 additions & 0 deletions src/SimpleVersion.Core/Rules/HeightRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using SimpleVersion.Pipeline;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SimpleVersion.Rules
{
public class HeightRule : IRule<string>
{
private static Lazy<HeightRule> _default = new Lazy<HeightRule>(() => new HeightRule());

public static HeightRule Instance => _default.Value;

public HeightRule() :this(false)
{

}

public HeightRule(bool padded)
{
Padded = padded;
}

public bool Padded { get; }

public string Token => "*";

public string Resolve(VersionContext context, string value)
{
if (Padded)
return value.Replace(Token, context.Result.HeightPadded);
else
return value.Replace(Token, context.Result.Height.ToString());
}

public IEnumerable<string> Apply(VersionContext context, IEnumerable<string> input)
{
if (!context.Configuration.Version.Contains(Token)
&& input.Count() != 0
&& !input.Any(x => x.Contains(Token)))
{
return input.Concat(new[] { Token });
}

return input;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public static IEnumerable<object[]> LabelParts()
yield return new object[] { new[] { "one", "two" }, "1.2.0", 106, "1.2.0-one-two-0106" };
yield return new object[] { new[] { "*", "one", "two" }, "1.2.0", 106, "1.2.0-0106-one-two" };
yield return new object[] { new[] { "one", "*", "two" }, "1.2.0", 106, "1.2.0-one-0106-two" };
yield return new object[] { new[] { "one", "two*", "three" }, "1.2.0", 106, "1.2.0-one-two0106-three" };
yield return new object[] { new[] { "one", "*two*", "three" }, "1.2.0", 106, "1.2.0-one-0106two0106-three" };
yield return new object[] { new[] { "one", "*t*o*", "three" }, "1.2.0", 106, "1.2.0-one-0106t0106o0106-three" };
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Licensed under the MIT license. See https://kieranties.mit-license.org/ for full license information.

using FluentAssertions;
using FluentAssertions;
using SimpleVersion.Pipeline;
using SimpleVersion.Pipeline.Formatting;
using System;
Expand All @@ -20,20 +18,23 @@ public Semver2FormatProcessFixture()

public static IEnumerable<object[]> LabelParts()
{
return new List<object[]>
{
new object[] { Array.Empty<object>(), "1.2.0", 10 },
new object[] { new[] { "one" }, "1.2.0", 10 },
new object[] { new[] { "one", "two" }, "1.2.0", 106 }
};
yield return new object[] { Array.Empty<object>(), "1.2.0", 10, "1.2.0" };
yield return new object[] { new[] { "one" }, "1.2.0", 10, "1.2.0-one.10" };
yield return new object[] { new[] { "one", "two" }, "1.2.0", 106, "1.2.0-one.two.106" };
yield return new object[] { new[] { "*", "one", "two" }, "1.2.0", 106, "1.2.0-106.one.two" };
yield return new object[] { new[] { "one", "*", "two" }, "1.2.0", 106, "1.2.0-one.106.two" };
yield return new object[] { new[] { "one", "two*", "three" }, "1.2.0", 106, "1.2.0-one.two106.three" };
yield return new object[] { new[] { "one", "*two*", "three" }, "1.2.0", 106, "1.2.0-one.106two106.three" };
yield return new object[] { new[] { "one", "*t*o*", "three" }, "1.2.0", 106, "1.2.0-one.106t106o106.three" };
}

[Theory]
[MemberData(nameof(LabelParts))]
public void Apply_LabelParts_NonRelease_Is_Formatted(
string[] parts,
string version,
int height)
int height,
string expectedPart)
{
// Arrange
var context = new VersionContext
Expand All @@ -43,26 +44,26 @@ public void Apply_LabelParts_NonRelease_Is_Formatted(
};
context.Result.Version = context.Configuration.Version;

string expected;
if (parts.Length > 0)
expected = $"{version}-{string.Join(".", parts)}.{height}.c4ca82d2";
else
expected = $"{version}-c4ca82d2";
var divider = parts.Length > 0 ? '.' : '-';
var shaSub = context.Result.Sha.Substring(0, 7);
var fullExpected = $"{expectedPart}{divider}c{shaSub}";

// Act
_sut.Apply(context);

// Assert
context.Result.Formats.Should().ContainKey("Semver2");
context.Result.Formats["Semver2"].Should().Be(expected);
context.Result.Formats["Semver2"].Should().Be(fullExpected);
}


[Theory]
[MemberData(nameof(LabelParts))]
public void Apply_LabelParts_Release_Is_Formatted(
string[] parts,
string version,
int height)
int height,
string expected)
{
// Arrange
var context = new VersionContext
Expand All @@ -71,13 +72,7 @@ public void Apply_LabelParts_Release_Is_Formatted(
Result = Utils.GetVersionResult(height)
};
context.Result.Version = context.Configuration.Version;

string expected;
if (parts.Length > 0)
expected = $"{version}-{string.Join(".", parts)}.{height}";
else
expected = $"{version}";


// Act
_sut.Apply(context);

Expand Down Expand Up @@ -107,12 +102,13 @@ public void Apply_MetaDataParts_NonRelease_Is_Formatted(
Result = Utils.GetVersionResult(height, false)
};
context.Result.Version = context.Configuration.Version;
var shaSub = context.Result.Sha.Substring(0, 7);

string expected;
if (parts.Length > 0)
expected = $"{version}-c4ca82d2+{string.Join(".", parts)}";
expected = $"{version}-c{shaSub}+{string.Join(".", parts)}";
else
expected = $"{version}-c4ca82d2";
expected = $"{version}-c{shaSub}";

// Act
_sut.Apply(context);
Expand All @@ -129,6 +125,7 @@ public void Apply_MetaDataParts_Release_Is_Formatted(
string version,
int height)
{

// Arrange
var context = new VersionContext
{
Expand Down

0 comments on commit 19f4a56

Please sign in to comment.