Add `nvm_get_minor_version`

Jordan Harband 2016-02-04 11:16:09 -08:00
parent de64a2d95f
commit 731c2f3f70
2 changed files with 73 additions and 0 deletions

28
nvm.sh
View File

@ -995,6 +995,34 @@ nvm_get_arch() {
echo "$NVM_ARCH" echo "$NVM_ARCH"
} }
nvm_get_minor_version() {
local VERSION
VERSION="$1"
if [ -z "$VERSION" ]; then
echo 'a version is required' >&2
return 1
fi
case "$VERSION" in
v | .* | *..* | v*[!.0123456789]* | [!v]*[!.0123456789]* | [!v0123456789]* | v[!0123456789]*)
echo 'invalid version number' >&2
return 2
;;
esac
local PREFIXED_VERSION
PREFIXED_VERSION="$(nvm_format_version "$VERSION")"
local MINOR
MINOR="$(echo "$PREFIXED_VERSION" | command grep -e '^v' | command cut -c2- | command cut -d . -f 1,2)"
if [ -z "$MINOR" ]; then
echo 'invalid version number! (please report this)' >&2
return 3
fi
echo "$MINOR"
}
nvm_ensure_default_set() { nvm_ensure_default_set() {
local VERSION local VERSION
VERSION="$1" VERSION="$1"

View File

@ -0,0 +1,45 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
expect () {
INPUT="$1"
EXPECTED_OUTPUT="$2"
OUTPUT="$(nvm_get_minor_version "$INPUT")"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_get_minor_version $INPUT did not return $EXPECTED_OUTPUT; got $OUTPUT"
V_OUTPUT="$(nvm_get_minor_version "v$INPUT")"
[ "_$V_OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_get_minor_version v$INPUT did not return $EXPECTED_OUTPUT; got $V_OUTPUT"
}
fail_with () {
INPUT="$1"
EXPECTED_CODE="$2"
EXIT_CODE="$(nvm_get_minor_version "$INPUT" >/dev/null 2>&1; echo $?)"
[ "_$EXIT_CODE" = "_$EXPECTED_CODE" ] || die "nvm_get_minor_version "$INPUT" did not fail with code "$EXPECTED_CODE"; got $EXIT_CODE"
}
expect 1 1.0
expect 1. 1.0
expect 1.2 1.2
expect 1.2. 1.2
expect 1.2.3 1.2
expect 1.2.3. 1.2
expect 1.2.3.4 1.2
fail_with '' 1
fail_with '.' 2
fail_with '..' 2
fail_with v 2
fail_with .a 2
fail_with .1 2
fail_with v.1 2
fail_with a.b 2
fail_with 1.a 2
fail_with a.1 2
fail_with v1.a 2
fail_with va.1 2