Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SemVerHelper.isValidSemVer(string) #811

Merged
merged 1 commit into from
May 26, 2015
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
12 changes: 12 additions & 0 deletions src/app/FakeLib/SemVerHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ type SemVerInfo =
0
| _ -> invalidArg "yobj" "cannot compare values of different types"


let private SemVerPattern = "^(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)(?:-[\da-zA-Z\-]+(?:\.[\da-zA-Z\-]+)*)?(?:\+[\da-zA-Z\-]+(?:\.[\da-zA-Z\-]+)*)?$"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be part of our normal Semver parser?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're saying we should throw an exception during parse for parsable but technically invalid semver strings? Int32.Parse doesn't align with the spec, particularly if we think about culture. While we can improve parse (and possibly make it more strict), some things (like prohibiting leading zeroes) are more complex without a regex.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok let's ask differently. Why do you need this new function? What's the use
case?
On May 25, 2015 4:40 PM, "Nathanael Jones" notifications@github.com wrote:

In src/app/FakeLib/SemVerHelper.fs
#811 (comment):

@@ -76,6 +76,15 @@ type SemVerInfo =
0
| _ -> invalidArg "yobj" "cannot compare values of different types"

+let private SemVerPattern = "^(?:0|[1-9][0-9]).(?:0|[1-9][0-9]).(?:0|[1-9][0-9])(?:-[\da-zA-Z-]+(?:.[\da-zA-Z-]+))?(?:+[\da-zA-Z-]+(?:.[\da-zA-Z-]+)*)?$"

You're saying we should throw an exception during parse for parsable but
technically invalid semver strings? Int32.Parse doesn't align with the
spec, particularly if we think about culture. While we can improve parse
(and possibly make it more strict), some things (like prohibiting leading
zeroes) are more complex without a regex.


Reply to this email directly or view it on GitHub
https://github.com/fsharp/FAKE/pull/811/files#r30982897.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use it to determine if a git tag is a semver. If it's a semver, then the build can take additional actions. Very useful for continuous delivery.


/// Returns true if input appears to be a parsable semver string
let isValidSemVer input =
let m = Regex.Match(input, SemVerPattern)
if m.Success then true
else false

/// Parses the given version string into a SemVerInfo which can be printed using ToString() or compared
/// according to the rules described in the [SemVer docs](http://semver.org/).
/// ## Sample
Expand All @@ -100,3 +109,6 @@ let parse version =
PreRelease = PreRelease.TryParse preRelease
Build = if l > 3 then splitted.[3] else ""
}



24 changes: 24 additions & 0 deletions src/test/Test.FAKECore/SemVerHelperSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ public class when_parsing_semver_strings_and_printing_the_result
() => SemVerHelper.parse("1.2.3-foo").ToString().ShouldEqual("1.2.3-foo");
}

public class when_validating_semver_strings
{
It should_validate_0_1_2 =
() => SemVerHelper.isValidSemVer("0.1.2").ShouldEqual(true);

It should_validate_alpha_beta_versions =
() => SemVerHelper.isValidSemVer("1.0.0-alpha.beta").ShouldEqual(true);

It should_validate_prerelease_versions_without_build =
() => SemVerHelper.isValidSemVer("1.2.3-foo").ShouldEqual(true);

It should_reject_leading_zeros =
() => SemVerHelper.isValidSemVer("01.02.03").ShouldEqual(false);

It should_require_3_numbers =
() => SemVerHelper.isValidSemVer("1.2").ShouldEqual(false);

It should_reject_leading_v =
() => SemVerHelper.isValidSemVer("v1.2.0").ShouldEqual(false);


}


public class when_parsing_semver_strings
{
static SemVerHelper.SemVerInfo semVer;
Expand Down