diff --git a/.dockerignore b/.dockerignore index fc578ccc81b..52e54284986 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,6 +8,7 @@ test/bak .urchin.log .urchin_stdout test/**/test_output +test/**/.nvmrc node_modules/ npm-debug.log diff --git a/.gitignore b/.gitignore index fcf59f80393..ec161d456d8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ test/bak .urchin.log .urchin_stdout test/**/test_output +test/**/.nvmrc node_modules/ npm-debug.log diff --git a/nvm.sh b/nvm.sh old mode 100644 new mode 100755 index d6af5332c43..aff63bc666a --- a/nvm.sh +++ b/nvm.sh @@ -2844,6 +2844,7 @@ nvm() { nvm_echo ' --no-progress Disable the progress bar on any downloads' nvm_echo ' --alias= After installing, set the alias specified to the version specified. (same as: nvm alias )' nvm_echo ' --default After installing, set default alias to the version specified. (same as: nvm alias default )' + nvm_echo ' --save After installing, write the specified version to .nvmrc' nvm_echo ' nvm uninstall Uninstall a version' nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.' nvm_echo ' nvm uninstall --lts= Uninstall using automatic alias for provided LTS line, if available.' @@ -2852,6 +2853,7 @@ nvm() { nvm_echo ' --silent Silences stdout/stderr output' nvm_echo ' --lts Uses automatic LTS (long-term support) alias `lts/*`, if available.' nvm_echo ' --lts= Uses automatic alias for provided LTS line, if available.' + nvm_echo ' --save Writes the specified version to .nvmrc.' nvm_echo ' nvm exec [] [] Run on . Uses .nvmrc if available and version is omitted.' nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm exec`:' nvm_echo ' --silent Silences stdout/stderr output' @@ -3059,6 +3061,8 @@ nvm() { local ALIAS local NVM_UPGRADE_NPM NVM_UPGRADE_NPM=0 + local NVM_WRITE_TO_NVMRC + NVM_WRITE_TO_NVMRC=0 local PROVIDED_REINSTALL_PACKAGES_FROM local REINSTALL_PACKAGES_FROM @@ -3158,6 +3162,10 @@ nvm() { SKIP_DEFAULT_PACKAGES=true shift ;; + --save) + NVM_WRITE_TO_NVMRC=1 + shift + ;; *) break # stop parsing args ;; @@ -3533,6 +3541,8 @@ nvm() { local NVM_LTS local IS_VERSION_FROM_NVMRC IS_VERSION_FROM_NVMRC=0 + local NVM_WRITE_TO_NVMRC + NVM_WRITE_TO_NVMRC=0 while [ $# -ne 0 ]; do case "$1" in @@ -3544,6 +3554,7 @@ nvm() { --) ;; --lts) NVM_LTS='*' ;; --lts=*) NVM_LTS="${1##--lts=}" ;; + --save) NVM_WRITE_TO_NVMRC=1 ;; --*) ;; *) if [ -n "${1-}" ]; then @@ -3576,16 +3587,24 @@ nvm() { >&2 nvm --help return 127 fi - + local NVMRC_SAVE_TEXT + NVMRC_SAVE_TEXT='' + if [ $NVM_WRITE_TO_NVMRC -eq 1 ] && [ "${VERSION}" != '∞' ] && [ "${VERSION}" != 'N/A' ]; then + echo "${VERSION}" | tee "${PWD}"/.nvmrc > /dev/null || { + nvm_err "Warning: Unable to write version \`${VERSION}\` to .nvmrc" + exit 3 + } + NVMRC_SAVE_TEXT=' (saved to .nvmrc)' + fi if [ "_${VERSION}" = '_system' ]; then if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then if [ "${NVM_SILENT:-0}" -ne 1 ]; then - nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)" + nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}" fi return elif nvm_has_system_iojs && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then if [ "${NVM_SILENT:-0}" -ne 1 ]; then - nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)" + nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}" fi return elif [ "${NVM_SILENT:-0}" -ne 1 ]; then @@ -3634,9 +3653,9 @@ nvm() { NVM_USE_OUTPUT='' if [ "${NVM_SILENT:-0}" -ne 1 ]; then if nvm_is_iojs_version "${VERSION}"; then - NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)" + NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}" else - NVM_USE_OUTPUT="Now using node ${VERSION}$(nvm_print_npm_version)" + NVM_USE_OUTPUT="Now using node ${VERSION}$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}" fi fi if [ "_${VERSION}" != "_system" ]; then diff --git a/test/fast/Unit tests/Running 'nvm install --save --lts' with incorrect file permissions fails nicely b/test/fast/Unit tests/Running 'nvm install --save --lts' with incorrect file permissions fails nicely new file mode 100755 index 00000000000..1dec35897e0 --- /dev/null +++ b/test/fast/Unit tests/Running 'nvm install --save --lts' with incorrect file permissions fails nicely @@ -0,0 +1,32 @@ +#!/bin/sh + +set -e + +die () { + unset -f nvm_ls_remote nvm_ls_remote_iojs + >&2 echo "$@" + exit 1 +} + +\. ../../../nvm.sh +\. ../../common.sh + +rm -rf .nvmrc +echo '' > .nvmrc +chmod 0 .nvmrc + +REMOTE="$PWD/mocks/nvm_ls_remote.txt" +nvm_ls_remote() { + cat "$REMOTE" +} +REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt" +nvm_ls_remote_iojs() { + cat "$REMOTE_IOJS" +} + +make_fake_node lts + +{ + (nvm install --save --lts) && + die "\`nvm install --save --lts\` did not fail with invalid permissions" +} || echo "\`nvm install --save --lts\` failed successfully" diff --git a/test/fast/Unit tests/Running 'nvm install --save x' works as expected b/test/fast/Unit tests/Running 'nvm install --save x' works as expected new file mode 100755 index 00000000000..db1a837a0cd --- /dev/null +++ b/test/fast/Unit tests/Running 'nvm install --save x' works as expected @@ -0,0 +1,41 @@ +#!/bin/sh + +set -e + +die () { + unset -f nvm_ls_remote nvm_ls_remote_iojs + >&2 echo "$@" + exit 1 +} + +\. ../../../nvm.sh +\. ../../common.sh + +REMOTE="$PWD/mocks/nvm_ls_remote.txt" +nvm_ls_remote() { + cat "$REMOTE" +} +REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt" +nvm_ls_remote_iojs() { + cat "$REMOTE_IOJS" +} + +test_version () { + rm -f .nvmrc + VERSION=${1-} + + make_fake_node "${VERSION}" + + nvm install --save "${VERSION}" || die "\`nvm install --save ${VERSION}\` failed" + OUTPUT="$(cat .nvmrc)" + EXPECTED_OUTPUT="$(nvm_ls_current)" + + [ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \ + || die "\`nvm use use --save ${VERSION}\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'" +} + +test_version '--lts' || die +test_version 'lts/argon' || die +test_version 'lts/*' || die +test_version 'node' || die +test_version 'iojs' || die diff --git a/test/fast/Unit tests/Running 'nvm install --save' should work with a '.nvmrc' file in the parent directory b/test/fast/Unit tests/Running 'nvm install --save' should work with a '.nvmrc' file in the parent directory new file mode 100755 index 00000000000..94c14839382 --- /dev/null +++ b/test/fast/Unit tests/Running 'nvm install --save' should work with a '.nvmrc' file in the parent directory @@ -0,0 +1,35 @@ +#!/bin/sh + +set -e + +die () { + unset -f nvm_ls_remote nvm_ls_remote_iojs + >&2 echo "$@" + exit 1 +} + +\. ../../../nvm.sh + +REMOTE="$PWD/mocks/nvm_ls_remote.txt" +nvm_ls_remote() { + cat "$REMOTE" +} +REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt" +nvm_ls_remote_iojs() { + cat "$REMOTE_IOJS" +} + +make_fake_node lts + +(cd .. +rm -rf .nvmrc +nvm install --save --lts) + +nvm install --save +OUTPUT="$(cat .nvmrc)" +EXPECTED_OUTPUT="$(nvm_ls_current)" + +[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \ + || die "\`nvm use use --save\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'" + +rm -rf ../.nvmrc .nvmrc diff --git a/test/fast/Unit tests/Running 'nvm use --save test' where 'test' is an alias should work as expected b/test/fast/Unit tests/Running 'nvm use --save test' where 'test' is an alias should work as expected new file mode 100755 index 00000000000..b883287e949 --- /dev/null +++ b/test/fast/Unit tests/Running 'nvm use --save test' where 'test' is an alias should work as expected @@ -0,0 +1,19 @@ +#!/bin/sh + +set -e + +die () { echo "$@" ; exit 1; } + +\. ../../../nvm.sh + +rm -rf .nvmrc; + +nvm alias test node +nvm use test --save || die "\`nvm use test --save\` failed" +OUTPUT="$(cat .nvmrc)" +EXPECTED_OUTPUT=$(nvm_ls_current) + +[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \ + || die "\`nvm use --save test\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'" + +nvm unalias test diff --git a/test/fast/Unit tests/Running 'nvm use --silent --save node' should be silenced b/test/fast/Unit tests/Running 'nvm use --silent --save node' should be silenced new file mode 100755 index 00000000000..d8de815d365 --- /dev/null +++ b/test/fast/Unit tests/Running 'nvm use --silent --save node' should be silenced @@ -0,0 +1,14 @@ +#!/bin/sh + +set -e + +die () { echo "$@" ; exit 1; } + +\. ../../../nvm.sh + +rm -rf .nvmrc; +OUTPUT="$(nvm use --silent --save node || die "\`nvm use --silent --save node\` failed")" +EXPECTED_OUTPUT='' + +[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \ + || die "\`nvm use --silent --save node\` was not silenced; got '${OUTPUT}'" diff --git a/test/fast/teardown b/test/fast/teardown index 75a406e7eee..c7387cf44bf 100755 --- a/test/fast/teardown +++ b/test/fast/teardown @@ -8,5 +8,5 @@ set -ex type setopt >/dev/null 2>&1 && setopt NULL_GLOB type shopt >/dev/null 2>&1 && shopt -s nullglob - rm -fR v* src alias test/test-xz + rm -fR v* src alias test/test-xz .nvmrc )