Merge pull request #618 from mikeal/patch-1

Adding iojs to README.
diff --git a/nvm.sh b/nvm.sh
index 70b6c7c..fbbf663 100644
--- a/nvm.sh
+++ b/nvm.sh
@@ -217,18 +217,52 @@
   local PATTERN
   PATTERN="$1"
   local VERSION
-  if nvm_is_iojs_version "$PATTERN"; then
-    VERSION="$(nvm_ls_remote_iojs "$PATTERN")"
+  if nvm_validate_implicit_alias "$PATTERN" 2> /dev/null ; then
+    VERSIONS="$(nvm_ls_remote "$PATTERN")"
   else
-    VERSION="$(nvm_ls_remote "$PATTERN")"
+    case "_$PATTERN" in
+      "_$(nvm_node_prefix)")
+        VERSION="$(nvm_ls_remote stable)"
+      ;;
+      *)
+        VERSION="$(nvm_remote_versions "$PATTERN" | tail -n1)"
+      ;;
+    esac
   fi
-  echo "$VERSION" | tail -n1
-
+  echo "$VERSION"
   if [ "_$VERSION" = '_N/A' ]; then
     return 3
   fi
 }
 
+nvm_remote_versions() {
+  local PATTERN
+  PATTERN="$1"
+  if nvm_validate_implicit_alias "$PATTERN" 2> /dev/null ; then
+    echo >&2 "Implicit aliases are not supported in nvm_remote_versions."
+    return 1
+  fi
+  case "_$PATTERN" in
+    "_$(nvm_iojs_prefix)" | "_io.js")
+      VERSIONS="$(nvm_ls_remote_iojs)"
+    ;;
+    "_$(nvm_node_prefix)")
+      VERSIONS="$(nvm_ls_remote)"
+    ;;
+    *)
+      VERSIONS="$(echo "$(nvm_ls_remote "$PATTERN")
+$(nvm_ls_remote_iojs "$PATTERN")" | command grep -v "N/A" | command sed '/^$/d')"
+    ;;
+  esac
+
+  if [ -z "$VERSIONS" ]; then
+    echo "N/A"
+    return 3
+  else
+    echo "$VERSIONS"
+  fi
+}
+
 nvm_normalize_version() {
   echo "$1" | command sed -e 's/^v//' | command awk -F. '{ printf("%d%06d%06d\n", $1,$2,$3); }'
 }
@@ -527,7 +561,7 @@
   local GREP_OPTIONS
   GREP_OPTIONS=''
   if nvm_validate_implicit_alias "$PATTERN" 2> /dev/null ; then
-    PATTERN="$(nvm_remote_version "$(nvm_print_implicit_alias remote "$PATTERN")")"
+    PATTERN="$(nvm_ls_remote "$(nvm_print_implicit_alias remote "$PATTERN")" | tail -n1)"
   elif [ -n "$PATTERN" ]; then
     PATTERN="$(nvm_ensure_version_prefix "$PATTERN")"
   else
@@ -866,9 +900,6 @@
     return
   fi
 
-  # Try to figure out the os and arch for binary fetching
-  local os
-  local arch
   local GREP_OPTIONS
   GREP_OPTIONS=''
 
@@ -954,17 +985,12 @@
         shift
       fi
 
-      case "_$provided_version" in
-        "_$(nvm_iojs_prefix)" | "_io.js")
-          VERSION="$(nvm_add_iojs_prefix $(nvm_ls_remote_iojs | tail -n1))"
-        ;;
-        "_$(nvm_node_prefix)")
-          VERSION="$(nvm_ls_remote stable)"
-        ;;
-        *)
-          VERSION="$(nvm_remote_version "$provided_version")"
-        ;;
-      esac
+      VERSION="$(nvm_remote_version "$provided_version")"
+
+      if [ "_$VERSION" = "_N/A" ]; then
+        echo "Version '$provided_version' not found - try \`nvm ls-remote\` to browse available versions." >&2
+        return 3
+      fi
 
       ADDITIONAL_PARAMETERS=''
       local PROVIDED_REINSTALL_PACKAGES_FROM
@@ -1007,11 +1033,6 @@
         return $?
       fi
 
-      if [ "_$VERSION" = "_N/A" ]; then
-        echo "Version '$provided_version' not found - try \`nvm ls-remote\` to browse available versions." >&2
-        return 3
-      fi
-
       if [ "_$NVM_OS" = "_freebsd" ]; then
         # node.js and io.js do not have a FreeBSD binary
         nobinary=1
@@ -1204,7 +1225,11 @@
       if [ "$NVM_SYMLINK_CURRENT" = true ]; then
         command rm -f "$NVM_DIR/current" && ln -s "$NVM_VERSION_DIR" "$NVM_DIR/current"
       fi
-      echo "Now using node $VERSION"
+      if nvm_is_iojs_version "$VERSION"; then
+        echo "Now using io.js $(nvm_strip_iojs_prefix "$VERSION")"
+      else
+        echo "Now using node $VERSION"
+      fi
     ;;
     "run" )
       local provided_version
@@ -1327,7 +1352,7 @@
 
       local NVM_OUTPUT
       NVM_OUTPUT="$(echo "$NVM_LS_REMOTE_OUTPUT
-$NVM_LS_REMOTE_IOJS_OUTPUT" | grep -v "N/A" | sed '/^$/d')"
+$NVM_LS_REMOTE_IOJS_OUTPUT" | command grep -v "N/A" | sed '/^$/d')"
       if [ -n "$NVM_OUTPUT" ]; then
         nvm_print_versions "$NVM_OUTPUT"
         return $NVM_LS_REMOTE_EXIT_CODE || $NVM_LS_REMOTE_IOJS_EXIT_CODE
@@ -1484,7 +1509,7 @@
         nvm_iojs_prefix nvm_node_prefix \
         nvm_add_iojs_prefix nvm_strip_iojs_prefix \
         nvm_is_iojs_version \
-        nvm_ls_remote nvm_ls nvm_remote_version \
+        nvm_ls_remote nvm_ls nvm_remote_version nvm_remote_versions \
         nvm_version nvm_rc_version \
         nvm_version_greater nvm_version_greater_than_or_equal_to \
         nvm_supports_source_options > /dev/null 2>&1
diff --git "a/test/fast/Running \"nvm use iojs\" uses latest io.js version" "b/test/fast/Running \"nvm use iojs\" uses latest io.js version"
new file mode 100755
index 0000000..53f67fa
--- /dev/null
+++ "b/test/fast/Running \"nvm use iojs\" uses latest io.js version"
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+die () { echo $@ ; cleanup ; exit 1; }
+
+cleanup() {
+  rm -rf ../../alias/foo
+}
+
+. ../../nvm.sh
+
+echo 'foo' > ../../alias/foo
+
+OUTPUT="$(nvm use foo 2>&1)"
+EXPECTED_OUTPUT='The alias "foo" leads to an infinite loop. Aborting.'
+[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
+  || die "'nvm use foo' did not output '$EXPECTED_OUTPUT'; got '$OUTPUT'"
+
+EXIT_CODE="$(nvm use foo 2>/dev/null ; echo $?)"
+[ "_$EXIT_CODE" = "_8" ] || die "Expected exit code 8; got $EXIT_CODE"
+
+cleanup;
+
diff --git a/test/fast/Unit tests/nvm_remote_versions b/test/fast/Unit tests/nvm_remote_versions
new file mode 100755
index 0000000..bbbaa24
--- /dev/null
+++ b/test/fast/Unit tests/nvm_remote_versions
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+die () { echo $@ ; cleanup ; exit 1; }
+
+cleanup() {
+  unset -f nvm_ls_remote nvm_ls_remote_iojs
+}
+
+. ../../../nvm.sh
+
+OUTPUT="$(nvm_remote_versions stable 2>&1)"
+EXPECTED_OUTPUT="Implicit aliases are not supported in nvm_remote_versions."
+EXIT_CODE="$(nvm_remote_versions stable >/dev/null 2>&1; echo $?)"
+[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "implicit alias 'stable' did not error out with correct message, got $OUTPUT"
+[ "_$EXIT_CODE" = "_1" ] || die "implicit alias 'stable' did not exit with code 1, got $EXIT_CODE"
+
+OUTPUT="$(nvm_remote_versions unstable 2>&1)"
+EXPECTED_OUTPUT="Implicit aliases are not supported in nvm_remote_versions."
+EXIT_CODE="$(nvm_remote_versions unstable >/dev/null 2>&1; echo $?)"
+[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "implicit alias 'unstable' did not error out with correct message, got $OUTPUT"
+[ "_$EXIT_CODE" = "_1" ] || die "implicit alias 'unstable' did not exit with code 1, got $EXIT_CODE"
+
+nvm_ls_remote() {
+  echo "N/A"
+}
+OUTPUT="$(nvm_remote_versions foo)"
+EXIT_CODE="$(nvm_remote_versions foo >/dev/null 2>&1 ; echo $?)"
+[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A"
+[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE"
+
+nvm_ls_remote_iojs() {
+  echo "N/A"
+}
+OUTPUT="$(nvm_remote_versions iojs-foo)"
+EXIT_CODE="$(nvm_remote_versions iojs-foo >/dev/null 2>&1 ; echo $?)"
+[ "_$OUTPUT" = "_N/A" ] || die "nonexistent version did not report N/A"
+[ "_$EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $EXIT_CODE"
+
+
+nvm_ls_remote() {
+  echo "test output"
+  echo "more test output"
+  echo "pattern received: _$1_"
+}
+nvm_ls_remote_iojs() {
+  echo "test iojs output"
+  echo "more iojs test output"
+  echo "iojs pattern received: _$1_"
+}
+
+OUTPUT="$(nvm_remote_versions foo)"
+EXIT_CODE="$(nvm_remote_versions foo >/dev/null 2>&1 ; echo $?)"
+[ "_$OUTPUT" = "_$(nvm_ls_remote foo)
+$(nvm_ls_remote_iojs foo)" ] \
+  || die "nvm_remote_versions foo did not return contents of nvm_ls_remote foo combined with nvm_ls_remote_iojs foo; got $OUTPUT"
+[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_versions foo did not exit with 0, got $EXIT_CODE"
+
+OUTPUT="$(nvm_remote_versions node)"
+EXIT_CODE="$(nvm_remote_versions node >/dev/null 2>&1 ; echo $?)"
+[ "_$OUTPUT" = "_$(nvm_ls_remote)" ] \
+  || die "nvm_remote_versions node did not return contents of nvm_ls_remote; got $OUTPUT"
+[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_versions node did not exit with 0, got $EXIT_CODE"
+
+OUTPUT="$(nvm_remote_versions iojs-foo)"
+EXIT_CODE="$(nvm_remote_versions iojs-foo >/dev/null 2>&1 ; echo $?)"
+[ "_$OUTPUT" = "_$(nvm_ls_remote iojs-foo)
+$(nvm_ls_remote_iojs iojs-foo)" ] \
+  || die "nvm_remote_versions iojs-foo did not return contents of nvm_ls_remote iojs-foo combined with nvm_ls_remote_iojs iojs-foo; got $OUTPUT"
+[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_versions iojs-foo did not exit with 0, got $EXIT_CODE"
+
+OUTPUT="$(nvm_remote_versions iojs)"
+EXIT_CODE="$(nvm_remote_versions iojs >/dev/null 2>&1 ; echo $?)"
+[ "_$OUTPUT" = "_$(nvm_ls_remote_iojs)" ] \
+  || die "nvm_remote_versions iojs did not return contents of nvm_ls_remote_iojs; got $OUTPUT"
+[ "_$EXIT_CODE" = "_0" ] || die "nvm_remote_versions iojs did not exit with 0, got $EXIT_CODE"
+
+cleanup
+
diff --git a/test/installation/io.js/nvm install v1 works b/test/installation/io.js/nvm install v1 works
new file mode 100755
index 0000000..b54d1f0
--- /dev/null
+++ b/test/installation/io.js/nvm install v1 works
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+die () { echo $@ ; exit 1; }
+
+. ../../../nvm.sh
+
+NVM_VERSION="v1"
+NVM_PREFIXED_TEST_VERSION="$(nvm ls-remote "$NVM_VERSION" | tail -n1 | sed 's/^[    ]*//;s/[        ]*$//')"
+NVM_TEST_VERSION="$(nvm_strip_iojs_prefix "$NVM_PREFIXED_TEST_VERSION")"
+
+# Remove the stuff we're clobbering.
+[ -e ../../../$NVM_TEST_VERSION ] && rm -R ../../../$NVM_TEST_VERSION
+
+# Install from binary
+nvm install "$NVM_VERSION" || die "nvm install $NVM_VERSION failed"
+
+# Check
+[ -d ../../../versions/io.js/$NVM_TEST_VERSION ]
+nvm run $NVM_PREFIXED_TEST_VERSION --version | grep $NVM_TEST_VERSION || die "'nvm run $NVM_PREFIXED_TEST_VERSION --version | grep $NVM_TEST_VERSION' failed"
+