From 16edba8fe8f8a07943cfc4c224b8c59b213da2c1 Mon Sep 17 00:00:00 2001 From: Caleb Hattingh Date: Sun, 4 Aug 2019 12:20:04 +1000 Subject: [PATCH] Apply abspath() to the env_dir, and add a test to verify. --- tests/test_vox.py | 44 +++++++++++++++++++++++++++++++++++++++++++- xontrib/voxapi.py | 4 ++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/tests/test_vox.py b/tests/test_vox.py index 80ee59803..b74abfbac 100644 --- a/tests/test_vox.py +++ b/tests/test_vox.py @@ -1,9 +1,10 @@ """Vox tests""" -import builtins import stat import os +import subprocess as sp import pytest +import sys from xontrib.voxapi import Vox from tools import skip_if_on_conda, skip_if_on_msys @@ -80,6 +81,47 @@ def test_activate(xonsh_builtins, tmpdir): assert last_event == ("deactivate", "spam") +@skip_if_on_msys +@skip_if_on_conda +def test_activate_non_vox_venv(xonsh_builtins, tmpdir): + """ + Create a virtual environment using Python's built-in venv module + (not in VIRTUALENV_HOME) and verify that vox can activate it correctly. + """ + xonsh_builtins.__xonsh__.env.setdefault("PATH", []) + + last_event = None + + @xonsh_builtins.events.vox_on_activate + def activate(name, **_): + nonlocal last_event + last_event = "activate", name + + @xonsh_builtins.events.vox_on_deactivate + def deactivate(name, **_): + nonlocal last_event + last_event = "deactivate", name + + with tmpdir.as_cwd(): + venv_dirname = 'venv' + sp.run([sys.executable, '-m', 'venv', venv_dirname]) + vox = Vox() + vox.activate(venv_dirname) + vxv = vox[venv_dirname] + + env = xonsh_builtins.__xonsh__.env + assert os.path.isabs(vxv.bin) + assert env["PATH"][0] == vxv.bin + assert os.path.isabs(vxv.env) + assert env["VIRTUAL_ENV"] == vxv.env + assert last_event == ("activate", venv_dirname) + + vox.deactivate() + assert not env["PATH"] + assert "VIRTUAL_ENV" not in env + assert last_event == ("deactivate", tmpdir.join(venv_dirname)) + + @skip_if_on_msys @skip_if_on_conda def test_path(xonsh_builtins, tmpdir): diff --git a/xontrib/voxapi.py b/xontrib/voxapi.py index 7b274596d..414a6c7fc 100644 --- a/xontrib/voxapi.py +++ b/xontrib/voxapi.py @@ -88,7 +88,7 @@ def _mkvenv(env_dir): This only cares about the platform. No filesystem calls are made. """ - env_dir = os.path.normpath(env_dir) + env_dir = os.path.abspath(env_dir) if ON_WINDOWS: binname = os.path.join(env_dir, "Scripts") incpath = os.path.join(env_dir, "Include") @@ -358,7 +358,7 @@ class Vox(collections.abc.Mapping): self.deactivate() type(self).oldvars = {"PATH": list(env["PATH"])} - env["PATH"].insert(0, os.path.abspath(ve.bin)) + env["PATH"].insert(0, ve.bin) env["VIRTUAL_ENV"] = ve.env if "PYTHONHOME" in env: type(self).oldvars["PYTHONHOME"] = env.pop("PYTHONHOME")