mirror of
https://github.com/nvm-sh/nvm.git
synced 2025-07-03 14:13:43 +00:00
aliases-circular: steal test helper from rbenv and use it
This commit is contained in:
parent
0852f2e779
commit
7e0fc2ea72
@ -1,5 +1,7 @@
|
|||||||
#!/usr/bin/env bats
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
load test_helper
|
||||||
|
|
||||||
NVM_SRC_DIR="${BATS_TEST_DIRNAME}/../.."
|
NVM_SRC_DIR="${BATS_TEST_DIRNAME}/../.."
|
||||||
|
|
||||||
setup() {
|
setup() {
|
||||||
@ -30,35 +32,27 @@ teardown() {
|
|||||||
die () { echo $@ ; exit 1; }
|
die () { echo $@ ; exit 1; }
|
||||||
|
|
||||||
run nvm_resolve_alias loopback
|
run nvm_resolve_alias loopback
|
||||||
[ "$output" -eq "∞" ]
|
assert_success "∞"
|
||||||
|
|
||||||
run nvm alias loopback
|
run nvm alias loopback
|
||||||
[ "$output" -eq "loopback -> loopback (-> ∞)" ]
|
assert_success "loopback -> loopback (-> ∞)"
|
||||||
|
|
||||||
ALIAS="$(nvm_resolve_alias one)"
|
run nvm_resolve_alias one
|
||||||
[ "_$ALIAS" = "_∞" ]
|
assert_success "∞"
|
||||||
OUTPUT="$(nvm alias one)"
|
run nvm alias one
|
||||||
EXPECTED_OUTPUT="one -> two (-> ∞)"
|
assert_success "one -> two (-> ∞)"
|
||||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ]
|
|
||||||
|
|
||||||
ALIAS="$(nvm_resolve_alias two)"
|
run nvm_resolve_alias two
|
||||||
[ "_$ALIAS" = "_∞" ]
|
assert_success "∞"
|
||||||
OUTPUT="$(nvm alias two)"
|
run nvm alias two
|
||||||
EXPECTED_OUTPUT="two -> three (-> ∞)"
|
assert_success "two -> three (-> ∞)"
|
||||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ]
|
|
||||||
|
|
||||||
ALIAS="$(nvm_resolve_alias three)"
|
run nvm_resolve_alias three
|
||||||
[ "_$ALIAS" = "_∞" ]
|
assert_success "∞"
|
||||||
OUTPUT="$(nvm alias three)"
|
run nvm alias three
|
||||||
EXPECTED_OUTPUT="three -> one (-> ∞)"
|
assert_success "three -> one (-> ∞)"
|
||||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ]
|
|
||||||
|
|
||||||
ALIAS="$(nvm_resolve_alias four)"
|
run nvm_resolve_alias four
|
||||||
[ "_$ALIAS" = "_∞" ]
|
assert_success "∞"
|
||||||
OUTPUT="$(nvm alias four)"
|
run nvm alias four
|
||||||
EXPECTED_OUTPUT="four -> two (-> ∞)"
|
assert_success "four -> two (-> ∞)"
|
||||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ]
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
127
test/bats/test_helper.bash
Normal file
127
test/bats/test_helper.bash
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
# test_helper.bash stolen from rbenv
|
||||||
|
#
|
||||||
|
#unset RBENV_VERSION
|
||||||
|
#unset RBENV_DIR
|
||||||
|
|
||||||
|
if enable -f "${BATS_TEST_DIRNAME}"/../libexec/rbenv-realpath.dylib realpath 2>/dev/null; then
|
||||||
|
RBENV_TEST_DIR="$(realpath "$BATS_TMPDIR")/rbenv"
|
||||||
|
else
|
||||||
|
if [ -n "$RBENV_NATIVE_EXT" ]; then
|
||||||
|
echo "rbenv: failed to load \`realpath' builtin" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
RBENV_TEST_DIR="${BATS_TMPDIR}/rbenv"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# guard against executing this block twice due to bats internals
|
||||||
|
if [ "$RBENV_ROOT" != "${RBENV_TEST_DIR}/root" ]; then
|
||||||
|
export RBENV_ROOT="${RBENV_TEST_DIR}/root"
|
||||||
|
export HOME="${RBENV_TEST_DIR}/home"
|
||||||
|
|
||||||
|
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
|
||||||
|
PATH="${RBENV_TEST_DIR}/bin:$PATH"
|
||||||
|
PATH="${BATS_TEST_DIRNAME}/../libexec:$PATH"
|
||||||
|
PATH="${BATS_TEST_DIRNAME}/libexec:$PATH"
|
||||||
|
PATH="${RBENV_ROOT}/shims:$PATH"
|
||||||
|
export PATH
|
||||||
|
fi
|
||||||
|
|
||||||
|
teardown() {
|
||||||
|
rm -rf "$RBENV_TEST_DIR"
|
||||||
|
}
|
||||||
|
|
||||||
|
flunk() {
|
||||||
|
{ if [ "$#" -eq 0 ]; then cat -
|
||||||
|
else echo "$@"
|
||||||
|
fi
|
||||||
|
} | sed "s:${RBENV_TEST_DIR}:TEST_DIR:g" >&2
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_success() {
|
||||||
|
if [ "$status" -ne 0 ]; then
|
||||||
|
flunk "command failed with exit status $status"
|
||||||
|
elif [ "$#" -gt 0 ]; then
|
||||||
|
assert_output "$1"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_failure() {
|
||||||
|
if [ "$status" -eq 0 ]; then
|
||||||
|
flunk "expected failed exit status"
|
||||||
|
elif [ "$#" -gt 0 ]; then
|
||||||
|
assert_output "$1"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_equal() {
|
||||||
|
if [ "$1" != "$2" ]; then
|
||||||
|
{ echo "expected: $1"
|
||||||
|
echo "actual: $2"
|
||||||
|
} | flunk
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_output() {
|
||||||
|
local expected
|
||||||
|
if [ $# -eq 0 ]; then expected="$(cat -)"
|
||||||
|
else expected="$1"
|
||||||
|
fi
|
||||||
|
assert_equal "$expected" "$output"
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_line() {
|
||||||
|
if [ "$1" -ge 0 ] 2>/dev/null; then
|
||||||
|
assert_equal "$2" "${lines[$1]}"
|
||||||
|
else
|
||||||
|
local line
|
||||||
|
for line in "${lines[@]}"; do
|
||||||
|
if [ "$line" = "$1" ]; then return 0; fi
|
||||||
|
done
|
||||||
|
flunk "expected line \`$1'"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
refute_line() {
|
||||||
|
if [ "$1" -ge 0 ] 2>/dev/null; then
|
||||||
|
local num_lines="${#lines[@]}"
|
||||||
|
if [ "$1" -lt "$num_lines" ]; then
|
||||||
|
flunk "output has $num_lines lines"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
local line
|
||||||
|
for line in "${lines[@]}"; do
|
||||||
|
if [ "$line" = "$1" ]; then
|
||||||
|
flunk "expected to not find line \`$line'"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
assert() {
|
||||||
|
if ! "$@"; then
|
||||||
|
flunk "failed: $@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Output a modified PATH that ensures that the given executable is not present,
|
||||||
|
# but in which system utils necessary for rbenv operation are still available.
|
||||||
|
path_without() {
|
||||||
|
local exe="$1"
|
||||||
|
local path="${PATH}:"
|
||||||
|
local found alt util
|
||||||
|
for found in $(which -a "$exe"); do
|
||||||
|
found="${found%/*}"
|
||||||
|
if [ "$found" != "${RBENV_ROOT}/shims" ]; then
|
||||||
|
alt="${RBENV_TEST_DIR}/$(echo "${found#/}" | tr '/' '-')"
|
||||||
|
mkdir -p "$alt"
|
||||||
|
for util in bash head cut readlink greadlink; do
|
||||||
|
if [ -x "${found}/$util" ]; then
|
||||||
|
ln -s "${found}/$util" "${alt}/$util"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
path="${path/${found}:/${alt}:}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo "${path%:}"
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user