From cbf0f12aac82bc06b232d0bda245508c947b00e9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 12 Apr 2015 10:59:17 -0700 Subject: [PATCH] Create `nvm_print_npm_version` and use that when printing the `npm` version. --- nvm.sh | 22 +++++++-------- ..."nvm use system\" should work as expected" | 8 ++++-- test/fast/Unit tests/nvm_print_npm_version | 28 +++++++++++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) create mode 100755 test/fast/Unit tests/nvm_print_npm_version diff --git a/nvm.sh b/nvm.sh index 3d6f979..2c0c255 100755 --- a/nvm.sh +++ b/nvm.sh @@ -58,6 +58,12 @@ nvm_has_system_iojs() { [ "$(nvm deactivate >/dev/null 2>&1 && command -v iojs)" != '' ] } +nvm_print_npm_version() { + if nvm_has "npm"; then + npm --version 2>/dev/null | command xargs printf " (npm v%s)" + fi +} + # Make zsh glob matching behave same as bash # This fixes the "zsh: no matches found" errors if nvm_has "unsetopt"; then @@ -1359,15 +1365,11 @@ nvm() { fi if [ "_$VERSION" = '_system' ]; then - local NPM_VERSION - if nvm_has "npm"; then - NPM_VERSION="(npm v$(npm --version 2>/dev/null))" - fi if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then - echo "Now using system version of node: $(node -v 2>/dev/null) $NPM_VERSION" + echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)" return elif nvm_has_system_iojs && nvm deactivate >/dev/null 2>&1; then - echo "Now using system version of io.js: $(iojs --version 2>/dev/null) $NPM_VERSION" + echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)" return else echo "System version of node not found." >&2 @@ -1410,14 +1412,10 @@ nvm() { if [ "$NVM_SYMLINK_CURRENT" = true ]; then command rm -f "$NVM_DIR/current" && ln -s "$NVM_VERSION_DIR" "$NVM_DIR/current" fi - local NPM_VERSION - if nvm_has "npm"; then - NPM_VERSION="(npm v$(npm --version 2>/dev/null))" - fi if nvm_is_iojs_version "$VERSION"; then - echo "Now using io.js $(nvm_strip_iojs_prefix "$VERSION") $NPM_VERSION" + echo "Now using io.js $(nvm_strip_iojs_prefix "$VERSION")$(nvm_print_npm_version)" else - echo "Now using node $VERSION $NPM_VERSION" + echo "Now using node $VERSION$(nvm_print_npm_version)" fi ;; "run" ) diff --git "a/test/fast/Running \"nvm use system\" should work as expected" "b/test/fast/Running \"nvm use system\" should work as expected" index 2ed9638..86a0955 100755 --- "a/test/fast/Running \"nvm use system\" should work as expected" +++ "b/test/fast/Running \"nvm use system\" should work as expected" @@ -5,9 +5,13 @@ die () { echo $@ ; exit 1; } . ../../nvm.sh nvm_has_system_node() { return 0; } -[ "$(nvm use system 2>&1 | tail -n1)" = "Now using system version of node: $(node -v) (npm v$(npm -v))" ] || die "Could not use system version of node" +nvm_print_npm_version() { return ' (npm v1.2.3)'; } +EXPECTED_OUTPUT="Now using system version of node: $(node -v)$(nvm_print_npm_version)" +[ "$(nvm use system 2>&1 | tail -n1)" = "$EXPECTED_OUTPUT" ] || die "Could not use system version of node" nvm_has_system_node() { return 1; } -[ "$(nvm use system 2>&1 | tail -n1)" = "System version of node not found." ] || die "Did not report error, system node not found" +nvm_print_npm_version() { return ''; } +EXPECTED_OUTPUT="System version of node not found." +[ "$(nvm use system 2>&1 | tail -n1)" = "$EXPECTED_OUTPUT" ] || die "Did not report error, system node not found" nvm use system 2>&1 > /dev/null || [ $? -eq 127 ] || die "Did not return error code, system node not found" diff --git a/test/fast/Unit tests/nvm_print_npm_version b/test/fast/Unit tests/nvm_print_npm_version new file mode 100755 index 0000000..2bf24a0 --- /dev/null +++ b/test/fast/Unit tests/nvm_print_npm_version @@ -0,0 +1,28 @@ +#!/bin/sh + +cleanup () { + alias nvm_has='\nvm_has' + alias npm='\npm' + unset -f nvm_has npm +} +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +nvm_has() { return 1; } +OUTPUT="$(nvm_print_npm_version)" +[ -z "$OUTPUT" ] || die "nvm_print_npm_version did not return empty when nvm_has returns 1, got '$OUTPUT'" + +nvm_has() { return 0; } +npm() { + if [ "_$@" = "_--version" ]; then + echo "1.2.3" + else + echo "error" + fi +} +OUTPUT="$(nvm_print_npm_version)" +EXPECTED_OUTPUT=" (npm v1.2.3)" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_npm_version did not provided '$EXPECTED_OUTPUT', got '$OUTPUT'" + +cleanup