Skip to content

Commit

Permalink
Updated OctopusVersionParser regex
Browse files Browse the repository at this point in the history
- Now requires a word boundary directly after the version numbers in the version string, prevents input strings like 123ABC from parsing as 123.0.0.0-ABC
  • Loading branch information
tleed5 committed Jan 4, 2024
1 parent 4fcaa4f commit 1324ce2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
72 changes: 44 additions & 28 deletions source/Octopus.Versioning.Tests/Octopus/OctopusVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,32 +683,6 @@ public void TestMavenVersions(string version,
"0",
"alpha",
"")]
[TestCase("1_0_alpha",
1,
0,
0,
0,
0,
0,
0,
"",
"0_alpha",
"0",
"alpha",
"")]
[TestCase("1_0alpha",
1,
0,
0,
0,
0,
0,
0,
"",
"0alpha",
"0alpha",
"",
"")]
[TestCase("1.0a1-SNAPSHOT",
1,
0,
Expand All @@ -718,8 +692,8 @@ public void TestMavenVersions(string version,
0,
0,
"",
"a1-SNAPSHOT",
"a1",
"0a1-SNAPSHOT",
"0a1",
"SNAPSHOT",
"")]
[TestCase("1-2.3-SNAPSHOT",
Expand Down Expand Up @@ -1076,12 +1050,54 @@ public void TestOriginalStringIsTheSame(string version)
Assert.AreEqual(octoVersion.OriginalString, semanticVersion.OriginalString);
}

[Test]
[TestCase("123",
123,
0,
0,
0,
"")]
[TestCase("123ABC",
0,
0,
0,
0,
"123ABC")]
[TestCase("123ABC-CBC",
0,
0,
0,
0,
"123ABC-CBC")]
[TestCase("123.123.123.123",
123,
123,
123,
123,
"")]
[TestCase("123.123ABC.123",
123,
0,
0,
0,
"123ABC.123")]
public void TestVersionNumbersAreDelimited(string inputVersion, int major, int minor, int patch, int revision, string release)
{
var parsedVersion = OctopusVersionParser.Parse(inputVersion);
Assert.AreEqual(major, parsedVersion.Major);
Assert.AreEqual(minor, parsedVersion.Minor);
Assert.AreEqual(patch, parsedVersion.Patch);
Assert.AreEqual(revision, parsedVersion.Revision);
Assert.AreEqual(release, parsedVersion.Release);
}

public static string RandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.\\+";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[Random.Next(s.Length)])
.ToArray());
}

}
}
8 changes: 4 additions & 4 deletions source/Octopus.Versioning/Octopus/OctopusVersionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public class OctopusVersionParser
// Versions can start with an optional V
@$"\s*(?<{Prefix}>v|V)?" +
// Get the major version number
@$"\s*(?<{Major}>\d+)\s*" +
@$"\s*(?<{Major}>\d+\b)\s*" +
// Get the minor version number, delimited by a period, comma, dash or underscore
@$"(?:\.\s*(?<{Minor}>\d+)\s*)?" +
@$"(?:\.\s*(?<{Minor}>\d+\b)\s*)?" +
// Get the patch version number, delimited by a period, comma, dash or underscore
@$"(?:\.\s*(?<{Patch}>\d+)\s*)?" +
@$"(?:\.\s*(?<{Patch}>\d+\b)\s*)?" +
// Get the revision version number, delimited by a period, comma, dash or underscore
@$"(?:\.\s*(?<{Revision}>\d+)\s*)?)?" +
@$"(?:\.\s*(?<{Revision}>\d+\b)\s*)?)?" +
// Everything after the last digit and before the plus is the prerelease
@$"(?:[.\-_\\])?(?<{Prerelease}>(?<{PrereleasePrefix}>[A-Za-z0-9]*?)([.\-_\\](?<{PrereleaseCounter}>[A-Za-z0-9.\-_\\]*?)?)?)?" +
// The metadata is everything after the plus
Expand Down

0 comments on commit 1324ce2

Please sign in to comment.