diff --git a/nvm.sh b/nvm.sh index f6c4ec4..d1947f3 100644 --- a/nvm.sh +++ b/nvm.sh @@ -1926,6 +1926,9 @@ nvm_install_binary() { return 3 fi + local nosource + nosource="${4-}" + local VERSION VERSION="$(nvm_strip_iojs_prefix "${PREFIXED_VERSION}")" @@ -1965,6 +1968,13 @@ nvm_install_binary() { return 0 fi + + # Read nosource from arguments + if [ "${nosource-}" = '1' ]; then + nvm_err 'Binary download failed. Download from source aborted.' + return 0 + fi + nvm_err 'Binary download failed, trying source.' if [ -n "${TMPDIR-}" ]; then command rm -rf "${TMPDIR}" @@ -2665,6 +2675,7 @@ nvm() { nvm_echo ' nvm install [] Download and install a . Uses .nvmrc if available and version is omitted.' nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm install`:' nvm_echo ' -s Skip binary download, install from source only.' + nvm_echo ' -b Skip source download, install from binary only.' nvm_echo ' --reinstall-packages-from= When installing, reinstall packages installed in ' nvm_echo ' --lts When installing, only select from LTS (long-term support) versions' nvm_echo ' --lts= When installing, only select from versions for a specific LTS line' @@ -2879,9 +2890,11 @@ nvm() { fi local nobinary + local nosource local noprogress nobinary=0 noprogress=0 + nosource=0 local LTS local ALIAS local NVM_UPGRADE_NPM @@ -2901,6 +2914,18 @@ nvm() { -s) shift # consume "-s" nobinary=1 + if [ $nosource -eq 1 ]; then + nvm err '-s and -b cannot be set together since they would skip install from both binary and source' + return 6 + fi + ;; + -b) + shift # consume "-b" + nosource=1 + if [ $nobinary -eq 1 ]; then + nvm err '-s and -b cannot be set together since they would skip install from both binary and source' + return 6 + fi ;; -j) shift # consume "-j" @@ -3165,7 +3190,7 @@ nvm() { # skip binary install if "nobinary" option specified. if [ $nobinary -ne 1 ] && nvm_binary_available "${VERSION}"; then - NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" nvm_install_binary "${FLAVOR}" std "${VERSION}" + NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" nvm_install_binary "${FLAVOR}" std "${VERSION}" "${nosource}" EXIT_CODE=$? fi if [ $EXIT_CODE -ne 0 ]; then diff --git a/test/fast/Unit tests/nvm_install_binary_nosource b/test/fast/Unit tests/nvm_install_binary_nosource new file mode 100755 index 0000000..e425b15 --- /dev/null +++ b/test/fast/Unit tests/nvm_install_binary_nosource @@ -0,0 +1,30 @@ +#!/bin/sh + +cleanup () { + nvm cache clear + nvm deactivate + rm -rf ${NVM_DIR}/v* + nvm unalias default +} + +die () { echo "$@" ; cleanup; exit 1;} + +\. ../../../nvm.sh + +nvm_binary_available() { + return 1 +} + +# Unit test to check if the function errors out when the flag is set +OUTPUT="$(nvm_install_binary node std 8.0.0 1 2>&1)" +EXPECTED_OUTPUT='Binary download failed. Download from source aborted.' +if [ "${OUTPUT#*$EXPECTED_OUTPUT}" = "${OUTPUT}" ]; then + die "No source binary flag is active and should have returned >${EXPECTED_OUTPUT}<. Instead it returned >${OUTPUT}<" +fi + +# Unit test to check if the function errors out when the flag is set +OUTPUT="$(nvm_install_binary node std 8.0.0 0 2>&1)" +EXPECTED_OUTPUT='Binary download failed. Download from source aborted.' +if [ "${OUTPUT#*$EXPECTED_OUTPUT}" != "${OUTPUT}" ]; then + die "No source binary flag is not active and should have downloaded from source. Instead it returned >${OUTPUT}<" +fi diff --git a/test/installation_node/install from binary with binary flag set b/test/installation_node/install from binary with binary flag set new file mode 100644 index 0000000..35e53ab --- /dev/null +++ b/test/installation_node/install from binary with binary flag set @@ -0,0 +1,37 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +\. ../../nvm.sh + +nvm unalias default || die 'unable to unalias default' + +NVM_TEST_VERSION=v0.10.7 + +# Remove the stuff we're clobbering. +[ -e ../../$NVM_TEST_VERSION ] && rm -R ../../$NVM_TEST_VERSION + +# Install from binary +nvm install -b $NVM_TEST_VERSION || die "install $NVM_TEST_VERSION failed" + +# Check +[ -d ../../$NVM_TEST_VERSION ] +nvm run $NVM_TEST_VERSION --version | grep $NVM_TEST_VERSION || die "'nvm run $NVM_TEST_VERSION --version | grep $NVM_TEST_VERSION' failed" + +# ensure default is set +NVM_CURRENT_DEFAULT="$(nvm_alias default)" +[ "$NVM_CURRENT_DEFAULT" = "$NVM_TEST_VERSION" ] || die "wrong default alias: $(nvm alias)" + +# Falls back to source but if -b is set fails binary download. +OUTPUT="$(nvm install -b 9.0.0 2>&1)" +EXPECTED_OUTPUT='Binary download failed. Download from source aborted.' +if [ "${OUTPUT#*$EXPECTED_OUTPUT}" = "${OUTPUT}" ]; then + die "No source binary flag is active and should have returned >${EXPECTED_OUTPUT}<. Instead it returned >${OUTPUT}<" +fi + +# Falls back to source but if -b is not set. +OUTPUT="$(nvm install 9.0.0 2>&1)" +EXPECTED_OUTPUT='Binary download failed. Download from source aborted.' +if [ "${OUTPUT#*$EXPECTED_OUTPUT}" != "${OUTPUT}" ]; then + die "No source binary flag is active and should have returned >${EXPECTED_OUTPUT}<. Instead it returned >${OUTPUT}<" +fi