mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2025-11-12 05:23:53 -05:00
init: fix shell detection when invoked from a script
- fixes `ps` command to lookup parent process - adds `assert_output_contains_not` test helper Ref: https://github.com/yyuu/pyenv/issues/373
This commit is contained in:
@@ -20,10 +20,11 @@ done
|
|||||||
|
|
||||||
shell="${1:-$PYENV_SHELL}"
|
shell="${1:-$PYENV_SHELL}"
|
||||||
if [ -z "$shell" ]; then
|
if [ -z "$shell" ]; then
|
||||||
shell="$(ps c -p "$PPID" -o 'ucomm=' 2>/dev/null || true)"
|
shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
|
||||||
shell="${shell##-}"
|
shell="${shell##-}"
|
||||||
shell="${shell%% *}"
|
shell="${shell%% *}"
|
||||||
shell="$(basename "${shell:-$SHELL}")"
|
shell="${shell:-$SHELL}"
|
||||||
|
shell="${shell##*/}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$print" ]; then
|
if [ -z "$print" ]; then
|
||||||
|
|||||||
@@ -4,12 +4,29 @@ load test_helper
|
|||||||
|
|
||||||
@test "detect parent shell" {
|
@test "detect parent shell" {
|
||||||
unset PYENV_SHELL
|
unset PYENV_SHELL
|
||||||
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
|
|
||||||
SHELL=/bin/false run pyenv-virtualenv-init -
|
SHELL=/bin/false run pyenv-virtualenv-init -
|
||||||
assert_success
|
assert_success
|
||||||
assert_output_contains ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";'
|
assert_output_contains ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "detect parent shell from script (sh)" {
|
||||||
|
unset PYENV_SHELL
|
||||||
|
printf '#!/bin/sh\necho "$(pyenv-virtualenv-init -)"' > ${BATS_TEST_DIRNAME}/script.sh
|
||||||
|
chmod +x ${BATS_TEST_DIRNAME}/script.sh
|
||||||
|
run ${BATS_TEST_DIRNAME}/script.sh
|
||||||
|
assert_success
|
||||||
|
assert_output_contains_not ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "detect parent shell from script (bash)" {
|
||||||
|
unset PYENV_SHELL
|
||||||
|
printf '#!/bin/bash\necho "$(pyenv-virtualenv-init -)"' > ${BATS_TEST_DIRNAME}/script.sh
|
||||||
|
chmod +x ${BATS_TEST_DIRNAME}/script.sh
|
||||||
|
run ${BATS_TEST_DIRNAME}/script.sh
|
||||||
|
assert_success
|
||||||
|
assert_output_contains ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";'
|
||||||
|
}
|
||||||
|
|
||||||
@test "sh-compatible instructions" {
|
@test "sh-compatible instructions" {
|
||||||
run pyenv-virtualenv-init bash
|
run pyenv-virtualenv-init bash
|
||||||
assert [ "$status" -eq 1 ]
|
assert [ "$status" -eq 1 ]
|
||||||
|
|||||||
@@ -97,6 +97,16 @@ assert_output_contains() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert_output_contains_not() {
|
||||||
|
local expected="$1"
|
||||||
|
echo "$output" | grep -F "$expected" >/dev/null && {
|
||||||
|
{ echo "expected output to not contain $expected"
|
||||||
|
echo "actual: $output"
|
||||||
|
} | flunk; return
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
create_executable() {
|
create_executable() {
|
||||||
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
|
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
|
||||||
touch "${PYENV_ROOT}/versions/$1/bin/$2"
|
touch "${PYENV_ROOT}/versions/$1/bin/$2"
|
||||||
|
|||||||
Reference in New Issue
Block a user