xonsh/tests/test_dirstack.py

119 lines
3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-05-28 02:48:11 +02:00
"""Testing dirstack"""
from __future__ import unicode_literals, print_function
from contextlib import contextmanager
from functools import wraps
import os
import builtins
2016-06-22 22:57:50 +03:00
import pytest
2015-05-28 02:48:11 +02:00
from xonsh import dirstack
from xonsh.environ import Env
from xonsh.built_ins import load_builtins
HERE = os.path.abspath(os.path.dirname(__file__))
PARENT = os.path.dirname(HERE)
2018-08-30 09:18:49 -05:00
2015-05-28 02:48:11 +02:00
@contextmanager
def chdir(adir):
old_dir = os.getcwd()
os.chdir(adir)
yield
os.chdir(old_dir)
2016-06-26 02:05:26 +03:00
def test_simple(xonsh_builtins):
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env = Env(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(xonsh_builtins):
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env = Env(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
2016-06-26 02:05:26 +03:00
def test_cdpath_collision(xonsh_builtins):
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env = Env(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
2016-06-26 02:05:26 +03:00
def test_cdpath_expansion(xonsh_builtins):
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env = Env(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)
), "dirstack._try_cdpath: could not resolve {0}".format(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(xonsh_builtins, tmpdir):
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env = Env(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
@xonsh_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:
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(xonsh_builtins, tmpdir):
2018-09-13 14:03:35 -04:00
xonsh_builtins.__xonsh__.env = Env(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:
raise
finally:
while len(dirstack.DIRSTACK) > old_ds_size:
dirstack.popd([])
assert old_dir == os.getcwd()