2021-03-17 00:07:11 +03:00
|
|
|
"""Testing for ``xonsh.shell.Shell``"""
|
|
|
|
import os
|
|
|
|
|
|
|
|
from xonsh.shell import Shell
|
|
|
|
from xonsh.history.json import JsonHistory
|
|
|
|
from xonsh.history.sqlite import SqliteHistory
|
|
|
|
from xonsh.history.dummy import DummyHistory
|
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_shell_with_json_history(xession, xonsh_execer, tmpdir_factory):
|
2021-03-17 00:07:11 +03:00
|
|
|
"""
|
|
|
|
Check that shell successfully load JSON history from file.
|
|
|
|
"""
|
|
|
|
tempdir = str(tmpdir_factory.mktemp("history"))
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
history_file = os.path.join(tempdir, "history.json")
|
2021-03-17 00:07:11 +03:00
|
|
|
h = JsonHistory(filename=history_file)
|
2021-05-20 15:44:26 +05:30
|
|
|
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],
|
|
|
|
}
|
|
|
|
)
|
2021-03-17 00:07:11 +03:00
|
|
|
h.flush()
|
|
|
|
|
2022-01-08 04:03:22 +05:30
|
|
|
xession.env.update(
|
|
|
|
dict(
|
|
|
|
XONSH_DATA_DIR=tempdir,
|
|
|
|
XONSH_INTERACTIVE=True,
|
|
|
|
XONSH_HISTORY_BACKEND="json",
|
|
|
|
XONSH_HISTORY_FILE=history_file,
|
|
|
|
# XONSH_DEBUG=1 # to show errors
|
|
|
|
)
|
2021-03-17 00:07:11 +03:00
|
|
|
)
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
Shell(xonsh_execer, shell_type="none")
|
2021-03-17 00:07:11 +03:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
assert len([i for i in xession.history.all_items()]) == 2
|
2021-03-17 00:07:11 +03:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_shell_with_sqlite_history(xession, xonsh_execer, tmpdir_factory):
|
2021-03-17 00:07:11 +03:00
|
|
|
"""
|
|
|
|
Check that shell successfully load SQLite history from file.
|
|
|
|
"""
|
|
|
|
tempdir = str(tmpdir_factory.mktemp("history"))
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
history_file = os.path.join(tempdir, "history.db")
|
2021-03-17 00:07:11 +03:00
|
|
|
h = SqliteHistory(filename=history_file)
|
2021-05-20 15:44:26 +05:30
|
|
|
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],
|
|
|
|
}
|
|
|
|
)
|
2021-03-17 00:07:11 +03:00
|
|
|
h.flush()
|
|
|
|
|
2022-01-08 04:03:22 +05:30
|
|
|
xession.env.update(
|
|
|
|
dict(
|
|
|
|
XONSH_DATA_DIR=tempdir,
|
|
|
|
XONSH_INTERACTIVE=True,
|
|
|
|
XONSH_HISTORY_BACKEND="sqlite",
|
|
|
|
XONSH_HISTORY_FILE=history_file,
|
|
|
|
# XONSH_DEBUG=1 # to show errors
|
|
|
|
)
|
2021-03-17 00:07:11 +03:00
|
|
|
)
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
Shell(xonsh_execer, shell_type="none")
|
2021-03-17 00:07:11 +03:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
assert len([i for i in xession.history.all_items()]) == 2
|
2021-03-17 00:07:11 +03:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_shell_with_dummy_history_in_not_interactive(xession, xonsh_execer):
|
2021-03-17 00:07:11 +03:00
|
|
|
"""
|
|
|
|
Check that shell use Dummy history in not interactive mode.
|
|
|
|
"""
|
2022-01-08 04:03:22 +05:30
|
|
|
xession.env["XONSH_INTERACTIVE"] = False
|
2021-05-20 15:44:26 +05:30
|
|
|
xession.history = None
|
|
|
|
Shell(xonsh_execer, shell_type="none")
|
|
|
|
assert isinstance(xession.history, DummyHistory)
|