Merge pull request #477 from koenpunt/without-curl

add test without curl
diff --git a/README.markdown b/README.markdown
index f0a2610..d6cc43b 100644
--- a/README.markdown
+++ b/README.markdown
@@ -8,11 +8,11 @@
 
 To install you could use the [install script][2] using cURL:
 
-    curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
+    curl https://raw.githubusercontent.com/creationix/nvm/v0.13.0/install.sh | bash
 
 or Wget:
 
-    wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
+    wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.13.0/install.sh | bash
 
 <sub>The script clones the nvm repository to `~/.nvm` and adds the source line to your profile (`~/.bash_profile`, `~/.zshrc` or `~/.profile`).</sub>
 
@@ -38,7 +38,7 @@
 ## Usage
 
 You can create an `.nvmrc` file containing version number in the project root directory (or any parent directory).
-`nvm use`, `nvm install`, and `nvm run` will all respect an `.nvmrc` file.
+`nvm use`, `nvm install`, `nvm exec`, and `nvm run` will all respect an `.nvmrc` file.
 
 To download, compile, and install the latest v0.10.x release of node, do this:
 
@@ -52,6 +52,10 @@
 
     nvm run 0.10 --version
 
+Or, you can run any arbitrary command in a subshell with the desired version of node:
+
+    nvm exec 0.10 node --version
+
 If you want to see what versions are installed:
 
     nvm ls
@@ -163,7 +167,7 @@
     nvm install -s 0.8.6
 
 [1]: https://github.com/creationix/nvm.git
-[2]: https://github.com/creationix/nvm/blob/v0.11.1/install.sh
+[2]: https://github.com/creationix/nvm/blob/v0.13.0/install.sh
 [3]: https://travis-ci.org/creationix/nvm
 [Urchin]: https://github.com/scraperwiki/urchin
 
diff --git a/install.sh b/install.sh
index d87137f..74bc7f5 100755
--- a/install.sh
+++ b/install.sh
@@ -44,12 +44,12 @@
     mkdir -p "$NVM_DIR"
     git clone "$NVM_SOURCE" "$NVM_DIR"
   fi
-  cd $NVM_DIR && git checkout v0.11.1 && git branch -D master
+  cd $NVM_DIR && git checkout v0.13.0 && git branch -D master || true
 }
 
 install_nvm_as_script() {
   if [ -z "$NVM_SOURCE" ]; then
-    NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/v0.11.1/nvm.sh"
+    NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/v0.13.0/nvm.sh"
   fi
 
   # Downloading to $NVM_DIR
diff --git a/nvm.sh b/nvm.sh
index 1197059..52bea15 100644
--- a/nvm.sh
+++ b/nvm.sh
@@ -53,6 +53,19 @@
   export NVM_NODEJS_ORG_MIRROR="http://nodejs.org/dist"
 fi
 
+nvm_tree_contains_path() {
+  local tree
+  tree="$1"
+  local node_path
+  node_path="$2"
+  local pathdir
+  pathdir=$(dirname "$node_path")
+  while [ "$pathdir" != "" ] && [ "$pathdir" != "." ] && [ "$pathdir" != "/" ] && [ "$pathdir" != "$tree" ]; do
+    pathdir=$(dirname "$pathdir")
+  done
+  [ "$pathdir" = "$tree" ]
+}
+
 # Traverse up in directory tree to find containing folder
 nvm_find_up() {
   local path
@@ -92,6 +105,11 @@
     PATTERN='current'
   fi
 
+  if [ "$PATTERN" = "current" ]; then
+    nvm_ls_current
+    return $?
+  fi
+
   VERSION=`nvm_ls $PATTERN | tail -n1`
   echo "$VERSION"
 
@@ -113,7 +131,7 @@
 }
 
 nvm_normalize_version() {
-  echo "$1" | sed -e 's/^v//' | awk -F. '{ printf("%d%03d%03d\n", $1,$2,$3); }'
+  echo "$1" | sed -e 's/^v//' | \awk -F. '{ printf("%d%03d%03d\n", $1,$2,$3); }'
 }
 
 nvm_format_version() {
@@ -142,7 +160,15 @@
 }
 
 nvm_ls_current() {
-  echo `node -v 2>/dev/null`
+  local NODE_PATH
+  NODE_PATH="$(which node)"
+  if [ $? -ne 0 ]; then
+    echo 'none'
+  elif nvm_tree_contains_path "$NVM_DIR" "$NODE_PATH"; then
+    echo `node -v 2>/dev/null`
+  else
+    echo 'system'
+  fi
 }
 
 nvm_ls() {
@@ -170,12 +196,15 @@
       PATTERN="$PATTERN."
     fi
     VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \
-      | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.'`
+      | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'`
   fi
   if [ -z "$VERSIONS" ]; then
     echo "N/A"
     return 3
   fi
+  if [ -z "$PATTERN" ] && nvm_has_system_node; then
+    VERSIONS="$VERSIONS$(printf '\n%s' 'system')"
+  fi
   echo "$VERSIONS"
   return
 }
@@ -205,11 +234,11 @@
 
 nvm_checksum() {
   if nvm_has "shasum"; then
-    checksum=$(shasum $1 | awk '{print $1}')
+    checksum=$(shasum $1 | \awk '{print $1}')
   elif nvm_has "sha1"; then
     checksum=$(sha1 -q $1)
   else
-    checksum=$(sha1sum $1 | awk '{print $1}')
+    checksum=$(sha1sum $1 | \awk '{print $1}')
   fi
 
   if [ "$checksum" = "$2" ]; then
@@ -233,6 +262,8 @@
       FORMAT='\033[0;32m-> %9s\033[0m'
     elif [ -d "$NVM_DIR/$VERSION" ]; then
       FORMAT='\033[0;34m%12s\033[0m'
+    elif [ "$VERSION" = "system" ]; then
+      FORMAT='\033[0;33m%12s\033[0m'
     else
       FORMAT='%12s'
     fi
@@ -317,7 +348,7 @@
       version_not_provided=0
       local provided_version
 
-      if ! nvm_has "nvm_download"; then
+      if ! nvm_has "curl" && ! nvm_has "wget"; then
         echo 'nvm needs curl or wget to proceed.' >&2;
         return 1
       fi
@@ -381,7 +412,7 @@
           if nvm_binary_available "$VERSION"; then
             t="$VERSION-$os-$arch"
             url="$NVM_NODEJS_ORG_MIRROR/$VERSION/node-${t}.tar.gz"
-            sum=`nvm_download -s $NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt -o - | \grep node-${t}.tar.gz | awk '{print $1}'`
+            sum=`nvm_download -s $NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt -o - | \grep node-${t}.tar.gz | \awk '{print $1}'`
             local tmpdir
             tmpdir="$NVM_DIR/bin/node-${t}"
             local tmptarball
@@ -420,7 +451,7 @@
       tmptarball="$tmpdir/node-$VERSION.tar.gz"
       if [ "`nvm_download -s -I "$NVM_NODEJS_ORG_MIRROR/$VERSION/node-$VERSION.tar.gz" -o - | \grep '200 OK'`" != '' ]; then
         tarball="$NVM_NODEJS_ORG_MIRROR/$VERSION/node-$VERSION.tar.gz"
-        sum=`nvm_download -s $NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt -o - | \grep node-$VERSION.tar.gz | awk '{print $1}'`
+        sum=`nvm_download -s $NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt -o - | \grep node-$VERSION.tar.gz | \awk '{print $1}'`
       elif [ "`nvm_download -s -I "$NVM_NODEJS_ORG_MIRROR/node-$VERSION.tar.gz" -o - | \grep '200 OK'`" != '' ]; then
         tarball="$NVM_NODEJS_ORG_MIRROR/node-$VERSION.tar.gz"
       fi
@@ -519,7 +550,17 @@
           VERSION=`nvm_version $NVM_RC_VERSION`
         fi
       else
-        VERSION=`nvm_version $2`
+        if [ $2 = 'system' ]; then
+          if nvm_has_system_node && nvm deactivate; then
+            echo "Now using system version of node: $(node -v 2>/dev/null)."
+            return
+          else
+            echo "System version of node not found." >&2
+            return 127
+          fi
+        else
+          VERSION=`nvm_version $2`
+        fi
       fi
       if [ -z "$VERSION" ]; then
         nvm help
@@ -598,6 +639,29 @@
       echo "Running node $VERSION"
       NODE_PATH=$RUN_NODE_PATH $NVM_DIR/$VERSION/bin/node "$@"
     ;;
+    "exec" )
+      shift
+
+      local provided_version
+      provided_version=$1
+      if [ -n "$provided_version" ]; then
+        VERSION=`nvm_version $provided_version`
+        if [ $VERSION = "N/A" ]; then
+          provided_version=''
+          nvm_rc_version
+          VERSION=`nvm_version $NVM_RC_VERSION`
+        else
+          shift
+        fi
+      fi
+
+      if [ ! -d "$NVM_DIR/$VERSION" ]; then
+        echo "$VERSION version is not installed yet" >&2
+        return 1
+      fi
+      echo "Running node $VERSION"
+      NODE_VERSION=$VERSION $NVM_DIR/nvm-exec "$@"
+    ;;
     "ls" | "list" )
       local NVM_LS_OUTPUT
       local NVM_LS_EXIT_CODE
@@ -682,7 +746,7 @@
       nvm_version $2
     ;;
     "--version" )
-      echo "0.11.1"
+      echo "0.13.0"
     ;;
     "unload" )
       unset -f nvm nvm_print_versions nvm_checksum nvm_ls_remote nvm_ls nvm_remote_version nvm_version nvm_rc_version > /dev/null 2>&1
diff --git a/package.json b/package.json
index 262c952..01f680a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "nvm",
-  "version": "0.11.1",
+  "version": "0.13.0",
   "description": "Node Version Manager - Simple bash script to manage multiple active node.js versions",
   "directories": {
     "test": "test"
diff --git "a/test/fast/Listing versions/Running \"nvm ls node\" should return a nonzero exit code when not found" "b/test/fast/Listing versions/Running \"nvm ls node\" should return a nonzero exit code when not found"
new file mode 100755
index 0000000..f7d68f7
--- /dev/null
+++ "b/test/fast/Listing versions/Running \"nvm ls node\" should return a nonzero exit code when not found"
@@ -0,0 +1,7 @@
+ #!/bin/sh
+
+. ../../../nvm.sh
+
+nvm ls node
+[ "$?" = "3" ]
+
diff --git "a/test/fast/Listing versions/Running \"nvm ls\" should include \"system\" when appropriate" "b/test/fast/Listing versions/Running \"nvm ls\" should include \"system\" when appropriate"
new file mode 100755
index 0000000..9afd40a
--- /dev/null
+++ "b/test/fast/Listing versions/Running \"nvm ls\" should include \"system\" when appropriate"
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+die () { echo $@ ; exit 1; }
+
+. ../../../nvm.sh
+
+mkdir -p ../../../v0.0.1
+mkdir -p ../../../v0.0.3
+mkdir -p ../../../v0.0.9
+mkdir -p ../../../v0.3.1
+mkdir -p ../../../v0.3.3
+mkdir -p ../../../v0.3.9
+
+nvm_has_system_node() { return 0; }
+nvm ls | grep system 2>&1 > /dev/null
+[ $? -eq 0 ] || die '"nvm ls" did not contain "system" when system node is present'
+
+nvm_has_system_node() { return 1; }
+nvm ls | grep system 2>&1 > /dev/null
+[ $? -ne 0 ] || die '"nvm ls" contained "system" when system node is not present'
+
diff --git "a/test/fast/Running \"nvm current\" should display current nvm environment." "b/test/fast/Running \"nvm current\" should display current nvm environment."
index d9293ed..5748fd6 100755
--- "a/test/fast/Running \"nvm current\" should display current nvm environment."
+++ "b/test/fast/Running \"nvm current\" should display current nvm environment."
@@ -3,4 +3,8 @@
 die () { echo $@ ; exit 1; }
 
 . ../../nvm.sh
-[ "$(nvm current)" = "$(node -v)" ] || die "Failed to find current version"
+
+nvm deactivate 2>&1
+
+[ "$(nvm current)" = "system" ] || [ "$(nvm current)" = "none" ] || die '"nvm current" did not report "system" or "none" when deactivated'
+
diff --git "a/test/fast/Running \"nvm use system\" should work as expected" "b/test/fast/Running \"nvm use system\" should work as expected"
new file mode 100755
index 0000000..9b4e850
--- /dev/null
+++ "b/test/fast/Running \"nvm use system\" should work as expected"
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+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)." ] || 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 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_tree_contains_path b/test/fast/Unit tests/nvm_tree_contains_path
new file mode 100755
index 0000000..0ddfb86
--- /dev/null
+++ b/test/fast/Unit tests/nvm_tree_contains_path
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+cleanup () {
+  rm tmp/node
+  rmdir tmp
+  rm tmp2/node
+  rmdir tmp2
+}
+die () { echo $@ ; cleanup; exit 1; }
+
+. ../../../nvm.sh
+
+mkdir -p tmp
+touch -p tmp/node
+mkdir -p tmp2
+touch -p tmp2/node
+
+nvm_tree_contains_path tmp tmp/node || die '"tmp" should contain "tmp/node"'
+
+nvm_tree_contains_path tmp tmp2/node && die '"tmp" should not contain "tmp2/node"'
+
+nvm_tree_contains_path tmp2 tmp2/node || die '"tmp2" should contain "tmp2/node"'
+
+nvm_tree_contains_path tmp2 tmp/node && die '"tmp2" should not contain "tmp/node"'
+
+cleanup
+
diff --git "a/test/slow/Running \"nvm current\" should display current nvm environment." "b/test/slow/Running \"nvm current\" should display current nvm environment."
new file mode 100755
index 0000000..de82ceb
--- /dev/null
+++ "b/test/slow/Running \"nvm current\" should display current nvm environment."
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+die () { echo $@ ; exit 1; }
+
+. ../../nvm.sh
+
+nvm install 0.10
+
+[ "$(nvm current)" = "$(node -v)" ] || die "Failed to find current version: got \"$(nvm current)\", expected \"$(node -v)\""
+
diff --git "a/test/slow/nvm exec/Running \"nvm exec 0.x\" should work" "b/test/slow/nvm exec/Running \"nvm exec 0.x\" should work"
new file mode 100755
index 0000000..7798d79
--- /dev/null
+++ "b/test/slow/nvm exec/Running \"nvm exec 0.x\" should work"
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+die () { echo $@ ; exit 1; }
+
+. ../../../nvm.sh
+
+nvm use 0.10
+NPM_VERSION_TEN="$(npm --version)"
+
+nvm use 0.11.7 && [ "$(node --version)" = "v0.11.7" ] || die "\`nvm use\` failed!"
+
+[ "$(nvm exec 0.10 npm --version | tail -1)" = "$NPM_VERSION_TEN" ] || die "`nvm exec` failed to run with the correct version"
+
diff --git "a/test/slow/nvm exec/Running \"nvm exec\" should pick up .nvmrc version" "b/test/slow/nvm exec/Running \"nvm exec\" should pick up .nvmrc version"
new file mode 100755
index 0000000..203cd9a
--- /dev/null
+++ "b/test/slow/nvm exec/Running \"nvm exec\" should pick up .nvmrc version"
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+die () { echo $@ ; exit 1; }
+
+. ../../../nvm.sh
+
+nvm use 0.10.7
+NPM_VERSION_TEN="$(npm --version)"
+
+nvm use 0.11.7 && [ "$(node --version)" = "v0.11.7" ] || die "\`nvm use\` failed!"
+
+echo "0.10.7" > .nvmrc
+
+[ "$(nvm exec npm --version | tail -1)" = "$NPM_VERSION_TEN" ] || die "\`nvm exec\` failed to run with the .nvmrc version"
+
+[ "$(nvm exec npm --version | head -1)" = "Found '$PWD/.nvmrc' with version <0.10.7>" ] || die "\`nvm exec\` failed to print out the \"found in .nvmrc\" message"
+
diff --git a/test/slow/nvm exec/setup_dir b/test/slow/nvm exec/setup_dir
new file mode 100755
index 0000000..a5fc4bc
--- /dev/null
+++ b/test/slow/nvm exec/setup_dir
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+. ../../../nvm.sh
+nvm install 0.10.7
+nvm install 0.11.7
+
+if [ -f ".nvmrc" ]; then
+  mv .nvmrc .nvmrc.bak
+fi
+
diff --git a/test/slow/nvm exec/teardown_dir b/test/slow/nvm exec/teardown_dir
new file mode 100755
index 0000000..a6b923c
--- /dev/null
+++ b/test/slow/nvm exec/teardown_dir
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+. ../../../nvm.sh
+nvm uninstall v0.10.7
+nvm uninstall v0.11.7
+
+rm .nvmrc
+
+if [ -f ".nvmrc.bak" ]; then
+  mv .nvmrc.bak .nvmrc
+fi
+