2015-05-28 02:48:11 +02:00
|
|
|
"""Testing dirstack"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2020-05-05 06:42:28 -04:00
|
|
|
import pytest # noqa F401
|
2015-05-28 02:48:11 +02:00
|
|
|
|
|
|
|
from xonsh import dirstack
|
2023-03-02 13:01:08 +06:00
|
|
|
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
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_simple(xession):
|
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
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_cdpath_simple(xession):
|
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
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_cdpath_collision(xession):
|
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):
|
2016-09-05 20:36:45 -04: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() == os.path.join(HERE, "tests")
|
2015-05-28 02:48:11 +02:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_cdpath_expansion(xession):
|
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:
|
2016-09-21 14:45:58 +03:00
|
|
|
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)
|
2021-12-07 01:12:26 +05:30
|
|
|
), f"dirstack._try_cdpath: could not resolve {d}"
|
2016-09-21 14:45:58 +03:00
|
|
|
finally:
|
|
|
|
for d in test_dirs:
|
|
|
|
if os.path.exists(d):
|
|
|
|
os.rmdir(d)
|
2016-08-04 20:52:41 -04:00
|
|
|
|
2016-09-05 20:36:45 -04:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_cdpath_events(xession, tmpdir):
|
2022-01-08 04:03:22 +05:30
|
|
|
xession.env.update(dict(CDPATH=PARENT, PWD=os.getcwd()))
|
2016-08-27 12:06:16 -04:00
|
|
|
target = str(tmpdir)
|
2016-08-04 20:52:41 -04:00
|
|
|
|
2016-08-27 12:06:16 -04:00
|
|
|
ev = None
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
@xession.builtins.events.on_chdir
|
2017-01-14 18:40:29 -05:00
|
|
|
def handler(olddir, newdir, **kw):
|
2016-08-27 12:06:16 -04:00
|
|
|
nonlocal ev
|
2017-01-14 18:40:29 -05:00
|
|
|
ev = olddir, newdir
|
2016-08-27 12:06:16 -04:00
|
|
|
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
try:
|
|
|
|
dirstack.cd([target])
|
2020-05-05 06:42:28 -04:00
|
|
|
except Exception:
|
2016-08-27 12:06:16 -04:00
|
|
|
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)
|
2016-09-05 20:36:45 -04:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_cd_autopush(xession, tmpdir):
|
2022-01-08 04:03:22 +05:30
|
|
|
xession.env.update(dict(CDPATH=PARENT, PWD=os.getcwd(), AUTO_PUSHD=True))
|
2016-09-05 20:36:45 -04:00
|
|
|
target = str(tmpdir)
|
|
|
|
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
old_ds_size = len(dirstack.DIRSTACK)
|
|
|
|
|
|
|
|
assert target != old_dir
|
|
|
|
|
2016-09-07 21:37:33 -04:00
|
|
|
try:
|
|
|
|
dirstack.cd([target])
|
|
|
|
assert target == os.getcwd()
|
2016-09-05 20:36:45 -04:00
|
|
|
assert old_ds_size + 1 == len(dirstack.DIRSTACK)
|
2016-09-07 21:37:33 -04:00
|
|
|
dirstack.popd([])
|
2020-05-05 06:42:28 -04:00
|
|
|
except Exception:
|
2016-09-07 21:37:33 -04:00
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
while len(dirstack.DIRSTACK) > old_ds_size:
|
|
|
|
dirstack.popd([])
|
2016-09-05 20:36:45 -04:00
|
|
|
|
2016-09-07 21:37:33 -04:00
|
|
|
assert old_dir == os.getcwd()
|