Files
nvm/test/install_script/nvm_check_global_modules
Jordan Harband d66749b21c [Fix] install.sh: run the system npm outside the project
`nvm_check_global_modules` ran `npm --version` and `npm list -g` in `$PWD`,
so the project `.npmrc` applied, eg its `logs-dir` on npm v8.11 through v10.5.
The "npm unavailable" test put its fake npm on a relative `PATH` entry, which no longer resolves from `/`.
2026-11-16 15:31:02 -08:00

104 lines
3.3 KiB
Bash
Executable File

#!/bin/sh
cleanup () {
rm -rf "$npm_config_prefix/lib" >/dev/null 2>&1
unset npm_config_prefix
[ -z "${FAKE_NPM_DIR-}" ] || rm -rf "${FAKE_NPM_DIR}"
[ -z "${WINDOWS_NPM_DIR-}" ] || rm -rf "${WINDOWS_NPM_DIR}"
PATH="$ORIGINAL_PATH"
NVM_DIR="$ORIGINAL_NVM_DIR"
unset -f setup cleanup die
unset message ORIGINAL_PATH ORIGINAL_NVM_DIR FAKE_NPM_DIR WINDOWS_NPM_DIR
}
die () { echo "$@" ; cleanup ; exit 1; }
NVM_ENV=testing \. ../../install.sh
setup () {
ORIGINAL_PATH="$PATH"
ORIGINAL_NVM_DIR="$NVM_DIR"
# Pretend we're not using NVM
unset NVM_DIR
npm_config_prefix="$(pwd)"
export npm_config_prefix
mkdir -p "$npm_config_prefix/lib"
}
setup
npm install -g nop >/dev/null || die 'nvm_check_global_modules cannot be tested because `npm` cannot install the `nop` package'
message=$(nvm_check_global_modules)
[ ! -z "$message" ] || die "nvm_check_global_modules should have printed a notice when npm had global modules installed; got:\n${message}"
if [ -n "${ORIGINAL_NVM_DIR}" ] && [ -z "${GITHUB_ACTIONS}" ]; then
# Admit we're using NVM, just for this one test
# TODO: fix this for GHA
message=$(NVM_DIR="${ORIGINAL_NVM_DIR}" nvm_check_global_modules)
[ -z "$message" ] || die "nvm_check_global_modules should not have printed a notice when npm is managed by nvm; got:\n${message}"
fi
npm uninstall -g nop >/dev/null
message=$(nvm_check_global_modules)
[ -z "$message" ] || die "nvm_check_global_modules should not have printed a notice when npm had no global modules installed; got:\n${message}"
# Faking an installation of npm
mkdir -p "$npm_config_prefix/lib/node_modules/npm"
cat <<'JSON' >"$npm_config_prefix/lib/node_modules/npm/package.json"
{ "name": "npm", "version": "0.0.1fake" }
JSON
message=$(nvm_check_global_modules)
[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm had only itself installed as a global module; got:\n${message}"
# Faking the absence of npm
FAKE_NPM_DIR="$(mktemp -d)"
touch "${FAKE_NPM_DIR}/npm"
chmod +x "${FAKE_NPM_DIR}/npm"
PATH="${FAKE_NPM_DIR}:$PATH"
message=$(nvm_check_global_modules)
[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm was unavailable; got:\n${message}"
# Faking a Windows Node's npm, with a global module installed
WINDOWS_NPM_DIR="$(mktemp -d)"
cat <<'SH' >"${WINDOWS_NPM_DIR}/npm"
#!/bin/sh
case "$1" in
--version) echo '10.0.0' ;;
list) printf '%s\n' 'npm@10.0.0' '└── windows-global@1.0.0' ;;
esac
SH
touch "${WINDOWS_NPM_DIR}/npm.cmd"
chmod +x "${WINDOWS_NPM_DIR}/npm"
PATH="${WINDOWS_NPM_DIR}:${ORIGINAL_PATH}"
nvm_is_wsl() { return 0; }
message=$(nvm_check_global_modules)
[ -z "$message" ] || die "nvm_check_global_modules should not run a Windows npm under WSL; got:\n${message}"
nvm_is_wsl() { return 1; }
message=$(nvm_check_global_modules)
[ -n "$message" ] || die 'nvm_check_global_modules should still warn about a Windows npm outside WSL, where it is the system node'
# npm must not apply the npmrc of the project that `$PWD` is in
cat <<'SH' >"${FAKE_NPM_DIR}/npm"
#!/bin/sh
case "$1" in
--version) echo '10.0.0' ;;
list) printf '%s\n' 'npm@10.0.0' "└── cwd@[$(pwd)]" ;;
esac
SH
PATH="${FAKE_NPM_DIR}:${ORIGINAL_PATH}"
message=$(nvm_check_global_modules)
case "$message" in
*"cwd@[/]"*) ;;
*) die "nvm_check_global_modules should run npm from \`/\`; got:\n${message}" ;;
esac
cleanup