mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
Add events to vox.
This commit is contained in:
parent
9b73b4343d
commit
0d791d1bea
2 changed files with 65 additions and 0 deletions
|
@ -14,9 +14,23 @@ def test_crud(xonsh_builtins, tmpdir):
|
|||
Creates a virtual environment, gets it, enumerates it, and then deletes it.
|
||||
"""
|
||||
xonsh_builtins.__xonsh_env__['VIRTUALENV_HOME'] = str(tmpdir)
|
||||
|
||||
last_event = None
|
||||
|
||||
@xonsh_builtins.events.vox_on_create
|
||||
def create(name):
|
||||
nonlocal last_event
|
||||
last_event = 'create', name
|
||||
|
||||
@xonsh_builtins.events.vox_on_delete
|
||||
def delete(name):
|
||||
nonlocal last_event
|
||||
last_event = 'delete', name
|
||||
|
||||
vox = Vox()
|
||||
vox.create('spam')
|
||||
assert stat.S_ISDIR(tmpdir.join('spam').stat().mode)
|
||||
assert last_event == ('create', 'spam')
|
||||
|
||||
env, bin = vox['spam']
|
||||
assert env == str(tmpdir.join('spam'))
|
||||
|
@ -27,6 +41,7 @@ def test_crud(xonsh_builtins, tmpdir):
|
|||
del vox['spam']
|
||||
|
||||
assert not tmpdir.join('spam').check()
|
||||
assert last_event == ('delete', 'spam')
|
||||
|
||||
|
||||
@skip_if_on_conda
|
||||
|
@ -37,12 +52,27 @@ def test_activate(xonsh_builtins, tmpdir):
|
|||
xonsh_builtins.__xonsh_env__['VIRTUALENV_HOME'] = str(tmpdir)
|
||||
# I consider the case that the user doesn't have a PATH set to be unreasonable
|
||||
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
|
||||
|
||||
vox = Vox()
|
||||
vox.create('spam')
|
||||
vox.activate('spam')
|
||||
assert xonsh_builtins.__xonsh_env__['VIRTUAL_ENV'] == vox['spam'].env
|
||||
assert last_event == ('activate', 'spam')
|
||||
vox.deactivate()
|
||||
assert 'VIRTUAL_ENV' not in xonsh_builtins.__xonsh_env__
|
||||
assert last_event == ('deactivate', 'spam')
|
||||
|
||||
|
||||
@skip_if_on_conda
|
||||
|
|
|
@ -7,6 +7,35 @@ import collections.abc
|
|||
|
||||
from xonsh.platform import ON_POSIX, ON_WINDOWS, scandir
|
||||
|
||||
# This is because builtins aren't globally created during testing.
|
||||
# FIXME: Is there a better way?
|
||||
from xonsh.events import events
|
||||
|
||||
|
||||
events.doc('vox_on_create', """
|
||||
vox_on_create(env: str) -> None
|
||||
|
||||
Fired after an environment is created.
|
||||
""")
|
||||
|
||||
events.doc('vox_on_activate', """
|
||||
vox_on_activate(env: str) -> None
|
||||
|
||||
Fired after an environment is activated.
|
||||
""")
|
||||
|
||||
events.doc('vox_on_deactivate', """
|
||||
vox_on_deactivate(env: str) -> None
|
||||
|
||||
Fired after an environment is deactivated.
|
||||
""")
|
||||
|
||||
events.doc('vox_on_delete', """
|
||||
vox_on_delete(env: str) -> None
|
||||
|
||||
Fired after an environment is deleted (through vox).
|
||||
""")
|
||||
|
||||
|
||||
VirtualEnvironment = collections.namedtuple('VirtualEnvironment', ['env', 'bin'])
|
||||
|
||||
|
@ -60,6 +89,7 @@ class Vox(collections.abc.Mapping):
|
|||
env_path,
|
||||
system_site_packages=system_site_packages, symlinks=symlinks,
|
||||
with_pip=with_pip)
|
||||
events.vox_on_create.fire(name)
|
||||
|
||||
def upgrade(self, name, *, symlinks=False, with_pip=True):
|
||||
"""Create a virtual environment in $VIRTUALENV_HOME with python3's ``venv``.
|
||||
|
@ -185,6 +215,8 @@ class Vox(collections.abc.Mapping):
|
|||
if 'PYTHONHOME' in env:
|
||||
type(self).oldvars['PYTHONHOME'] = env.pop('PYTHONHOME')
|
||||
|
||||
events.vox_on_activate.fire(name)
|
||||
|
||||
def deactivate(self):
|
||||
"""
|
||||
Deactive the active virtual environment. Returns the name of it.
|
||||
|
@ -203,6 +235,7 @@ class Vox(collections.abc.Mapping):
|
|||
|
||||
env.pop('VIRTUAL_ENV')
|
||||
|
||||
events.vox_on_deactivate.fire(env_name)
|
||||
return env_name
|
||||
|
||||
def __delitem__(self, name):
|
||||
|
@ -222,3 +255,5 @@ class Vox(collections.abc.Mapping):
|
|||
# No current venv, ... fails
|
||||
pass
|
||||
shutil.rmtree(env_path)
|
||||
|
||||
events.vox_on_delete.fire(name)
|
||||
|
|
Loading…
Add table
Reference in a new issue