mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-04-03 11:34:50 +08:00
[Fix] nvm_download_artifact: fix error propagation from subshells
`return` inside `(...)` subshells only exits the subshell, not the calling function.
Errors in mkdir, download, and checksum verification were silently ignored.
Use `{ ...; }` brace groups instead.
Bug introduced in ba3ad8e460.
This commit is contained in:
12
nvm.sh
12
nvm.sh
@@ -2526,10 +2526,10 @@ nvm_download_artifact() {
|
|||||||
local CHECKSUM
|
local CHECKSUM
|
||||||
CHECKSUM="$(nvm_get_checksum "${FLAVOR}" "${TYPE}" "${VERSION}" "${SLUG}" "${COMPRESSION}")"
|
CHECKSUM="$(nvm_get_checksum "${FLAVOR}" "${TYPE}" "${VERSION}" "${SLUG}" "${COMPRESSION}")"
|
||||||
|
|
||||||
command mkdir -p "${tmpdir}/files" || (
|
command mkdir -p "${tmpdir}/files" || {
|
||||||
nvm_err "creating directory ${tmpdir}/files failed"
|
nvm_err "creating directory ${tmpdir}/files failed"
|
||||||
return 3
|
return 3
|
||||||
)
|
}
|
||||||
|
|
||||||
local TARBALL_URL
|
local TARBALL_URL
|
||||||
if nvm_version_greater_than_or_equal_to "${VERSION}" 0.1.14; then
|
if nvm_version_greater_than_or_equal_to "${VERSION}" 0.1.14; then
|
||||||
@@ -2552,11 +2552,11 @@ nvm_download_artifact() {
|
|||||||
command rm -rf "${TARBALL}"
|
command rm -rf "${TARBALL}"
|
||||||
fi
|
fi
|
||||||
nvm_err "Downloading ${TARBALL_URL}..."
|
nvm_err "Downloading ${TARBALL_URL}..."
|
||||||
nvm_download -L -C - "${PROGRESS_BAR}" "${TARBALL_URL}" -o "${TARBALL}" || (
|
nvm_download -L -C - "${PROGRESS_BAR}" "${TARBALL_URL}" -o "${TARBALL}" || {
|
||||||
command rm -rf "${TARBALL}" "${tmpdir}"
|
command rm -rf "${TARBALL}" "${tmpdir}"
|
||||||
nvm_err "download from ${TARBALL_URL} failed"
|
nvm_err "download from ${TARBALL_URL} failed"
|
||||||
return 4
|
return 4
|
||||||
)
|
}
|
||||||
|
|
||||||
if nvm_grep '404 Not Found' "${TARBALL}" >/dev/null; then
|
if nvm_grep '404 Not Found' "${TARBALL}" >/dev/null; then
|
||||||
command rm -rf "${TARBALL}" "${tmpdir}"
|
command rm -rf "${TARBALL}" "${tmpdir}"
|
||||||
@@ -2564,10 +2564,10 @@ nvm_download_artifact() {
|
|||||||
return 5
|
return 5
|
||||||
fi
|
fi
|
||||||
|
|
||||||
nvm_compare_checksum "${TARBALL}" "${CHECKSUM}" || (
|
nvm_compare_checksum "${TARBALL}" "${CHECKSUM}" || {
|
||||||
command rm -rf "${tmpdir}/files"
|
command rm -rf "${tmpdir}/files"
|
||||||
return 6
|
return 6
|
||||||
)
|
}
|
||||||
|
|
||||||
nvm_echo "${TARBALL}"
|
nvm_echo "${TARBALL}"
|
||||||
}
|
}
|
||||||
|
|||||||
55
test/fast/Unit tests/nvm_download_artifact error propagation
Executable file
55
test/fast/Unit tests/nvm_download_artifact error propagation
Executable file
@@ -0,0 +1,55 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
die () { echo "$@" ; exit 1; }
|
||||||
|
|
||||||
|
: nvm.sh
|
||||||
|
\. ../../../nvm.sh
|
||||||
|
|
||||||
|
\. ../../common.sh
|
||||||
|
|
||||||
|
# Test that mkdir failure propagates from brace group (not subshell)
|
||||||
|
nvm_download_artifact_mkdir_test() {
|
||||||
|
local tmpdir
|
||||||
|
tmpdir="/nonexistent/path/that/cannot/exist"
|
||||||
|
command mkdir -p "${tmpdir}/files" || {
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nvm_download_artifact_mkdir_test 2>/dev/null
|
||||||
|
EXIT_CODE=$?
|
||||||
|
[ "${EXIT_CODE}" = "3" ] || die "Expected mkdir failure to propagate with exit code 3, got ${EXIT_CODE}"
|
||||||
|
|
||||||
|
# Test that download failure propagates with exit code 4
|
||||||
|
nvm_get_mirror() { echo "http://example.com"; }
|
||||||
|
nvm_binary_available() { return 0; }
|
||||||
|
nvm_get_download_slug() { echo "node-v20.0.0-linux-x64"; }
|
||||||
|
nvm_get_artifact_compression() { echo "tar.gz"; }
|
||||||
|
nvm_cache_dir() { echo "${TMPDIR:-/tmp}/nvm_test_cache_$$"; }
|
||||||
|
nvm_get_checksum() { echo "fake_checksum"; }
|
||||||
|
nvm_version_greater_than_or_equal_to() { return 0; }
|
||||||
|
nvm_download() { return 1; }
|
||||||
|
nvm_grep() { return 1; }
|
||||||
|
|
||||||
|
nvm_download_artifact node binary std v20.0.0 2>/dev/null
|
||||||
|
EXIT_CODE=$?
|
||||||
|
[ "${EXIT_CODE}" = "4" ] || die "Expected download failure to propagate with exit code 4, got ${EXIT_CODE}"
|
||||||
|
|
||||||
|
# clean up any dirs created by the download failure test
|
||||||
|
command rm -rf "$(nvm_cache_dir)"
|
||||||
|
|
||||||
|
# Test that checksum failure propagates with exit code 6
|
||||||
|
nvm_download() {
|
||||||
|
while [ "$#" -gt 0 ]; do
|
||||||
|
if [ "$1" = "-o" ]; then command touch "$2"; return 0; fi
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
nvm_compare_checksum() { return 1; }
|
||||||
|
|
||||||
|
nvm_download_artifact node binary std v20.0.0 2>/dev/null
|
||||||
|
EXIT_CODE=$?
|
||||||
|
[ "${EXIT_CODE}" = "6" ] || die "Expected checksum failure to propagate with exit code 6, got ${EXIT_CODE}"
|
||||||
|
|
||||||
|
# clean up
|
||||||
|
command rm -rf "$(nvm_cache_dir)"
|
||||||
Reference in New Issue
Block a user