Add setup function for creating a mock system venv

Change the main setup to only set PYENV_ROOT. Each test now sets up the
required virtual environments. This results in some duplication but makes
it clearer what each test is doing.

An alternative approach would be to modify the venv created by create_m_venv to
make it look like a system venv, but two distinct functions make it much clearer
what they are doing even though there is some overlap between the functions.
This commit is contained in:
Sam Doran 2026-04-27 18:06:06 -04:00
parent 5e134e155c
commit ba8598a25d
No known key found for this signature in database
GPG key ID: 01602635F94328AA

View file

@ -2,28 +2,69 @@
load test_helper
create_m_system_venv() {
# Create a mock virtual environment in the same way a venv created from
# the system Python version looks.
#
# The venv is in ${PYENV_ROOT}/versions, is not a symlink, and
# home is /usr/bin in the pyenv.cfg.
create_executable "$2" "python"
create_executable "$2" "activate"
local version="$1"
local venv="$2"
local venv_dir="${PYENV_ROOT}/versions/${venv}"
echo "home = /usr/bin" > "${venv_dir}/pyvenv.cfg"
}
create_m_venv() {
setup_m_venv "$1"
# Create a mock virtual environment from an installed (not system) Python version.
#
# The venv name is a symlink inside ${PYENV_ROOT}/versions that points to
# the real venv directory inside the Python version used to created and
# home points to the bin directory inside the venv dir.
setup_m_venv "$1/envs/$2"
local version="${1%%/envs/*}" # Get the first part of "2.7.6/envs/venv27"
local venv="${1##*/}" # Get the last part of "2.7.6/envs/venv27"
local version="$1"
local venv="$2"
local venv_dir="${PYENV_ROOT}/versions/${version}/envs/${venv}"
local version_dir="${PYENV_ROOT}/versions/${version}"
local env_dir="${version_dir}/envs/${venv}"
echo "home = ${version_dir}/bin" > "${env_dir}/pyvenv.cfg"
ln -s "${env_dir}" "${PYENV_ROOT}/versions/${venv}"
echo "home = ${PYENV_ROOT}/versions/${version}/bin" > "${venv_dir}/pyvenv.cfg"
ln -s "${venv_dir}" "${PYENV_ROOT}/versions/${venv}"
}
setup() {
export PYENV_ROOT="${TMP}/pyenv"
create_m_venv "2.7.6/envs/venv27"
create_m_venv "3.3.3/envs/venv33"
}
@test "system venv" {
stub pyenv-version-name ": echo venv314"
stub pyenv-version-origin ": echo PYENV_VERSION"
create_m_venv "3.14.3" "venv314"
create_m_system_venv "3.9.11" "system_venv"
run pyenv-virtualenvs
assert_success
assert_output <<OUT
3.14.3/envs/venv314 (created from ${PYENV_ROOT}/versions/3.14.3)
system_venv (created from /usr)
* venv314 --> ${PYENV_ROOT}/versions/3.14.3/envs/venv314 (set by PYENV_VERSION)
OUT
unstub pyenv-version-name
}
@test "list virtual environments" {
stub pyenv-version-name ": echo system"
create_m_venv "2.7.6" "venv27"
create_m_venv "3.3.3" "venv33"
run pyenv-virtualenvs
assert_success
@ -41,6 +82,9 @@ OUT
stub pyenv-version-name ": echo 3.3.3/envs/venv33"
stub pyenv-version-origin ": echo PYENV_VERSION"
create_m_venv "2.7.6" "venv27"
create_m_venv "3.3.3" "venv33"
run pyenv-virtualenvs
assert_success
@ -56,6 +100,9 @@ OUT
}
@test "list bare virtual environments" {
create_m_venv "2.7.6" "venv27"
create_m_venv "3.3.3" "venv33"
run pyenv-virtualenvs --bare
assert_success
@ -68,6 +115,9 @@ OUT
}
@test "list bare virtual environments without aliases" {
create_m_venv "2.7.6" "venv27"
create_m_venv "3.3.3" "venv33"
run pyenv-virtualenvs --bare --skip-aliases
assert_success
@ -80,6 +130,9 @@ OUT
@test "list virtual environments without aliases" {
stub pyenv-version-name ": echo system"
create_m_venv "2.7.6" "venv27"
create_m_venv "3.3.3" "venv33"
run pyenv-virtualenvs --skip-aliases
assert_success
@ -95,6 +148,9 @@ OUT
stub pyenv-version-name ": echo venv27"
stub pyenv-version-origin ": echo PYENV_VERSION"
create_m_venv "2.7.6" "venv27"
create_m_venv "3.3.3" "venv33"
run pyenv-virtualenvs
assert_success
@ -110,6 +166,9 @@ OUT
}
@test "completions output" {
create_m_venv "2.7.6" "venv27"
create_m_venv "3.3.3" "venv33"
run pyenv-virtualenvs --complete
assert_success