From 82716427165106cb17f596c191b4e64edc9e1687 Mon Sep 17 00:00:00 2001 From: Nathanael Jones Date: Sat, 23 May 2015 11:35:41 -0400 Subject: [PATCH] Add SemVerHelper.isValidSemVer(string) Allows build scripts to check if a git tag or input string is a semver. --- src/app/FakeLib/SemVerHelper.fs | 12 +++++++++++ src/test/Test.FAKECore/SemVerHelperSpecs.cs | 24 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/app/FakeLib/SemVerHelper.fs b/src/app/FakeLib/SemVerHelper.fs index c782ce8fd36..134dc791f11 100644 --- a/src/app/FakeLib/SemVerHelper.fs +++ b/src/app/FakeLib/SemVerHelper.fs @@ -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\-]+)*)?$" + +/// 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 @@ -100,3 +109,6 @@ let parse version = PreRelease = PreRelease.TryParse preRelease Build = if l > 3 then splitted.[3] else "" } + + + diff --git a/src/test/Test.FAKECore/SemVerHelperSpecs.cs b/src/test/Test.FAKECore/SemVerHelperSpecs.cs index 0e543b265dc..8a3de15ba47 100644 --- a/src/test/Test.FAKECore/SemVerHelperSpecs.cs +++ b/src/test/Test.FAKECore/SemVerHelperSpecs.cs @@ -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;