xonsh/tests/test_dirstack.py

117 lines
2.9 KiB
Python
Raw Normal View History

2015-05-28 02:48:11 +02:00
"""Testing dirstack"""
import os
import pytest # noqa F401
2015-05-28 02:48:11 +02:00
from xonsh import dirstack
from xonsh.tools import chdir
2015-05-28 02:48:11 +02:00
HERE = os.path.abspath(os.path.dirname(__file__))
PARENT = os.path.dirname(HERE)
2018-08-30 09:18:49 -05:00
def test_simple(xession):
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
xession.env.update(dict(CDPATH=PARENT, PWD=PARENT))
2016-06-26 02:05:26 +03:00
with chdir(PARENT):
2018-08-30 09:18:49 -05:00
assert os.getcwd() != HERE
2016-06-26 02:05:26 +03:00
dirstack.cd(["tests"])
2018-08-30 09:18:49 -05:00
assert os.getcwd() == HERE
2016-06-26 02:05:26 +03:00
def test_cdpath_simple(xession):
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
xession.env.update(dict(CDPATH=PARENT, PWD=HERE))
2016-06-26 02:05:26 +03:00
with chdir(os.path.normpath("/")):
2018-08-30 09:18:49 -05:00
assert os.getcwd() != HERE
2016-06-26 02:05:26 +03:00
dirstack.cd(["tests"])
2018-08-30 09:18:49 -05:00
assert os.getcwd() == HERE
2016-06-26 02:05:26 +03:00
2015-05-28 02:48:11 +02:00
def test_cdpath_collision(xession):
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
xession.env.update(dict(CDPATH=PARENT, PWD=HERE))
2016-06-26 02:05:26 +03:00
sub_tests = os.path.join(HERE, "tests")
if not os.path.exists(sub_tests):
os.mkdir(sub_tests)
with chdir(HERE):
assert os.getcwd() == HERE
2016-06-26 02:05:26 +03:00
dirstack.cd(["tests"])
2018-08-30 09:18:49 -05:00
assert os.getcwd() == os.path.join(HERE, "tests")
2015-05-28 02:48:11 +02:00
def test_cdpath_expansion(xession):
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
xession.env.update(dict(HERE=HERE, CDPATH=("~", "$HERE")))
2016-06-26 02:05:26 +03:00
test_dirs = (
os.path.join(HERE, "xonsh-test-cdpath-here"),
2018-08-30 09:18:49 -05:00
os.path.expanduser("~/xonsh-test-cdpath-home"),
2016-06-26 02:05:26 +03:00
)
try:
for d in test_dirs:
if not os.path.exists(d):
os.mkdir(d)
2018-08-30 09:18:49 -05:00
assert os.path.exists(
dirstack._try_cdpath(d)
), f"dirstack._try_cdpath: could not resolve {d}"
finally:
for d in test_dirs:
if os.path.exists(d):
os.rmdir(d)
2016-08-04 20:52:41 -04:00
def test_cdpath_events(xession, tmpdir):
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
xession.env.update(dict(CDPATH=PARENT, PWD=os.getcwd()))
target = str(tmpdir)
2016-08-04 20:52:41 -04:00
ev = None
2018-08-30 09:18:49 -05:00
@xession.builtins.events.on_chdir
2017-01-14 18:40:29 -05:00
def handler(olddir, newdir, **kw):
nonlocal ev
2017-01-14 18:40:29 -05:00
ev = olddir, newdir
old_dir = os.getcwd()
try:
dirstack.cd([target])
except Exception:
raise
else:
assert (old_dir, target) == ev
finally:
# Use os.chdir() here so dirstack.cd() doesn't fire events (or fail again)
os.chdir(old_dir)
def test_cd_autopush(xession, tmpdir):
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +05:30
xession.env.update(dict(CDPATH=PARENT, PWD=os.getcwd(), AUTO_PUSHD=True))
target = str(tmpdir)
old_dir = os.getcwd()
old_ds_size = len(dirstack.DIRSTACK)
assert target != old_dir
try:
dirstack.cd([target])
assert target == os.getcwd()
assert old_ds_size + 1 == len(dirstack.DIRSTACK)
dirstack.popd([])
except Exception:
raise
finally:
while len(dirstack.DIRSTACK) > old_ds_size:
dirstack.popd([])
assert old_dir == os.getcwd()
def test_cd_home(xession, tmpdir):
target = str(tmpdir)
old_home = xession.env.get("HOME")
xession.env.update(dict(HOME=target, PWD=os.getcwd(), AUTO_PUSHD=True))
dirstack.cd([])
assert target == os.getcwd()
dirstack.popd([])
xession.env.update(dict(HOME=old_home))