Skip to content

Commit

Permalink
many test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
scopatz committed Sep 13, 2018
1 parent 6aa7aba commit 4c91df2
Show file tree
Hide file tree
Showing 30 changed files with 186 additions and 157 deletions.
47 changes: 25 additions & 22 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from xonsh.built_ins import ensure_list_of_strs, enter_macro
from xonsh.built_ins import ensure_list_of_strs, enter_macro, XonshSession
from xonsh.execer import Execer
from xonsh.jobs import tasks
from xonsh.events import events
Expand All @@ -27,8 +27,10 @@ def xonsh_execer(monkeypatch):
"xonsh.built_ins.load_builtins.__code__",
(lambda *args, **kwargs: None).__code__,
)
if not hasattr(builtins, "__xonsh__"):
builtins.__xonsh__ = XonshSession()
execer = Execer(unload=False)
builtins.__xonsh_execer__ = execer
builtins.__xonsh__.execer = execer
return execer


Expand All @@ -46,27 +48,28 @@ def xonsh_events():
def xonsh_builtins(xonsh_events):
"""Mock out most of the builtins xonsh attributes."""
old_builtins = set(dir(builtins))
builtins.__xonsh_env__ = DummyEnv()
execer = getattr(getattr(builtins, "__xonsh__", None), "execer", None)
builtins.__xonsh__ = XonshSession(execer=execer, ctx={})
builtins.__xonsh__.env = DummyEnv()
if ON_WINDOWS:
builtins.__xonsh_env__["PATHEXT"] = [".EXE", ".BAT", ".CMD"]
builtins.__xonsh_ctx__ = {}
builtins.__xonsh_shell__ = DummyShell()
builtins.__xonsh_help__ = lambda x: x
builtins.__xonsh_glob__ = glob.glob
builtins.__xonsh_exit__ = False
builtins.__xonsh_superhelp__ = lambda x: x
builtins.__xonsh_regexpath__ = lambda x: []
builtins.__xonsh_expand_path__ = lambda x: x
builtins.__xonsh_subproc_captured__ = sp
builtins.__xonsh_subproc_uncaptured__ = sp
builtins.__xonsh_stdout_uncaptured__ = None
builtins.__xonsh_stderr_uncaptured__ = None
builtins.__xonsh_ensure_list_of_strs__ = ensure_list_of_strs
builtins.__xonsh_commands_cache__ = DummyCommandsCache()
builtins.__xonsh_all_jobs__ = {}
builtins.__xonsh_history__ = DummyHistory()
builtins.__xonsh_subproc_captured_hiddenobject__ = sp
builtins.__xonsh_enter_macro__ = enter_macro
builtins.__xonsh__.env["PATHEXT"] = [".EXE", ".BAT", ".CMD"]
builtins.__xonsh__.shell = DummyShell()
builtins.__xonsh__.help = lambda x: x
builtins.__xonsh__.glob = glob.glob
builtins.__xonsh__.exit = False
builtins.__xonsh__.superhelp = lambda x: x
builtins.__xonsh__.regexpath = lambda x: []
builtins.__xonsh__.expand_path = lambda x: x
builtins.__xonsh__.subproc_captured = sp
builtins.__xonsh__.subproc_uncaptured = sp
builtins.__xonsh__.stdout_uncaptured = None
builtins.__xonsh__.stderr_uncaptured = None
builtins.__xonsh__.ensure_list_of_strs = ensure_list_of_strs
builtins.__xonsh__.commands_cache = DummyCommandsCache()
builtins.__xonsh__.all_jobs = {}
builtins.__xonsh__.history = DummyHistory()
builtins.__xonsh__.subproc_captured_hiddenobject = sp
builtins.__xonsh__.enter_macro = enter_macro
builtins.evalx = eval
builtins.execx = None
builtins.compilex = None
Expand Down
2 changes: 1 addition & 1 deletion tests/test_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ def test_eval_recursive(xonsh_builtins):

@skip_if_on_windows
def test_eval_recursive_callable_partial(xonsh_builtins):
xonsh_builtins.__xonsh_env__ = Env(HOME=os.path.expanduser("~"))
xonsh_builtins.__xonsh__.env = Env(HOME=os.path.expanduser("~"))
assert ALIASES.get("indirect_cd")(["arg2", "arg3"]) == ["..", "arg2", "arg3"]
8 changes: 4 additions & 4 deletions tests/test_base_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def test_pwd_tracks_cwd(xonsh_builtins, xonsh_execer, tmpdir_factory, monkeypatch):
asubdir = str(tmpdir_factory.mktemp("asubdir"))
cur_wd = os.getcwd()
xonsh_builtins.__xonsh_env__ = Env(
xonsh_builtins.__xonsh__.env = Env(
PWD=cur_wd, XONSH_CACHE_SCRIPTS=False, XONSH_CACHE_EVERYTHING=False
)

Expand All @@ -23,11 +23,11 @@ def test_pwd_tracks_cwd(xonsh_builtins, xonsh_execer, tmpdir_factory, monkeypatc

assert os.path.abspath(os.getcwd()) == os.path.abspath(asubdir)
assert os.path.abspath(os.getcwd()) == os.path.abspath(
xonsh_builtins.__xonsh_env__["PWD"]
xonsh_builtins.__xonsh__.env["PWD"]
)
assert "OLDPWD" in xonsh_builtins.__xonsh_env__
assert "OLDPWD" in xonsh_builtins.__xonsh__.env
assert os.path.abspath(cur_wd) == os.path.abspath(
xonsh_builtins.__xonsh_env__["OLDPWD"]
xonsh_builtins.__xonsh__.env["OLDPWD"]
)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_bashisms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def test_preproc(inp, exp, xonsh_builtins):
"""Test the bash preprocessor."""
from xontrib.bashisms import bash_preproc

xonsh_builtins.__xonsh_history__.inps = ["ls\n"]
xonsh_builtins.__xonsh__.history.inps = ["ls\n"]
obs = bash_preproc(inp)
assert exp == obs
4 changes: 2 additions & 2 deletions tests/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def test_reglob_tests(testfile):

@pytest.fixture
def home_env(xonsh_builtins):
"""Set `__xonsh_env__ ` to a new Env instance on `xonsh_builtins`"""
xonsh_builtins.__xonsh_env__ = Env(HOME=HOME_PATH)
"""Set `__xonsh__.env ` to a new Env instance on `xonsh_builtins`"""
xonsh_builtins.__xonsh__.env = Env(HOME=HOME_PATH)
return xonsh_builtins


Expand Down
4 changes: 2 additions & 2 deletions tests/test_commands_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ def test_non_exist_is_only_functional_alias(xonsh_builtins):

@skip_if_on_windows
def test_bash_is_only_functional_alias(xonsh_builtins):
builtins.__xonsh_env__["PATH"] = os.environ["PATH"].split(os.pathsep)
builtins.__xonsh__.env["PATH"] = os.environ["PATH"].split(os.pathsep)
cc = CommandsCache()
assert not cc.is_only_functional_alias("bash")


@skip_if_on_windows
def test_bash_and_is_alias_is_only_functional_alias(xonsh_builtins):
builtins.__xonsh_env__["PATH"] = os.environ["PATH"].split(os.pathsep)
builtins.__xonsh__.env["PATH"] = os.environ["PATH"].split(os.pathsep)
cc = CommandsCache()
builtins.aliases["bash"] = lambda args: os.chdir(args[0])
assert not cc.is_only_functional_alias("bash")
12 changes: 6 additions & 6 deletions tests/test_dirstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ def chdir(adir):


def test_simple(xonsh_builtins):
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=PARENT)
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=PARENT)
with chdir(PARENT):
assert os.getcwd() != HERE
dirstack.cd(["tests"])
assert os.getcwd() == HERE


def test_cdpath_simple(xonsh_builtins):
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=HERE)
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=HERE)
with chdir(os.path.normpath("/")):
assert os.getcwd() != HERE
dirstack.cd(["tests"])
assert os.getcwd() == HERE


def test_cdpath_collision(xonsh_builtins):
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=HERE)
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=HERE)
sub_tests = os.path.join(HERE, "tests")
if not os.path.exists(sub_tests):
os.mkdir(sub_tests)
Expand All @@ -54,7 +54,7 @@ def test_cdpath_collision(xonsh_builtins):


def test_cdpath_expansion(xonsh_builtins):
xonsh_builtins.__xonsh_env__ = Env(HERE=HERE, CDPATH=("~", "$HERE"))
xonsh_builtins.__xonsh__.env = Env(HERE=HERE, CDPATH=("~", "$HERE"))
test_dirs = (
os.path.join(HERE, "xonsh-test-cdpath-here"),
os.path.expanduser("~/xonsh-test-cdpath-home"),
Expand All @@ -73,7 +73,7 @@ def test_cdpath_expansion(xonsh_builtins):


def test_cdpath_events(xonsh_builtins, tmpdir):
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=os.getcwd())
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=os.getcwd())
target = str(tmpdir)

ev = None
Expand All @@ -96,7 +96,7 @@ def handler(olddir, newdir, **kw):


def test_cd_autopush(xonsh_builtins, tmpdir):
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=os.getcwd(), AUTO_PUSHD=True)
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=os.getcwd(), AUTO_PUSHD=True)
target = str(tmpdir)

old_dir = os.getcwd()
Expand Down
26 changes: 13 additions & 13 deletions tests/test_dirstack_unc.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def shares_setup(tmpdir_factory):
def test_pushdpopd(xonsh_builtins):
"""Simple non-UNC push/pop to verify we didn't break nonUNC case.
"""
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=HERE)
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=HERE)

dirstack.cd([PARENT])
owd = os.getcwd()
assert owd.casefold() == xonsh_builtins.__xonsh_env__["PWD"].casefold()
assert owd.casefold() == xonsh_builtins.__xonsh__.env["PWD"].casefold()
dirstack.pushd([HERE])
wd = os.getcwd()
assert wd.casefold() == HERE.casefold()
Expand All @@ -107,7 +107,7 @@ def test_pushdpopd(xonsh_builtins):


def test_cd_dot(xonsh_builtins):
xonsh_builtins.__xonsh_env__ = Env(PWD=os.getcwd())
xonsh_builtins.__xonsh__.env = Env(PWD=os.getcwd())

owd = os.getcwd().casefold()
dirstack.cd(["."])
Expand All @@ -118,10 +118,10 @@ def test_cd_dot(xonsh_builtins):
def test_uncpushd_simple_push_pop(xonsh_builtins, shares_setup):
if shares_setup is None:
return
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=HERE)
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=HERE)
dirstack.cd([PARENT])
owd = os.getcwd()
assert owd.casefold() == xonsh_builtins.__xonsh_env__["PWD"].casefold()
assert owd.casefold() == xonsh_builtins.__xonsh__.env["PWD"].casefold()
dirstack.pushd([r"\\localhost\uncpushd_test_HERE"])
wd = os.getcwd()
assert os.path.splitdrive(wd)[0].casefold() == TEMP_DRIVE[0]
Expand All @@ -135,11 +135,11 @@ def test_uncpushd_simple_push_pop(xonsh_builtins, shares_setup):
def test_uncpushd_push_to_same_share(xonsh_builtins, shares_setup):
if shares_setup is None:
return
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=HERE)
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=HERE)

dirstack.cd([PARENT])
owd = os.getcwd()
assert owd.casefold() == xonsh_builtins.__xonsh_env__["PWD"].casefold()
assert owd.casefold() == xonsh_builtins.__xonsh__.env["PWD"].casefold()
dirstack.pushd([r"\\localhost\uncpushd_test_HERE"])
wd = os.getcwd()
assert os.path.splitdrive(wd)[0].casefold() == TEMP_DRIVE[0]
Expand Down Expand Up @@ -169,11 +169,11 @@ def test_uncpushd_push_other_push_same(xonsh_builtins, shares_setup):
Then push to a again. Pop (check b unmapped and a still mapped), pop, pop (check a is unmapped)"""
if shares_setup is None:
return
xonsh_builtins.__xonsh_env__ = Env(CDPATH=PARENT, PWD=HERE)
xonsh_builtins.__xonsh__.env = Env(CDPATH=PARENT, PWD=HERE)

dirstack.cd([PARENT])
owd = os.getcwd()
assert owd.casefold() == xonsh_builtins.__xonsh_env__["PWD"].casefold()
assert owd.casefold() == xonsh_builtins.__xonsh__.env["PWD"].casefold()
dirstack.pushd([r"\\localhost\uncpushd_test_HERE"])
assert os.getcwd().casefold() == TEMP_DRIVE[0] + "\\"
assert len(_unc_tempDrives) == 1
Expand Down Expand Up @@ -285,15 +285,15 @@ def with_unc_check_disabled(): # just like the above, but value is 1 to *disabl

@pytest.fixture()
def xonsh_builtins_cd(xonsh_builtins):
xonsh_builtins.__xonsh_env__["CDPATH"] = PARENT
xonsh_builtins.__xonsh_env__["PWD"] = os.getcwd()
xonsh_builtins.__xonsh_env__["DIRSTACK_SIZE"] = 20
xonsh_builtins.__xonsh__.env["CDPATH"] = PARENT
xonsh_builtins.__xonsh__.env["PWD"] = os.getcwd()
xonsh_builtins.__xonsh__.env["DIRSTACK_SIZE"] = 20
return xonsh_builtins


@pytest.mark.skipif(not ON_WINDOWS, reason="Windows-only UNC functionality")
def test_uncpushd_cd_unc_auto_pushd(xonsh_builtins_cd, with_unc_check_enabled):
xonsh_builtins_cd.__xonsh_env__["AUTO_PUSHD"] = True
xonsh_builtins_cd.__xonsh__.env["AUTO_PUSHD"] = True
so, se, rc = dirstack.cd([r"\\localhost\uncpushd_test_PARENT"])
if rc != 0:
return
Expand Down
14 changes: 7 additions & 7 deletions tests/test_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ def test_locate_binary_on_windows(xonsh_builtins):
fpath = os.path.join(tmpdir, fname)
with open(fpath, "w") as f:
f.write(fpath)
xonsh_builtins.__xonsh_env__.update(
xonsh_builtins.__xonsh__.env.update(
{"PATH": [tmpdir], "PATHEXT": [".COM", ".EXE", ".BAT"]}
)
xonsh_builtins.__xonsh_commands_cache__ = CommandsCache()
xonsh_builtins.__xonsh__.commands_cache = CommandsCache()
assert locate_binary("file1") == os.path.join(tmpdir, "file1.exe")
assert locate_binary("file1.exe") == os.path.join(tmpdir, "file1.exe")
assert locate_binary("file2") == os.path.join(tmpdir, "FILE2.BAT")
Expand All @@ -159,7 +159,7 @@ def test_locate_binary_on_windows(xonsh_builtins):

def test_event_on_envvar_change(xonsh_builtins):
env = Env(TEST=0)
xonsh_builtins.__xonsh_env__ = env
xonsh_builtins.__xonsh__.env = env
share = []
# register
@xonsh_builtins.events.on_envvar_change
Expand All @@ -174,7 +174,7 @@ def handler(name, oldvalue, newvalue, **kwargs):

def test_event_on_envvar_new(xonsh_builtins):
env = Env()
xonsh_builtins.__xonsh_env__ = env
xonsh_builtins.__xonsh__.env = env
share = []
# register
@xonsh_builtins.events.on_envvar_new
Expand All @@ -189,7 +189,7 @@ def handler(name, value, **kwargs):

def test_event_on_envvar_change_from_none_value(xonsh_builtins):
env = Env(TEST=None)
xonsh_builtins.__xonsh_env__ = env
xonsh_builtins.__xonsh__.env = env
share = []
# register
@xonsh_builtins.events.on_envvar_change
Expand All @@ -205,7 +205,7 @@ def handler(name, oldvalue, newvalue, **kwargs):
@pytest.mark.parametrize("val", [1, None, True, "ok"])
def test_event_on_envvar_change_no_fire_when_value_is_same(val, xonsh_builtins):
env = Env(TEST=val)
xonsh_builtins.__xonsh_env__ = env
xonsh_builtins.__xonsh__.env = env
share = []
# register
@xonsh_builtins.events.on_envvar_change
Expand All @@ -220,7 +220,7 @@ def handler(name, oldvalue, newvalue, **kwargs):

def test_events_on_envvar_called_in_right_order(xonsh_builtins):
env = Env()
xonsh_builtins.__xonsh_env__ = env
xonsh_builtins.__xonsh__.env = env
share = []
# register
@xonsh_builtins.events.on_envvar_new
Expand Down
Loading

0 comments on commit 4c91df2

Please sign in to comment.