Skip to content

Commit f8469a1

Browse files
authored
Merge pull request #478 from native-api/3drparty_venv_over_ours
Don't activate if a 3rd-party venv is activated over ours
2 parents 0f83c33 + ac28398 commit f8469a1

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

bin/pyenv-sh-activate

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ fi
9696
venv="${versions}"
9797

9898
if [ -n "${VIRTUAL_ENV}" ]; then
99-
# exit as success if some virtualenv is already activated outside from pyenv-virtualenv
100-
if [ -z "${PYENV_VIRTUAL_ENV}" ]; then
99+
# exit as success if a non-pyenv virtualenv is active
100+
if [[ -z $PYENV_VIRTUAL_ENV || $PYENV_VIRTUAL_ENV != "$VIRTUAL_ENV" ]]; then
101101
if [ -z "${FORCE}" ]; then
102102
if [ -z "${QUIET}" ]; then
103103
echo "pyenv-virtualenv: virtualenv \`${VIRTUAL_ENV}' is already activated" 1>&2

test/activate.bats

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,34 @@ EOS
439439
unstub pyenv-prefix
440440
}
441441

442+
@test "do nothing if a 3rd-party virtualenv is active" {
443+
export PYENV_VIRTUALENV_INIT=1
444+
export VIRTUAL_ENV="${TMP}/venv-3rd-party"
445+
unset PYENV_VIRTUAL_ENV
446+
447+
PYENV_SHELL="bash" run pyenv-sh-activate "venv"
448+
449+
assert_success
450+
assert_output <<EOS
451+
pyenv-virtualenv: virtualenv \`${TMP}/venv-3rd-party' is already activated
452+
true
453+
EOS
454+
}
455+
456+
@test "do nothing if a 3rd-party virtualenv is active over ours" {
457+
export PYENV_VIRTUALENV_INIT=1
458+
export VIRTUAL_ENV="${TMP}/venv-3rd-party"
459+
export PYENV_VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
460+
461+
PYENV_SHELL="bash" run pyenv-sh-activate "venv"
462+
463+
assert_success
464+
assert_output <<EOS
465+
pyenv-virtualenv: virtualenv \`${TMP}/venv-3rd-party' is already activated
466+
true
467+
EOS
468+
}
469+
442470
@test "should fail if activate is invoked as a command" {
443471
run pyenv-activate
444472

test/stubs/stub

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ _STUB_RUN="${PROGRAM}_STUB_RUN"
1111
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
1212
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
1313
_STUB_END="${PROGRAM}_STUB_END"
14-
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
14+
_STUB_LOG="${PROGRAM}_STUB_LOG"
1515

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

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

2928

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

8180

8281
if [ -n "${!_STUB_END}" ]; then
83-
# Clean up the run file.
84-
rm -f "${!_STUB_RUN}"
85-
8682
# If the number of lines in the plan is larger than
8783
# the requested index, we failed.
8884
if [ $index -ge "${!_STUB_INDEX}" ]; then
8985
eval "${_STUB_RESULT}"=1
9086
fi
87+
if [ "${!_STUB_RESULT}" -ne 0 ]; then
88+
{
89+
echo "index: $index; stub index: ${!_STUB_INDEX}"
90+
echo "plan:"
91+
cat "${!_STUB_PLAN}" || true
92+
echo "run:"
93+
cat "${!_STUB_RUN}" || true
94+
echo "log:"
95+
cat "${!_STUB_LOG}" || true
96+
} >&2
97+
fi
98+
99+
# Clean up the run file.
100+
rm -f "${!_STUB_RUN}"
101+
rm -f "${!_STUB_LOG}"
91102

92103
# Return the result.
93104
exit "${!_STUB_RESULT}"

test/test_helper.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ stub() {
1616

1717
export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan"
1818
export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run"
19+
export "${prefix}_STUB_LOG"="${TMP}/${program}-stub-log"
1920
export "${prefix}_STUB_END"=
2021

2122
mkdir -p "${TMP}/bin"

0 commit comments

Comments
 (0)