xonsh/tests/test_vox.py

276 lines
7.1 KiB
Python
Raw Normal View History

"""Vox tests"""
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
from xonsh.platform import ON_WINDOWS
2016-07-23 11:21:41 -04:00
@skip_if_on_msys
@skip_if_on_conda
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
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")
2018-08-30 09:18:49 -05:00
ve = vox["spam"]
assert ve.env == str(tmpdir.join("spam"))
assert os.path.isdir(ve.bin)
2018-08-30 09:18:49 -05:00
assert "spam" in vox
assert "spam" in list(vox)
2018-08-30 09:18:49 -05:00
del vox["spam"]
2018-08-30 09:18:49 -05:00
assert not tmpdir.join("spam").check()
assert last_event == ("delete", "spam")
@skip_if_on_msys
@skip_if_on_conda
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)
# 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
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")
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")
@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, path, **_):
nonlocal last_event
last_event = "activate", name, path
@xonsh_builtins.events.vox_on_deactivate
def deactivate(name, path, **_):
nonlocal last_event
last_event = "deactivate", name, path
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,
str(pathlib.Path(str(tmpdir)) / 'venv')
)
vox.deactivate()
assert not env["PATH"]
assert "VIRTUAL_ENV" not in env
assert last_event == (
"deactivate",
tmpdir.join(venv_dirname),
str(pathlib.Path(str(tmpdir)) / 'venv')
)
@skip_if_on_msys
@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)
# 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", [])
2018-09-13 14:03:35 -04:00
oldpath = list(xonsh_builtins.__xonsh__.env["PATH"])
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"]
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"]
@skip_if_on_msys
@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)
vox = Vox()
2018-08-30 09:18:49 -05:00
vox.create("spam/eggs")
assert stat.S_ISDIR(tmpdir.join("spam", "eggs").stat().mode)
2018-08-30 09:18:49 -05:00
ve = vox["spam/eggs"]
assert ve.env == str(tmpdir.join("spam", "eggs"))
assert os.path.isdir(ve.bin)
2018-08-30 09:18:49 -05:00
assert "spam/eggs" in vox
assert "spam" not in vox
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)
2018-08-30 09:18:49 -05:00
del vox["spam/eggs"]
assert not tmpdir.join("spam", "eggs").check()
2016-12-09 23:14:41 -05:00
try:
import pathlib
except ImportError:
pass
else:
2018-08-30 09:18:49 -05: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()
@skip_if_on_msys
@skip_if_on_conda
2019-08-28 14:07:27 -04:00
def test_reserved_names(xonsh_builtins, tmpdir):
"""
2019-08-28 14:07:27 -04:00
Tests that reserved words are disallowed.
"""
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env["VIRTUALENV_HOME"] = str(tmpdir)
vox = Vox()
with pytest.raises(ValueError):
if ON_WINDOWS:
2018-08-30 09:18:49 -05:00
vox.create("Scripts")
else:
2018-08-30 09:18:49 -05:00
vox.create("bin")
with pytest.raises(ValueError):
if ON_WINDOWS:
2018-08-30 09:18:49 -05:00
vox.create("spameggs/Scripts")
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'])