Skip to content
Myr

nvm_validate_install

This file

Path

test/fast/Unit tests/nvm_validate_install

53 lines · 2.2 KiB

Last changed by

[Robustness] `nvm install`: reject a structurally broken installed versionJordan Harbandbefore adoptionaee1f83

History of this file

Take it

1#!/bin/sh23cleanup() {4  [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"5  unset -f die cleanup6  unset NVM_DIR version version_path7}89die() { echo "$@" ; cleanup ; exit 1; }1011: nvm.sh12\. ../../../nvm.sh1314type nvm_validate_install > /dev/null 2>&1 || die 'nvm_validate_install is not available'1516NVM_DIR="$(mktemp -d)"17[ -n "${NVM_DIR}" ] || die 'Unable to create temporary folder'1819version='v20.0.0'20version_path="${NVM_DIR}/versions/node/${version}"2122# No version directory at all: not valid.23nvm_validate_install "${version}" 2>/dev/null && die 'a missing version dir should not validate'2425# A healthy install: a non-empty node plus an npm symlink that resolves.26# (validation checks layout, not execution, so bin/node need not be a real27# runnable binary here - just non-empty and executable.)28mkdir -p "${version_path}/bin" "${version_path}/lib/node_modules/npm/bin" || die 'setup mkdir failed'29printf '#!/bin/sh\necho "%s"\n' "${version}" > "${version_path}/bin/node"30chmod +x "${version_path}/bin/node"31echo 'npm-cli' > "${version_path}/lib/node_modules/npm/bin/npm-cli.js"32( cd "${version_path}/bin" && ln -s ../lib/node_modules/npm/bin/npm-cli.js npm )33nvm_validate_install "${version}" 2>/dev/null || die 'a healthy install should validate'3435# A binary that is correctly installed but cannot run on this host (e.g. a36# newer node on an older glibc) must STILL validate: whether it runs is the37# host's concern, not a broken install.38printf '#!/bin/sh\nexit 1\n' > "${version_path}/bin/node"39chmod +x "${version_path}/bin/node"40nvm_validate_install "${version}" 2>/dev/null || die 'a non-runnable but present binary should still validate'4142# A zero-byte but executable node: passes `[ -x ]` but must not validate.43: > "${version_path}/bin/node"44chmod +x "${version_path}/bin/node"45nvm_validate_install "${version}" 2>/dev/null && die 'a zero-byte node should not validate'4647# A non-empty node again, but with a dangling npm symlink: must not validate.48printf '#!/bin/sh\necho "%s"\n' "${version}" > "${version_path}/bin/node"49chmod +x "${version_path}/bin/node"50rm -f "${version_path}/lib/node_modules/npm/bin/npm-cli.js"51nvm_validate_install "${version}" 2>/dev/null && die 'a dangling npm symlink should not validate'5253cleanup