Skip to content

Commit

Permalink
Fix update-path to work with virtualenv>=20.26.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Tran committed Oct 10, 2024
1 parent 3a289bf commit ed796e8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
29 changes: 28 additions & 1 deletion tests/virtualenv_tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,19 @@ def test_non_absolute_error(capsys):
assert out == '--update-path must be absolute: notabs\n'


@pytest.fixture
def fake_venv_quoted(tmpdir):
tmpdir.join('bin').ensure_dir()
tmpdir.join('lib/python2.7/site-packages').ensure_dir()
tmpdir.join('bin/activate').write('VIRTUAL_ENV="/venv"\n')
yield tmpdir


@pytest.fixture
def fake_venv(tmpdir):
tmpdir.join('bin').ensure_dir()
tmpdir.join('lib/python2.7/site-packages').ensure_dir()
tmpdir.join('bin/activate').write('VIRTUAL_ENV=/venv')
tmpdir.join('bin/activate').write('VIRTUAL_ENV=/venv\n')
yield tmpdir


Expand Down Expand Up @@ -257,3 +265,22 @@ def test_not_a_virtualenv_missing_versioned_lib_directory(fake_venv, capsys):
fake_venv, fake_venv.join('lib/python#.#'),
)
assert out == expected


def test_virtualenv_path_works_with_quoted_path(fake_venv_quoted, capsys):
ret = virtualenv_tools.main(
('--update-path=auto', fake_venv_quoted.strpath)
)
out, _ = capsys.readouterr()
assert ret == 0
assert out.startswith('Updated: ')
assert '/venv ->' in out


def test_virtualenv_path_works_with_nonquoted_path(fake_venv, capsys):
ret = virtualenv_tools.main(('--update-path=auto', fake_venv.strpath))
out, _ = capsys.readouterr()
assert ret == 0
assert out.startswith('Updated: ')
assert '/venv ->' in out

9 changes: 5 additions & 4 deletions virtualenv_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import marshal
import os.path
import re
import shlex
import shutil
import sys
from types import CodeType
Expand All @@ -32,7 +33,7 @@
_pybin_match = re.compile(r'^python\d+\.\d+$')
_pypy_match = re.compile(r'^pypy\d+.\d+$')
_activation_path_re = re.compile(
r'^(?:set -gx |setenv |)VIRTUAL_ENV[ =][\'"](.*?)[\'"]\s*$',
r'^(?:set -gx |setenv |)VIRTUAL_ENV[ =][\'"]?(.*?)[\'"]?\s*$',
)
VERBOSE = False
# magic length
Expand Down Expand Up @@ -253,11 +254,11 @@ def get_orig_path(venv_path: str) -> str:
activate_path = os.path.join(venv_path, 'bin/activate')

with open(activate_path) as activate:
venv_var_prefix = 'VIRTUAL_ENV='
for line in activate:
# virtualenv 20 changes the position
for possible in ('VIRTUAL_ENV="', "VIRTUAL_ENV='"):
if line.startswith(possible):
return line.split(possible[-1], 2)[1]
if line.startswith(venv_var_prefix):
return shlex.split(line[len(venv_var_prefix):])[0]
else:
raise AssertionError(
'Could not find VIRTUAL_ENV= in activation script: %s' %
Expand Down

0 comments on commit ed796e8

Please sign in to comment.