mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2025-11-08 11:33:55 -05:00
Add activate/deactivate hooks (#452)
This commit is contained in:
@@ -25,6 +25,27 @@ resolve_link() {
|
||||
unset FORCE
|
||||
unset QUIET
|
||||
|
||||
# Define `before_activate` and `after_activate` functions that allow
|
||||
# plugin hooks to register a string of code for execution before or
|
||||
# after activating a virtualenv.
|
||||
declare -a before_hooks after_hooks
|
||||
|
||||
before_activate() {
|
||||
local hook="$1"
|
||||
before_hooks["${#before_hooks[@]}"]="$hook"
|
||||
}
|
||||
|
||||
after_activate() {
|
||||
local hook="$1"
|
||||
after_hooks["${#after_hooks[@]}"]="$hook"
|
||||
}
|
||||
|
||||
# Load plugin hooks.
|
||||
OLDIFS="$IFS"
|
||||
IFS=$'\n' scripts=(`pyenv-hooks activate`)
|
||||
IFS="$OLDIFS"
|
||||
for script in "${scripts[@]}"; do source "$script"; done
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
"--complete" )
|
||||
@@ -137,6 +158,9 @@ fi
|
||||
|
||||
pyenv-sh-deactivate --force --quiet || true
|
||||
|
||||
# Execute `before_activate` hooks.
|
||||
for hook in "${before_hooks[@]}"; do eval "$hook"; done
|
||||
|
||||
if [ -n "$PYENV_VIRTUALENV_VERBOSE_ACTIVATE" ]; then
|
||||
echo "pyenv-virtualenv: activate ${venv}" 1>&2
|
||||
fi
|
||||
@@ -258,3 +282,6 @@ if [ -d "${prefix}/conda-meta" ] ||
|
||||
esac
|
||||
shopt -u nullglob
|
||||
fi
|
||||
|
||||
# Execute `after_activate` hooks.
|
||||
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
||||
|
||||
@@ -16,6 +16,27 @@ fi
|
||||
unset FORCE
|
||||
unset QUIET
|
||||
|
||||
# Define `before_deactivate` and `after_deactivate` functions that allow
|
||||
# plugin hooks to register a string of code for execution before or
|
||||
# after deactivating a virtualenv.
|
||||
declare -a before_hooks after_hooks
|
||||
|
||||
before_deactivate() {
|
||||
local hook="$1"
|
||||
before_hooks["${#before_hooks[@]}"]="$hook"
|
||||
}
|
||||
|
||||
after_deactivate() {
|
||||
local hook="$1"
|
||||
after_hooks["${#after_hooks[@]}"]="$hook"
|
||||
}
|
||||
|
||||
# Load plugin hooks.
|
||||
OLDIFS="$IFS"
|
||||
IFS=$'\n' scripts=(`pyenv-hooks deactivate`)
|
||||
IFS="$OLDIFS"
|
||||
for script in "${scripts[@]}"; do source "$script"; done
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
"-f" | "--force" )
|
||||
@@ -54,6 +75,9 @@ else
|
||||
venv="${prefix##*/}"
|
||||
fi
|
||||
|
||||
# Execute `before_deactivate` hooks.
|
||||
for hook in "${before_hooks[@]}"; do eval "$hook"; done
|
||||
|
||||
if [ -n "$PYENV_VIRTUALENV_VERBOSE_ACTIVATE" ]; then
|
||||
echo "pyenv-virtualenv: deactivate ${venv}" 1>&2
|
||||
fi
|
||||
@@ -191,3 +215,6 @@ fi;
|
||||
EOS
|
||||
;;
|
||||
esac
|
||||
|
||||
# Execute `after_deactivate` hooks.
|
||||
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
||||
|
||||
@@ -16,6 +16,7 @@ setup() {
|
||||
unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT
|
||||
unset VIRTUAL_ENV_DISABLE_PROMPT
|
||||
unset _OLD_VIRTUAL_PS1
|
||||
stub pyenv-hooks "activate : echo"
|
||||
}
|
||||
|
||||
@test "activate virtualenv from current version" {
|
||||
|
||||
@@ -17,6 +17,11 @@ setup() {
|
||||
unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT
|
||||
unset VIRTUAL_ENV_DISABLE_PROMPT
|
||||
unset _OLD_VIRTUAL_PS1
|
||||
stub pyenv-hooks "activate : echo"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
unstub pyenv-hooks
|
||||
}
|
||||
|
||||
@test "activate conda root from current version" {
|
||||
|
||||
@@ -16,6 +16,11 @@ setup() {
|
||||
unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT
|
||||
unset VIRTUAL_ENV_DISABLE_PROMPT
|
||||
unset _OLD_VIRTUAL_PS1
|
||||
stub pyenv-hooks "deactivate : echo"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
unstub pyenv-hooks
|
||||
}
|
||||
|
||||
@test "deactivate conda root" {
|
||||
|
||||
@@ -16,6 +16,7 @@ setup() {
|
||||
unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT
|
||||
unset VIRTUAL_ENV_DISABLE_PROMPT
|
||||
unset _OLD_VIRTUAL_PS1
|
||||
stub pyenv-hooks "deactivate : echo"
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv" {
|
||||
|
||||
@@ -40,3 +40,74 @@ OUT
|
||||
unstub pyenv-rehash
|
||||
teardown_version "3.5.1"
|
||||
}
|
||||
|
||||
@test "pyenv-sh-activate hooks" {
|
||||
cat > "${HOOK_PATH}/activate.bash" <<OUT
|
||||
before_activate 'echo "before"'
|
||||
after_activate 'echo "after"'
|
||||
OUT
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
|
||||
stub pyenv-version-name "echo venv"
|
||||
stub pyenv-virtualenv-prefix ""
|
||||
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
|
||||
stub pyenv-hooks "activate : echo '$HOOK_PATH'/activate.bash"
|
||||
stub pyenv-sh-deactivate ""
|
||||
|
||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
before
|
||||
export PYENV_VIRTUAL_ENV="${PYENV_ROOT}/versions/venv";
|
||||
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv";
|
||||
export _OLD_VIRTUAL_PS1="\${PS1:-}";
|
||||
export PS1="(venv) \${PS1:-}";
|
||||
after
|
||||
EOS
|
||||
|
||||
unstub pyenv-version-name
|
||||
unstub pyenv-virtualenv-prefix
|
||||
unstub pyenv-prefix
|
||||
unstub pyenv-hooks
|
||||
unstub pyenv-sh-deactivate
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv" {
|
||||
cat > "${HOOK_PATH}/deactivate.bash" <<OUT
|
||||
before_deactivate 'echo "before"'
|
||||
after_deactivate 'echo "after"'
|
||||
OUT
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
export PYENV_VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
|
||||
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
|
||||
export PYENV_ACTIVATE_SHELL=
|
||||
stub pyenv-hooks "deactivate : echo '$HOOK_PATH'/deactivate.bash"
|
||||
|
||||
PYENV_SHELL="bash" run pyenv-sh-deactivate
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
before
|
||||
unset PYENV_VIRTUAL_ENV;
|
||||
unset VIRTUAL_ENV;
|
||||
if [ -n "\${_OLD_VIRTUAL_PATH:-}" ]; then
|
||||
export PATH="\${_OLD_VIRTUAL_PATH}";
|
||||
unset _OLD_VIRTUAL_PATH;
|
||||
fi;
|
||||
if [ -n "\${_OLD_VIRTUAL_PYTHONHOME:-}" ]; then
|
||||
export PYTHONHOME="\${_OLD_VIRTUAL_PYTHONHOME}";
|
||||
unset _OLD_VIRTUAL_PYTHONHOME;
|
||||
fi;
|
||||
if [ -n "\${_OLD_VIRTUAL_PS1:-}" ]; then
|
||||
export PS1="\${_OLD_VIRTUAL_PS1}";
|
||||
unset _OLD_VIRTUAL_PS1;
|
||||
fi;
|
||||
if declare -f deactivate 1>/dev/null 2>&1; then
|
||||
unset -f deactivate;
|
||||
fi;
|
||||
after
|
||||
EOS
|
||||
|
||||
unstub pyenv-hooks
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user