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

Don't activate if a 3rd-party venv is activated over ours #478

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions bin/pyenv-sh-activate
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ fi
venv="${versions}"

if [ -n "${VIRTUAL_ENV}" ]; then
# exit as success if some virtualenv is already activated outside from pyenv-virtualenv
if [ -z "${PYENV_VIRTUAL_ENV}" ]; then
# exit as success if a non-pyenv virtualenv is active
if [[ -z $PYENV_VIRTUAL_ENV || $PYENV_VIRTUAL_ENV != "$VIRTUAL_ENV" ]]; then
if [ -z "${FORCE}" ]; then
if [ -z "${QUIET}" ]; then
echo "pyenv-virtualenv: virtualenv \`${VIRTUAL_ENV}' is already activated" 1>&2
Expand Down
28 changes: 28 additions & 0 deletions test/activate.bats
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,34 @@ EOS
unstub pyenv-prefix
}

@test "do nothing if a 3rd-party virtualenv is active" {
export PYENV_VIRTUALENV_INIT=1
export VIRTUAL_ENV="${TMP}/venv-3rd-party"
unset PYENV_VIRTUAL_ENV

PYENV_SHELL="bash" run pyenv-sh-activate "venv"

assert_success
assert_output <<EOS
pyenv-virtualenv: virtualenv \`${TMP}/venv-3rd-party' is already activated
true
EOS
}

@test "do nothing if a 3rd-party virtualenv is active over ours" {
export PYENV_VIRTUALENV_INIT=1
export VIRTUAL_ENV="${TMP}/venv-3rd-party"
export PYENV_VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"

PYENV_SHELL="bash" run pyenv-sh-activate "venv"

assert_success
assert_output <<EOS
pyenv-virtualenv: virtualenv \`${TMP}/venv-3rd-party' is already activated
true
EOS
}

@test "should fail if activate is invoked as a command" {
run pyenv-activate

Expand Down
27 changes: 19 additions & 8 deletions test/stubs/stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ _STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
_STUB_LOG="${PROGRAM}_STUB_LOG"

if [ -n "${!_STUB_DEBUG}" ]; then
echo "$program" "$@" >&${!_STUB_DEBUG}
fi
[ -n "${!_STUB_LOG}" ] || eval "${_STUB_LOG}"="${TMPDIR}/${program}-stub-log"
if test -z "${!_STUB_END}"; then echo "$program" "$@" >>"${!_STUB_LOG}"; fi

[ -e "${!_STUB_PLAN}" ] || exit 1
[ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run"
Expand All @@ -24,7 +23,7 @@ fi
# Initialize or load the stub run information.
eval "${_STUB_INDEX}"=1
eval "${_STUB_RESULT}"=0
[ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}"
if test -e "${!_STUB_RUN}"; then source "${!_STUB_RUN}"; fi


# Loop over each line in the plan.
Expand Down Expand Up @@ -80,14 +79,26 @@ done < "${!_STUB_PLAN}"


if [ -n "${!_STUB_END}" ]; then
# Clean up the run file.
rm -f "${!_STUB_RUN}"

# If the number of lines in the plan is larger than
# the requested index, we failed.
if [ $index -ge "${!_STUB_INDEX}" ]; then
eval "${_STUB_RESULT}"=1
fi
if [ "${!_STUB_RESULT}" -ne 0 ]; then
{
echo "index: $index; stub index: ${!_STUB_INDEX}"
echo "plan:"
cat "${!_STUB_PLAN}" || true
echo "run:"
cat "${!_STUB_RUN}" || true
echo "log:"
cat "${!_STUB_LOG}" || true
} >&2
fi

# Clean up the run file.
rm -f "${!_STUB_RUN}"
rm -f "${!_STUB_LOG}"

# Return the result.
exit "${!_STUB_RESULT}"
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ stub() {

export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan"
export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run"
export "${prefix}_STUB_LOG"="${TMP}/${program}-stub-log"
export "${prefix}_STUB_END"=

mkdir -p "${TMP}/bin"
Expand Down
Loading