From ba8598a25dd065a7f86f68f6bd6b8daa8c9b4c44 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Mon, 27 Apr 2026 18:06:06 -0400 Subject: [PATCH] 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. --- test/virtualenvs.bats | 79 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 10 deletions(-) diff --git a/test/virtualenvs.bats b/test/virtualenvs.bats index 9f66782..fecff33 100644 --- a/test/virtualenvs.bats +++ b/test/virtualenvs.bats @@ -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 < ${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