From de64a2d95f66a6fd17f8619d59ca6047d79aaee5 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 6 Feb 2016 22:34:49 -0800 Subject: [PATCH] [Fix] `nvm_format_version` should only return 3 version groups --- nvm.sh | 6 ++++-- test/fast/Unit tests/nvm_format_version | 15 +++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/nvm.sh b/nvm.sh index b8ffb0d..a74414d 100755 --- a/nvm.sh +++ b/nvm.sh @@ -339,10 +339,12 @@ nvm_ensure_version_prefix() { nvm_format_version() { local VERSION VERSION="$(nvm_ensure_version_prefix "$1")" - if [ "_$(nvm_num_version_groups "$VERSION")" != "_3" ]; then + local NUM_GROUPS + NUM_GROUPS="$(nvm_num_version_groups "$VERSION")" + if [ $NUM_GROUPS -lt 3 ]; then nvm_format_version "${VERSION%.}.0" else - echo "$VERSION" + echo "$VERSION" | cut -f1-3 -d. fi } diff --git a/test/fast/Unit tests/nvm_format_version b/test/fast/Unit tests/nvm_format_version index 134ca75..e77b3da 100755 --- a/test/fast/Unit tests/nvm_format_version +++ b/test/fast/Unit tests/nvm_format_version @@ -4,11 +4,18 @@ die () { echo $@ ; exit 1; } . ../../../nvm.sh -OUTPUT="$(nvm_format_version 0.1.2)" +INPUT="0.1.2" +OUTPUT="$(nvm_format_version "$INPUT")" EXPECTED_OUTPUT="v0.1.2" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_format_version 0.1.2 did not return $EXPECTED_OUTPUT; got $OUTPUT" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_format_version $INPUT did not return $EXPECTED_OUTPUT; got $OUTPUT" -OUTPUT="$(nvm_format_version 0.1)" +INPUT="0.1" +OUTPUT="$(nvm_format_version "$INPUT")" EXPECTED_OUTPUT="v0.1.0" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_format_version 0.1.0 did not return $EXPECTED_OUTPUT; got $OUTPUT" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_format_version $INPUT did not return $EXPECTED_OUTPUT; got $OUTPUT" + +INPUT="1.2.3.4.5" +OUTPUT="$(nvm_format_version "$INPUT")" +EXPECTED_OUTPUT="v1.2.3" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_format_version $INPUT did not return $EXPECTED_OUTPUT; got $OUTPUT"