Skip to content

Commit

Permalink
Update SemVerFactory to handle 'v' prefix (#67)
Browse files Browse the repository at this point in the history
* Add tests

* Update factory method
  • Loading branch information
scme0 authored Nov 16, 2023
1 parent 1314c08 commit 1b8c9db
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 47 deletions.
72 changes: 36 additions & 36 deletions source/Octopus.Versioning.Tests/Octopus/OctopusVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ public void TryParseTest()
"",
"",
"")]
[TestCase("v0.0.0",
0,
0,
0,
0,
"",
"",
"",
"")]
[TestCase("v0.0.1",
0,
0,
1,
0,
"",
"",
"",
"")]
[TestCase("v1.0.0",
1,
0,
0,
0,
"",
"",
"",
"")]
[TestCase("v0.0.0-foo",
0,
0,
0,
0,
"foo",
"foo",
"",
"")]
[TestCase("1.1.2-prerelease+meta",
1,
1,
Expand Down Expand Up @@ -759,15 +795,6 @@ public void TestIncompatibleMavenVersions(string version,
"beta",
"",
"")]
[TestCase("v0.0.0",
0,
0,
0,
0,
"",
"",
"",
"")]
[TestCase("V0.0.0",
0,
0,
Expand All @@ -777,15 +804,6 @@ public void TestIncompatibleMavenVersions(string version,
"",
"",
"")]
[TestCase("v0.0.1",
0,
0,
1,
0,
"",
"",
"",
"")]
[TestCase("V0.0.1",
0,
0,
Expand All @@ -795,24 +813,6 @@ public void TestIncompatibleMavenVersions(string version,
"",
"",
"")]
[TestCase("v1.0.0",
1,
0,
0,
0,
"",
"",
"",
"")]
[TestCase("v0.0.0-foo",
0,
0,
0,
0,
"foo",
"foo",
"",
"")]
[TestCase("V0.0.0-foo",
0,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,22 @@ public void TestMismatchingVersionsHashCodesAreDifferent()

Assert.AreNotEqual(ver1.GetHashCode(), ver2.GetHashCode());
}

[Test]
public void TestVersionWithPrefixedVIsSameAsWithout()
{
var ver1 = VersionFactory.CreateSemanticVersion("v1.2.3");
var ver2 = VersionFactory.CreateSemanticVersion("1.2.3");

Assert.AreEqual(ver1.GetHashCode(), ver2.GetHashCode());
}

[Test]
public void VIsInOriginalVersionWhenSemVerHasPrefixV()
{
var ver1 = VersionFactory.CreateSemanticVersion("v1.2.3");

Assert.AreEqual(ver1.OriginalString, "v1.2.3");
}
}
}
23 changes: 12 additions & 11 deletions source/Octopus.Versioning/Semver/SemVerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ public static SemanticVersion Parse(string value, bool preserveMissingComponents
/// </summary>
public static SemanticVersion? TryCreateVersion(string value, bool preserveMissingComponents = false)
{
SemanticVersion? version = null;
// trim the value before passing it in since we're not strict here
value = value?.Trim();
var originalVersion = value;

if (value != null)
// also trim the leading v if it exists
if (value?.FirstOrDefault() == 'v')
{
Version? systemVersion = null;
value = value.Substring(1);
}

// trim the value before passing it in since we not strict here
var sections = utils.ParseSections(value.Trim());
if (!string.IsNullOrWhiteSpace(value))
{
var sections = utils.ParseSections(value);

// null indicates the string did not meet the rules
if (sections != null
Expand All @@ -65,7 +70,7 @@ public static SemanticVersion Parse(string value, bool preserveMissingComponents
// System.Version requires at least a 2 part version to parse.
versionPart += ".0";

if (Version.TryParse(versionPart, out systemVersion))
if (Version.TryParse(versionPart, out var systemVersion))
{
// labels
if (sections.Item2 != null
Expand All @@ -81,17 +86,13 @@ public static SemanticVersion Parse(string value, bool preserveMissingComponents
? systemVersion
: utils.NormalizeVersionValue(systemVersion);

var originalVersion = value;

if (originalVersion.IndexOf(' ') > -1)
originalVersion = value.Replace(" ", "");

version = new SemanticVersion(ver,
return new SemanticVersion(ver,
sections.Item2,
sections.Item3 ?? string.Empty,
originalVersion);

return version;
}
}
}
Expand Down

0 comments on commit 1b8c9db

Please sign in to comment.