2016-07-20 14:50:22 -04:00
|
|
|
"""Vox tests"""
|
|
|
|
|
|
|
|
import stat
|
|
|
|
import os
|
2019-08-04 12:20:04 +10:00
|
|
|
import subprocess as sp
|
2016-10-02 21:49:38 -04:00
|
|
|
import pytest
|
2019-08-04 12:20:04 +10:00
|
|
|
import sys
|
2016-07-20 16:22:23 -04:00
|
|
|
from xontrib.voxapi import Vox
|
2016-07-20 14:50:22 -04:00
|
|
|
|
2020-08-05 07:29:31 -07:00
|
|
|
from tools import skip_if_on_conda, skip_if_on_msys
|
2016-10-02 21:49:38 -04:00
|
|
|
from xonsh.platform import ON_WINDOWS
|
2016-07-23 11:21:41 -04:00
|
|
|
|
2016-07-23 17:27:27 -04:00
|
|
|
|
2017-12-31 17:36:21 +01:00
|
|
|
@skip_if_on_msys
|
2016-07-23 17:27:27 -04:00
|
|
|
@skip_if_on_conda
|
2016-07-20 14:50:22 -04:00
|
|
|
def test_crud(xonsh_builtins, tmpdir):
|
|
|
|
"""
|
|
|
|
Creates a virtual environment, gets it, enumerates it, and then deletes it.
|
|
|
|
"""
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env["VIRTUALENV_HOME"] = str(tmpdir)
|
2016-08-28 13:37:23 -04:00
|
|
|
|
|
|
|
last_event = None
|
|
|
|
|
|
|
|
@xonsh_builtins.events.vox_on_create
|
2017-01-14 19:06:49 -05:00
|
|
|
def create(name, **_):
|
2016-08-28 13:37:23 -04:00
|
|
|
nonlocal last_event
|
2018-08-30 09:18:49 -05:00
|
|
|
last_event = "create", name
|
2016-08-28 13:37:23 -04:00
|
|
|
|
|
|
|
@xonsh_builtins.events.vox_on_delete
|
2017-01-14 19:06:49 -05:00
|
|
|
def delete(name, **_):
|
2016-08-28 13:37:23 -04:00
|
|
|
nonlocal last_event
|
2018-08-30 09:18:49 -05:00
|
|
|
last_event = "delete", name
|
2016-08-28 13:37:23 -04:00
|
|
|
|
2016-07-20 14:50:22 -04:00
|
|
|
vox = Vox()
|
2018-08-30 09:18:49 -05:00
|
|
|
vox.create("spam")
|
|
|
|
assert stat.S_ISDIR(tmpdir.join("spam").stat().mode)
|
|
|
|
assert last_event == ("create", "spam")
|
2016-07-20 14:50:22 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
ve = vox["spam"]
|
|
|
|
assert ve.env == str(tmpdir.join("spam"))
|
2016-09-13 20:29:44 -04:00
|
|
|
assert os.path.isdir(ve.bin)
|
2016-07-20 14:50:22 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
assert "spam" in vox
|
|
|
|
assert "spam" in list(vox)
|
2016-07-20 14:50:22 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
del vox["spam"]
|
2016-07-20 14:50:22 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
assert not tmpdir.join("spam").check()
|
|
|
|
assert last_event == ("delete", "spam")
|
2016-07-20 14:50:22 -04:00
|
|
|
|
2016-07-23 17:27:27 -04:00
|
|
|
|
2017-12-31 17:36:21 +01:00
|
|
|
@skip_if_on_msys
|
2016-07-23 17:27:27 -04:00
|
|
|
@skip_if_on_conda
|
2016-07-20 14:50:22 -04:00
|
|
|
def test_activate(xonsh_builtins, tmpdir):
|
|
|
|
"""
|
|
|
|
Creates a virtual environment, gets it, enumerates it, and then deletes it.
|
|
|
|
"""
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env["VIRTUALENV_HOME"] = str(tmpdir)
|
2016-07-20 14:50:22 -04:00
|
|
|
# I consider the case that the user doesn't have a PATH set to be unreasonable
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env.setdefault("PATH", [])
|
2016-08-28 13:37:23 -04:00
|
|
|
|
|
|
|
last_event = None
|
|
|
|
|
|
|
|
@xonsh_builtins.events.vox_on_activate
|
2017-01-14 19:06:49 -05:00
|
|
|
def activate(name, **_):
|
2016-08-28 13:37:23 -04:00
|
|
|
nonlocal last_event
|
2018-08-30 09:18:49 -05:00
|
|
|
last_event = "activate", name
|
2016-08-28 13:37:23 -04:00
|
|
|
|
|
|
|
@xonsh_builtins.events.vox_on_deactivate
|
2017-01-14 19:06:49 -05:00
|
|
|
def deactivate(name, **_):
|
2016-08-28 13:37:23 -04:00
|
|
|
nonlocal last_event
|
2018-08-30 09:18:49 -05:00
|
|
|
last_event = "deactivate", name
|
2016-08-28 13:37:23 -04:00
|
|
|
|
2016-07-20 14:50:22 -04:00
|
|
|
vox = Vox()
|
2018-08-30 09:18:49 -05:00
|
|
|
vox.create("spam")
|
|
|
|
vox.activate("spam")
|
2018-09-13 14:03:35 -04:00
|
|
|
assert xonsh_builtins.__xonsh__.env["VIRTUAL_ENV"] == vox["spam"].env
|
2018-08-30 09:18:49 -05:00
|
|
|
assert last_event == ("activate", "spam")
|
2016-07-20 14:50:22 -04:00
|
|
|
vox.deactivate()
|
2018-09-13 14:03:35 -04:00
|
|
|
assert "VIRTUAL_ENV" not in xonsh_builtins.__xonsh__.env
|
2018-08-30 09:18:49 -05:00
|
|
|
assert last_event == ("deactivate", "spam")
|
2016-07-30 03:35:39 -04:00
|
|
|
|
|
|
|
|
2019-08-04 12:20:04 +10:00
|
|
|
@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
|
2019-08-15 23:23:09 +10:00
|
|
|
def activate(name, path, **_):
|
2019-08-04 12:20:04 +10:00
|
|
|
nonlocal last_event
|
2019-08-15 23:23:09 +10:00
|
|
|
last_event = "activate", name, path
|
2019-08-04 12:20:04 +10:00
|
|
|
|
|
|
|
@xonsh_builtins.events.vox_on_deactivate
|
2019-08-15 23:23:09 +10:00
|
|
|
def deactivate(name, path, **_):
|
2019-08-04 12:20:04 +10:00
|
|
|
nonlocal last_event
|
2019-08-15 23:23:09 +10:00
|
|
|
last_event = "deactivate", name, path
|
2019-08-04 12:20:04 +10:00
|
|
|
|
|
|
|
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
|
2019-08-16 20:33:51 +10:00
|
|
|
assert last_event == (
|
|
|
|
"activate",
|
|
|
|
venv_dirname,
|
|
|
|
str(pathlib.Path(str(tmpdir)) / 'venv')
|
|
|
|
)
|
2019-08-04 12:20:04 +10:00
|
|
|
|
|
|
|
vox.deactivate()
|
|
|
|
assert not env["PATH"]
|
|
|
|
assert "VIRTUAL_ENV" not in env
|
2019-08-16 20:33:51 +10:00
|
|
|
assert last_event == (
|
|
|
|
"deactivate",
|
|
|
|
tmpdir.join(venv_dirname),
|
|
|
|
str(pathlib.Path(str(tmpdir)) / 'venv')
|
|
|
|
)
|
2019-08-04 12:20:04 +10:00
|
|
|
|
|
|
|
|
2017-12-31 17:36:21 +01:00
|
|
|
@skip_if_on_msys
|
2016-07-30 03:35:39 -04:00
|
|
|
@skip_if_on_conda
|
|
|
|
def test_path(xonsh_builtins, tmpdir):
|
|
|
|
"""
|
|
|
|
Test to make sure Vox properly activates and deactivates by examining $PATH
|
|
|
|
"""
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env["VIRTUALENV_HOME"] = str(tmpdir)
|
2016-07-30 03:35:39 -04:00
|
|
|
# I consider the case that the user doesn't have a PATH set to be unreasonable
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env.setdefault("PATH", [])
|
2016-07-30 03:35:39 -04:00
|
|
|
|
2018-09-13 14:03:35 -04:00
|
|
|
oldpath = list(xonsh_builtins.__xonsh__.env["PATH"])
|
2016-07-30 03:35:39 -04:00
|
|
|
vox = Vox()
|
2018-08-30 09:18:49 -05:00
|
|
|
vox.create("eggs")
|
|
|
|
|
|
|
|
vox.activate("eggs")
|
|
|
|
|
2018-09-13 14:03:35 -04:00
|
|
|
assert oldpath != xonsh_builtins.__xonsh__.env["PATH"]
|
2016-07-30 03:35:39 -04:00
|
|
|
|
|
|
|
vox.deactivate()
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2018-09-13 14:03:35 -04:00
|
|
|
assert oldpath == xonsh_builtins.__xonsh__.env["PATH"]
|
2016-09-13 20:49:34 -04:00
|
|
|
|
2016-10-02 21:49:38 -04:00
|
|
|
|
2017-12-31 17:36:21 +01:00
|
|
|
@skip_if_on_msys
|
2016-09-13 20:49:34 -04:00
|
|
|
@skip_if_on_conda
|
|
|
|
def test_crud_subdir(xonsh_builtins, tmpdir):
|
|
|
|
"""
|
|
|
|
Creates a virtual environment, gets it, enumerates it, and then deletes it.
|
|
|
|
"""
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env["VIRTUALENV_HOME"] = str(tmpdir)
|
2016-09-13 20:49:34 -04:00
|
|
|
|
|
|
|
vox = Vox()
|
2018-08-30 09:18:49 -05:00
|
|
|
vox.create("spam/eggs")
|
|
|
|
assert stat.S_ISDIR(tmpdir.join("spam", "eggs").stat().mode)
|
2016-09-13 20:49:34 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
ve = vox["spam/eggs"]
|
|
|
|
assert ve.env == str(tmpdir.join("spam", "eggs"))
|
2016-09-13 20:49:34 -04:00
|
|
|
assert os.path.isdir(ve.bin)
|
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
assert "spam/eggs" in vox
|
|
|
|
assert "spam" not in vox
|
2016-09-13 20:49:34 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
# assert 'spam/eggs' in list(vox) # This is NOT true on Windows
|
|
|
|
assert "spam" not in list(vox)
|
2016-09-13 20:49:34 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
del vox["spam/eggs"]
|
|
|
|
|
|
|
|
assert not tmpdir.join("spam", "eggs").check()
|
2016-09-13 20:49:34 -04:00
|
|
|
|
2016-10-02 21:49:38 -04:00
|
|
|
|
2016-12-09 23:14:41 -05:00
|
|
|
try:
|
|
|
|
import pathlib
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2017-12-31 17:36:21 +01:00
|
|
|
@skip_if_on_msys
|
2016-12-09 23:14:41 -05:00
|
|
|
@skip_if_on_conda
|
|
|
|
def test_crud_path(xonsh_builtins, tmpdir):
|
|
|
|
"""
|
|
|
|
Creates a virtual environment, gets it, enumerates it, and then deletes it.
|
|
|
|
"""
|
|
|
|
tmp = pathlib.Path(str(tmpdir))
|
|
|
|
|
|
|
|
vox = Vox()
|
|
|
|
vox.create(tmp)
|
2018-08-30 09:18:49 -05:00
|
|
|
assert stat.S_ISDIR(tmpdir.join("lib").stat().mode)
|
2016-12-09 23:14:41 -05:00
|
|
|
|
|
|
|
ve = vox[tmp]
|
|
|
|
assert ve.env == str(tmp)
|
|
|
|
assert os.path.isdir(ve.bin)
|
|
|
|
|
|
|
|
del vox[tmp]
|
|
|
|
|
|
|
|
assert not tmpdir.check()
|
|
|
|
|
2016-10-02 21:49:38 -04:00
|
|
|
|
2017-12-31 17:36:21 +01:00
|
|
|
@skip_if_on_msys
|
2016-10-02 21:49:38 -04:00
|
|
|
@skip_if_on_conda
|
2019-08-28 14:07:27 -04:00
|
|
|
def test_reserved_names(xonsh_builtins, tmpdir):
|
2016-10-02 21:49:38 -04:00
|
|
|
"""
|
2019-08-28 14:07:27 -04:00
|
|
|
Tests that reserved words are disallowed.
|
2016-10-02 21:49:38 -04:00
|
|
|
"""
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env["VIRTUALENV_HOME"] = str(tmpdir)
|
2016-10-02 21:49:38 -04:00
|
|
|
|
|
|
|
vox = Vox()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
if ON_WINDOWS:
|
2018-08-30 09:18:49 -05:00
|
|
|
vox.create("Scripts")
|
2016-10-02 21:49:38 -04:00
|
|
|
else:
|
2018-08-30 09:18:49 -05:00
|
|
|
vox.create("bin")
|
2016-10-02 21:49:38 -04:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
if ON_WINDOWS:
|
2018-08-30 09:18:49 -05:00
|
|
|
vox.create("spameggs/Scripts")
|
2016-10-02 21:49:38 -04:00
|
|
|
else:
|
2018-08-30 09:18:49 -05:00
|
|
|
vox.create("spameggs/bin")
|
2019-08-28 14:50:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
@skip_if_on_msys
|
|
|
|
@skip_if_on_conda
|
|
|
|
def test_autovox(xonsh_builtins, tmpdir):
|
|
|
|
"""
|
|
|
|
Tests that autovox works
|
|
|
|
"""
|
|
|
|
import importlib
|
|
|
|
import xonsh.dirstack
|
|
|
|
|
|
|
|
# Set up an isolated venv home
|
|
|
|
xonsh_builtins.__xonsh__.env["VIRTUALENV_HOME"] = str(tmpdir)
|
|
|
|
|
|
|
|
# Makes sure that event handlers are registered
|
|
|
|
import xontrib.autovox
|
|
|
|
importlib.reload(xontrib.autovox)
|
|
|
|
|
|
|
|
# Set up enough environment for xonsh to function
|
|
|
|
xonsh_builtins.__xonsh__.env['PWD'] = os.getcwd()
|
|
|
|
xonsh_builtins.__xonsh__.env['DIRSTACK_SIZE'] = 10
|
|
|
|
xonsh_builtins.__xonsh__.env['PATH'] = []
|
|
|
|
|
|
|
|
xonsh_builtins.__xonsh__.env['XONSH_SHOW_TRACEBACK'] = True
|
|
|
|
|
|
|
|
@xonsh_builtins.events.autovox_policy
|
|
|
|
def policy(path, **_):
|
|
|
|
print("Checking", repr(path), vox.active())
|
|
|
|
if str(path) == str(tmpdir):
|
|
|
|
return "myenv"
|
|
|
|
|
|
|
|
vox = Vox()
|
|
|
|
|
|
|
|
print(xonsh_builtins.__xonsh__.env['PWD'])
|
|
|
|
xonsh.dirstack.pushd([str(tmpdir)])
|
|
|
|
print(xonsh_builtins.__xonsh__.env['PWD'])
|
|
|
|
assert vox.active() is None
|
|
|
|
xonsh.dirstack.popd([])
|
|
|
|
print(xonsh_builtins.__xonsh__.env['PWD'])
|
|
|
|
|
|
|
|
vox.create('myenv')
|
|
|
|
xonsh.dirstack.pushd([str(tmpdir)])
|
|
|
|
print(xonsh_builtins.__xonsh__.env['PWD'])
|
|
|
|
assert vox.active() == 'myenv'
|
|
|
|
xonsh.dirstack.popd([])
|
|
|
|
print(xonsh_builtins.__xonsh__.env['PWD'])
|