Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activate/deactivate hooks #452

Merged
merged 5 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions bin/pyenv-sh-activate
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ 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

# Execute `before_activate` hooks.
for hook in "${before_hooks[@]}"; do eval "$hook"; done

native-api marked this conversation as resolved.
Show resolved Hide resolved
while [ $# -gt 0 ]; do
case "$1" in
"--complete" )
Expand Down Expand Up @@ -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
27 changes: 27 additions & 0 deletions bin/pyenv-sh-deactivate
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ 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

# Execute `before_deactivate` hooks.
for hook in "${before_hooks[@]}"; do eval "$hook"; done

while [ $# -gt 0 ]; do
case "$1" in
"-f" | "--force" )
Expand Down Expand Up @@ -191,3 +215,6 @@ fi;
EOS
;;
esac

# Execute `after_deactivate` hooks.
for hook in "${after_hooks[@]}"; do eval "$hook"; done
5 changes: 5 additions & 0 deletions test/activate.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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
native-api marked this conversation as resolved.
Show resolved Hide resolved
native-api marked this conversation as resolved.
Show resolved Hide resolved
}

@test "activate virtualenv from current version" {
Expand Down
5 changes: 5 additions & 0 deletions test/conda-activate.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
5 changes: 5 additions & 0 deletions test/conda-deactivate.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
7 changes: 6 additions & 1 deletion test/deactivate.bats
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bats
#!/usr/bin/env bats

load test_helper

Expand All @@ -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 virtualenv" {
Expand Down
71 changes: 71 additions & 0 deletions test/hooks.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}