xonsh/tests/test_shell.py
Noorhteen Raja NJ 38295a1dd9
Remove globals (#4280)
* refactor: remove usage of global variables in abbrevs.py

* chore: add flake8-mutable to prevent mutable defaults

* fix: abbrevs expand test

* refactor: add xonsh session singleton

* refactor: fix circular errors when using xonshSession as singleton

* refactor: remove black magicked builtin attributes

* style: black format tests as well

* refactor: update tests to use xonsh-session singleton

* refactor: update abbrevs to not use builtins

* test: remove DummyCommandsCache and patch orig class

* fix: failing test_command_completers

* test: use monkeypatch to update xession fixture

* fix: failing test_pipelines

* fix: failing test_main

* chore: run test suit as single invocation

* test: fix tests/test_xonsh.xsh

* refactor: remove builtins from docs/conf.py

* fix: mypy error in jobs

* fix: test error from test_main

* test: close xession error in test_command_completers

* chore: use pytest-cov for reporting coverage

this will include subprocess calls, and will increase coverage

* style:
2021-05-20 13:14:26 +03:00

93 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
"""Testing for ``xonsh.shell.Shell``"""
import os
from xonsh.environ import Env
from xonsh.shell import Shell
from xonsh.history.json import JsonHistory
from xonsh.history.sqlite import SqliteHistory
from xonsh.history.dummy import DummyHistory
def test_shell_with_json_history(xession, xonsh_execer, tmpdir_factory):
"""
Check that shell successfully load JSON history from file.
"""
tempdir = str(tmpdir_factory.mktemp("history"))
history_file = os.path.join(tempdir, "history.json")
h = JsonHistory(filename=history_file)
h.append(
{
"inp": "echo Hello world 1\n",
"rtn": 0,
"ts": [1615887820.7329783, 1615887820.7513437],
}
)
h.append(
{
"inp": "echo Hello world 2\n",
"rtn": 0,
"ts": [1615887820.7329783, 1615887820.7513437],
}
)
h.flush()
xession.env = Env(
XONSH_DATA_DIR=tempdir,
XONSH_INTERACTIVE=True,
XONSH_HISTORY_BACKEND="json",
XONSH_HISTORY_FILE=history_file,
# XONSH_DEBUG=1 # to show errors
)
Shell(xonsh_execer, shell_type="none")
assert len([i for i in xession.history.all_items()]) == 2
def test_shell_with_sqlite_history(xession, xonsh_execer, tmpdir_factory):
"""
Check that shell successfully load SQLite history from file.
"""
tempdir = str(tmpdir_factory.mktemp("history"))
history_file = os.path.join(tempdir, "history.db")
h = SqliteHistory(filename=history_file)
h.append(
{
"inp": "echo Hello world 1\n",
"rtn": 0,
"ts": [1615887820.7329783, 1615887820.7513437],
}
)
h.append(
{
"inp": "echo Hello world 2\n",
"rtn": 0,
"ts": [1615887820.7329783, 1615887820.7513437],
}
)
h.flush()
xession.env = Env(
XONSH_DATA_DIR=tempdir,
XONSH_INTERACTIVE=True,
XONSH_HISTORY_BACKEND="sqlite",
XONSH_HISTORY_FILE=history_file,
# XONSH_DEBUG=1 # to show errors
)
Shell(xonsh_execer, shell_type="none")
assert len([i for i in xession.history.all_items()]) == 2
def test_shell_with_dummy_history_in_not_interactive(xession, xonsh_execer):
"""
Check that shell use Dummy history in not interactive mode.
"""
xession.env = Env(XONSH_INTERACTIVE=False)
xession.history = None
Shell(xonsh_execer, shell_type="none")
assert isinstance(xession.history, DummyHistory)