diff --git a/bin/pyenv-sh-deactivate b/bin/pyenv-sh-deactivate index 4793d6f..86d2295 100755 --- a/bin/pyenv-sh-deactivate +++ b/bin/pyenv-sh-deactivate @@ -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" ) @@ -191,3 +215,6 @@ fi; EOS ;; esac + +# Execute `after_deactivate` hooks. +for hook in "${after_hooks[@]}"; do eval "$hook"; done diff --git a/test/activate.bats b/test/activate.bats index c644e4d..0bfae2a 100644 --- a/test/activate.bats +++ b/test/activate.bats @@ -16,7 +16,7 @@ setup() { unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT unset VIRTUAL_ENV_DISABLE_PROMPT unset _OLD_VIRTUAL_PS1 - stub pyenv-hooks "" + stub pyenv-hooks "activate : echo" } teardown() { diff --git a/test/conda-activate.bats b/test/conda-activate.bats index 0ac9693..7469d83 100644 --- a/test/conda-activate.bats +++ b/test/conda-activate.bats @@ -17,7 +17,7 @@ setup() { unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT unset VIRTUAL_ENV_DISABLE_PROMPT unset _OLD_VIRTUAL_PS1 - stub pyenv-hooks "" + stub pyenv-hooks "activate : echo" } teardown() { diff --git a/test/conda-deactivate.bats b/test/conda-deactivate.bats index 2abab60..905c7e5 100644 --- a/test/conda-deactivate.bats +++ b/test/conda-deactivate.bats @@ -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" { diff --git a/test/deactivate.bats b/test/deactivate.bats index 53ed0cb..394bd8c 100644 --- a/test/deactivate.bats +++ b/test/deactivate.bats @@ -1,4 +1,4 @@ - #!/usr/bin/env bats + #!/usr/bin/env bats load test_helper @@ -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" { diff --git a/test/hooks.bats b/test/hooks.bats index 88fffd5..3f7bfdf 100644 --- a/test/hooks.bats +++ b/test/hooks.bats @@ -72,3 +72,42 @@ EOS unstub pyenv-hooks unstub pyenv-sh-deactivate } + +@test "deactivate virtualenv" { + cat > "${HOOK_PATH}/deactivate.bash" </dev/null 2>&1; then + unset -f deactivate; +fi; +after +EOS + + unstub pyenv-hooks +}