Skip to content

Commit

Permalink
Adding validate command (#62)
Browse files Browse the repository at this point in the history
* Adding validate command

Fixes: #61

* Update README.md

* Update src/semver

Co-authored-by: François Saint-Jacques <[email protected]>
  • Loading branch information
mymasse and fsaintjacques authored Jan 14, 2022
1 parent 3eeb2d1 commit 1306402
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 39 deletions.
50 changes: 31 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Usage:
semver compare <version> <other_version>
semver diff <version> <other_version>
semver get (major|minor|patch|release|prerel|build) <version>
semver validate <version>
semver --help
semver --version
Expand Down Expand Up @@ -91,25 +92,29 @@ Options:
-h, --help Print this help message.
Commands:
bump Bump by one of major, minor, patch; zeroing or removing
subsequent parts. "bump prerel" sets the PRERELEASE part and
removes any BUILD part. A trailing dot in the <prerel> argument
introduces an incrementing numeric field which is added or
bumped. If no <prerel> argument is provided, an incrementing numeric
field is introduced/bumped. "bump build" sets the BUILD part.
"bump release" removes any PRERELEASE or BUILD parts.
The bumped version is written to stdout.
compare Compare <version> with <other_version>, output to stdout the
following values: -1 if <other_version> is newer, 0 if equal, 1 if
older. The BUILD part is not used in comparisons.
diff Compare <version> with <other_version>, output to stdout the
difference between two versions by the release type (MAJOR, MINOR,
PATCH, PRERELEASE, BUILD).
get Extract given part of <version>, where part is one of major, minor,
patch, prerel, build, or release.
bump Bump by one of major, minor, patch; zeroing or removing
subsequent parts. "bump prerel" sets the PRERELEASE part and
removes any BUILD part. A trailing dot in the <prerel> argument
introduces an incrementing numeric field which is added or
bumped. If no <prerel> argument is provided, an incrementing numeric
field is introduced/bumped. "bump build" sets the BUILD part.
"bump release" removes any PRERELEASE or BUILD parts.
The bumped version is written to stdout.
compare Compare <version> with <other_version>, output to stdout the
following values: -1 if <other_version> is newer, 0 if equal, 1 if
older. The BUILD part is not used in comparisons.
diff Compare <version> with <other_version>, output to stdout the
difference between two versions by the release type (MAJOR, MINOR,
PATCH, PRERELEASE, BUILD).
get Extract given part of <version>, where part is one of major, minor,
patch, prerel, build, or release.
validate Validate if <version> follows the SEMVER pattern (see <version>
definition). Print 'valid' to stdout if the version is valid, otherwise
print 'invalid'.
See also:
https://semver.org -- Semantic Versioning 2.0.0
Expand Down Expand Up @@ -189,3 +194,10 @@ Extract version part
rc.4
$ semver get prerel 1.2.3-rc-4+build.570
rc-4

Validate

$ semver validate 1.0.0
valid
$ semver validate 1
invalid
60 changes: 40 additions & 20 deletions src/semver
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ SEMVER_REGEX="\
(\\+${FIELD}(\\.${FIELD})*)?$"

PROG=semver
PROG_VERSION="3.2.0"
PROG_VERSION="3.3.0"

USAGE="\
Usage:
$PROG bump (major|minor|patch|release|prerel [<prerel>]|build <build>) <version>
$PROG compare <version> <other_version>
$PROG diff <version> <other_version>
$PROG get (major|minor|patch|release|prerel|build) <version>
$PROG validate <version>
$PROG --help
$PROG --version
Expand Down Expand Up @@ -50,25 +51,29 @@ Options:
-h, --help Print this help message.
Commands:
bump Bump by one of major, minor, patch; zeroing or removing
subsequent parts. \"bump prerel\" sets the PRERELEASE part and
removes any BUILD part. A trailing dot in the <prerel> argument
introduces an incrementing numeric field which is added or
bumped. If no <prerel> argument is provided, an incrementing numeric
field is introduced/bumped. \"bump build\" sets the BUILD part.
\"bump release\" removes any PRERELEASE or BUILD parts.
The bumped version is written to stdout.
compare Compare <version> with <other_version>, output to stdout the
following values: -1 if <other_version> is newer, 0 if equal, 1 if
older. The BUILD part is not used in comparisons.
diff Compare <version> with <other_version>, output to stdout the
difference between two versions by the release type (MAJOR, MINOR,
PATCH, PRERELEASE, BUILD).
get Extract given part of <version>, where part is one of major, minor,
patch, prerel, build, or release.
bump Bump by one of major, minor, patch; zeroing or removing
subsequent parts. \"bump prerel\" sets the PRERELEASE part and
removes any BUILD part. A trailing dot in the <prerel> argument
introduces an incrementing numeric field which is added or
bumped. If no <prerel> argument is provided, an incrementing numeric
field is introduced/bumped. \"bump build\" sets the BUILD part.
\"bump release\" removes any PRERELEASE or BUILD parts.
The bumped version is written to stdout.
compare Compare <version> with <other_version>, output to stdout the
following values: -1 if <other_version> is newer, 0 if equal, 1 if
older. The BUILD part is not used in comparisons.
diff Compare <version> with <other_version>, output to stdout the
difference between two versions by the release type (MAJOR, MINOR,
PATCH, PRERELEASE, BUILD).
get Extract given part of <version>, where part is one of major, minor,
patch, prerel, build, or release.
validate Validate if <version> follows the SEMVER pattern (see <version>
definition). Print 'valid' to stdout if the version is valid, otherwise
print 'invalid'.
See also:
https://semver.org -- Semantic Versioning 2.0.0"
Expand Down Expand Up @@ -384,6 +389,20 @@ function command_get {
exit 0
}

function command_validate {
if [[ "$#" -ne "1" ]]; then
usage_help
fi

if [[ "$1" =~ $SEMVER_REGEX ]]; then
echo "valid"
else
echo "invalid"
fi

exit 0
}

case $# in
0) echo "Unknown command: $*"; usage_help;;
esac
Expand All @@ -395,5 +414,6 @@ case $1 in
get) shift; command_get "$@";;
compare) shift; command_compare "$@";;
diff) shift; command_diff "$@";;
validate) shift; command_validate "$@";;
*) echo "Unknown arguments: $*"; usage_help;;
esac
54 changes: 54 additions & 0 deletions test/semver.bats
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,57 @@ SEMVER="src/semver"
[ "$status" -eq 1 ]
}

@test "missing arg in validate" {
run $SEMVER validate
[ "$status" -eq 1 ]
}

@test "correct arg in validate" {
run $SEMVER validate 1.2.2
[ "$status" -eq 0 ]
}

@test "too many args in validate" {
run $SEMVER validate 1.2.2 1.0.0
[ "$status" -eq 1 ]
}

@test "invalid semver with major only" {
result="$($SEMVER validate 1)"
[ "$result" = "invalid" ]
}

@test "invalid semver with minor only" {
result="$($SEMVER validate 1.1)"
[ "$result" = "invalid" ]
}

@test "valid semver with major/minor/patch" {
result="$($SEMVER validate 1.1.1)"
[ "$result" = "valid" ]
}

@test "invalid semver with pre-release" {
result="$($SEMVER validate 1.1.1-)"
[ "$result" = "invalid" ]
}

@test "valid semver with pre-release" {
result="$($SEMVER validate 1.1.1-1)"
[ "$result" = "valid" ]
}

@test "invalid semver with build" {
result="$($SEMVER validate 1.1.1+)"
[ "$result" = "invalid" ]
}

@test "valid semver with build" {
result="$($SEMVER validate 1.1.1+1)"
[ "$result" = "valid" ]
}

@test "valid semver with pre-release and build" {
result="$($SEMVER validate 1.1.1-1+1)"
[ "$result" = "valid" ]
}

0 comments on commit 1306402

Please sign in to comment.