mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-12-06 22:52:23 +03:00
[Tests] nvm_npm: add unit tests
This commit is contained in:
1 file changed
+118
Executable
+118
@@ -0,0 +1,118 @@
|
||||
#!/bin/sh
|
||||
|
||||
TEST_DIR=''
|
||||
|
||||
cleanup () {
|
||||
unset -f npm nvm_cd_root chpwd 2>/dev/null
|
||||
case "${TEST_DIR}" in
|
||||
/*) rm -rf "${TEST_DIR}" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TEST_DIR="$(mktemp -d)" || die 'could not make a temp dir'
|
||||
mkdir -p "${TEST_DIR}/bin" "${TEST_DIR}/caller" || die 'could not make the test dirs'
|
||||
NPM_CALLS="${TEST_DIR}/npm-calls"
|
||||
|
||||
cat > "${TEST_DIR}/bin/npm" <<FAKE
|
||||
#!/bin/sh
|
||||
echo "cwd: \$(pwd)" >> '${NPM_CALLS}'
|
||||
for arg in "\$@"; do
|
||||
echo "[\${arg}]" >> '${NPM_CALLS}'
|
||||
done
|
||||
[ -z "\${FAKE_NPM_STDIN-}" ] || echo "stdin: \$(cat)" >> '${NPM_CALLS}'
|
||||
echo 'npm stdout'
|
||||
echo 'npm stderr' >&2
|
||||
exit "\${FAKE_NPM_EXIT:-0}"
|
||||
FAKE
|
||||
chmod +x "${TEST_DIR}/bin/npm" || die 'could not make the fake npm'
|
||||
PATH="${TEST_DIR}/bin:${PATH}"
|
||||
unset FAKE_NPM_STDIN
|
||||
export FAKE_NPM_EXIT
|
||||
|
||||
check_calls () {
|
||||
CALLS="$(cat "${NPM_CALLS}" 2>/dev/null)"
|
||||
[ "_${CALLS}" = "_$2" ] || die "$1: expected npm calls >$2<; got >${CALLS}<"
|
||||
rm -f "${NPM_CALLS}"
|
||||
}
|
||||
|
||||
cd "${TEST_DIR}/caller" || die 'could not cd to the caller dir'
|
||||
CALLER_PWD="$(pwd)"
|
||||
|
||||
FAKE_NPM_EXIT=0
|
||||
nvm_npm --version >/dev/null 2>&1 || die "nvm_npm should succeed when npm succeeds; got $?"
|
||||
check_calls 'single argument' 'cwd: /
|
||||
[--version]'
|
||||
[ "_$(pwd)" = "_${CALLER_PWD}" ] || die "nvm_npm should not change the caller's directory: got >$(pwd)<, expected >${CALLER_PWD}<"
|
||||
|
||||
nvm_npm >/dev/null 2>&1 || die "nvm_npm with no arguments should succeed when npm succeeds; got $?"
|
||||
check_calls 'no arguments' 'cwd: /'
|
||||
|
||||
nvm_npm 'a b' '' '*' '$HOME' "it's" '-g' >/dev/null 2>&1 || die 'nvm_npm with special arguments should succeed'
|
||||
check_calls 'special arguments' 'cwd: /
|
||||
[a b]
|
||||
[]
|
||||
[*]
|
||||
[$HOME]
|
||||
[it'"'"'s]
|
||||
[-g]'
|
||||
|
||||
OUTPUT="$(nvm_npm ls 2>/dev/null)"
|
||||
[ "_${OUTPUT}" = '_npm stdout' ] || die "nvm_npm should pass through npm's stdout: got >${OUTPUT}<, expected >npm stdout<"
|
||||
check_calls 'stdout' 'cwd: /
|
||||
[ls]'
|
||||
|
||||
OUTPUT="$(nvm_npm ls 2>&1 >/dev/null)"
|
||||
[ "_${OUTPUT}" = '_npm stderr' ] || die "nvm_npm should pass through npm's stderr: got >${OUTPUT}<, expected >npm stderr<"
|
||||
check_calls 'stderr' 'cwd: /
|
||||
[ls]'
|
||||
|
||||
echo piped | FAKE_NPM_STDIN=1 nvm_npm ls >/dev/null 2>&1 || die 'nvm_npm with piped stdin should succeed'
|
||||
check_calls 'stdin' 'cwd: /
|
||||
[ls]
|
||||
stdin: piped'
|
||||
|
||||
FAKE_NPM_EXIT=3
|
||||
EXIT_CODE=0
|
||||
nvm_npm install >/dev/null 2>&1 || EXIT_CODE=$?
|
||||
[ "_${EXIT_CODE}" = '_3' ] || die "nvm_npm should return npm's exit code: got >${EXIT_CODE}<, expected >3<"
|
||||
check_calls 'failing npm' 'cwd: /
|
||||
[install]'
|
||||
[ "_$(pwd)" = "_${CALLER_PWD}" ] || die "a failing npm should not change the caller's directory: got >$(pwd)<"
|
||||
FAKE_NPM_EXIT=0
|
||||
|
||||
if nvm_is_zsh; then
|
||||
eval 'chpwd () { echo "chpwd: $(pwd)" >> "${NPM_CALLS}"; }'
|
||||
nvm_npm --version >/dev/null 2>&1 || die 'nvm_npm should succeed with a chpwd hook'
|
||||
check_calls 'zsh chpwd hook' 'cwd: /
|
||||
[--version]'
|
||||
unset -f chpwd
|
||||
fi
|
||||
|
||||
npm () {
|
||||
NVM_NPM_TEST_LEAK=leaked
|
||||
nvm_cd "${TEST_DIR}/bin" || return 7
|
||||
echo "function: $(pwd) $*"
|
||||
}
|
||||
unset NVM_NPM_TEST_LEAK
|
||||
OUTPUT="$(nvm_npm x y)"
|
||||
EXPECTED="function: $(cd "${TEST_DIR}/bin" && pwd) x y"
|
||||
[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "nvm_npm should run an \`npm\` function: got >${OUTPUT}<, expected >${EXPECTED}<"
|
||||
nvm_npm x y >/dev/null || die 'nvm_npm should succeed when the npm function succeeds'
|
||||
[ "_${NVM_NPM_TEST_LEAK-unset}" = '_unset' ] || die "nvm_npm should run npm in a subshell: got >${NVM_NPM_TEST_LEAK-unset}<, expected >unset<"
|
||||
[ "_$(pwd)" = "_${CALLER_PWD}" ] || die "a directory change inside npm should not reach the caller: got >$(pwd)<, expected >${CALLER_PWD}<"
|
||||
unset -f npm
|
||||
|
||||
nvm_cd_root () { echo 'cd failed' >&2; return 5; }
|
||||
EXIT_CODE=0
|
||||
OUTPUT="$(nvm_npm --version 2>&1)" || EXIT_CODE=$?
|
||||
[ "_${EXIT_CODE}" = '_5' ] || die "nvm_npm should fail when it cannot cd to /: got >${EXIT_CODE}<, expected >5<"
|
||||
[ "_${OUTPUT}" = '_cd failed' ] || die "nvm_npm should show the cd error: got >${OUTPUT}<, expected >cd failed<"
|
||||
check_calls 'failing nvm_cd_root' ''
|
||||
unset -f nvm_cd_root
|
||||
|
||||
cleanup
|
||||
Reference in new issue
Block a user