xonsh/tests/test_history.py

146 lines
4.4 KiB
Python
Raw Normal View History

2015-08-11 20:16:02 -04:00
"""Tests the xonsh history."""
from __future__ import unicode_literals, print_function
import io
2015-08-11 20:16:02 -04:00
import os
import sys
2015-08-11 20:16:02 -04:00
import nose
2015-08-11 23:05:14 -04:00
from nose.tools import assert_equal, assert_true
2015-08-11 20:16:02 -04:00
from xonsh.lazyjson import LazyJSON
2015-08-16 21:41:02 -04:00
from xonsh.history import History, CommandField
from xonsh import history
2015-08-11 20:16:02 -04:00
2015-08-11 23:05:14 -04:00
HIST_TEST_KWARGS = dict(sessionid='SESSIONID', gc=False)
2015-08-11 20:16:02 -04:00
def test_hist_init():
2015-08-11 23:05:14 -04:00
FNAME = 'xonsh-SESSIONID.json'
FNAME += '.init'
hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
2015-08-11 23:30:50 -04:00
with LazyJSON(FNAME) as lj:
2015-08-11 20:16:02 -04:00
obs = lj['here']
assert_equal('yup', obs)
2015-08-12 01:04:03 -04:00
os.remove(FNAME)
2015-08-11 20:16:02 -04:00
def test_hist_append():
2015-08-11 23:05:14 -04:00
FNAME = 'xonsh-SESSIONID.json'
FNAME += '.append'
hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
2015-08-11 20:16:02 -04:00
hf = hist.append({'joco': 'still alive'})
yield assert_true, hf is None
yield assert_equal, 'still alive', hist.buffer[0]['joco']
2015-08-11 23:05:14 -04:00
os.remove(FNAME)
2015-08-11 20:16:02 -04:00
def test_hist_flush():
2015-08-11 23:05:14 -04:00
FNAME = 'xonsh-SESSIONID.json'
FNAME += '.flush'
hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
2015-08-11 20:16:02 -04:00
hf = hist.flush()
yield assert_true, hf is None
hist.append({'joco': 'still alive'})
hf = hist.flush()
yield assert_true, hf is not None
while hf.is_alive():
pass
2015-08-11 23:30:50 -04:00
with LazyJSON(FNAME) as lj:
2015-08-11 20:16:02 -04:00
obs = lj['cmds'][0]['joco']
yield assert_equal, 'still alive', obs
2015-08-11 23:05:14 -04:00
os.remove(FNAME)
2015-08-11 20:16:02 -04:00
2015-08-16 21:41:02 -04:00
def test_cmd_field():
FNAME = 'xonsh-SESSIONID.json'
FNAME += '.cmdfield'
hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
# in-memory
hf = hist.append({'rtn': 1})
yield assert_true, hf is None
yield assert_equal, 1, hist.rtns[0]
yield assert_equal, 1, hist.rtns[-1]
yield assert_equal, None, hist.outs[-1]
# slice
yield assert_equal, [1], hist.rtns[:]
# on disk
hf = hist.flush()
yield assert_true, hf is not None
yield assert_equal, 1, hist.rtns[0]
yield assert_equal, 1, hist.rtns[-1]
yield assert_equal, None, hist.outs[-1]
os.remove(FNAME)
2015-08-11 20:16:02 -04:00
def test_show_cmd():
FNAME = 'xonsh-SESSIONID.json'
FNAME += '.show_cmd'
cmds = ['ls', 'cat hello kitty', 'abc', 'def', 'touch me', 'grep from me']
def FormatHistLine(idx, cmd):
return ' {:d} {:s}\n'.format(idx, cmd)
def TestShowCmd(hist_args, commands, base_idx=0, step=1):
stdout.seek(0, io.SEEK_SET)
stdout.truncate()
history.main(hist_args, hist=hist)
stdout.seek(0, io.SEEK_SET)
hist_lines = stdout.readlines()
yield assert_equal, len(commands), len(hist_lines)
for idx, (cmd, actual) in enumerate(zip(commands, hist_lines)):
expected = FormatHistLine(base_idx + idx * step, cmd)
yield assert_equal, expected, actual
hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
stdout = io.StringIO()
saved_stdout = sys.stdout
sys.stdout = stdout
for cmd in cmds: # populate the shell history
hist.append({'inp': cmd, 'rtn': 0})
# Verify an implicit "show" emits the entire history.
for x in TestShowCmd([], cmds):
yield x
# Verify an explicit "show" with no qualifiers emits the entire history.
for x in TestShowCmd(['show'], cmds):
yield x
# Verify an explicit "show" with a reversed qualifier emits the entire
# history in reverse order.
for x in TestShowCmd(['show', '-r'], list(reversed(cmds)),
len(cmds) - 1, -1):
yield x
# Verify that showing a specific history entry relative to the start of the
# history works.
for x in TestShowCmd(['show', '0'], [cmds[0]], 0):
yield x
for x in TestShowCmd(['show', '1'], [cmds[1]], 1):
yield x
# Verify that showing a specific history entry relative to the end of the
# history works.
for x in TestShowCmd(['show', '-2'], [cmds[-2]], len(cmds) - 2):
yield x
# Verify that showing a history range relative to the start of the
# history works.
for x in TestShowCmd(['show', '0:2'], cmds[0:2], 0):
yield x
for x in TestShowCmd(['show', '1::2'], cmds[1::2], 1, 2):
yield x
# Verify that showing a history range relative to the end of the
# history works.
for x in TestShowCmd(['show', '-2:'], cmds[-2:], len(cmds) - 2):
yield x
for x in TestShowCmd(['show', '-4:-2'], cmds[-4:-2], len(cmds) - 4):
yield x
sys.stdout = saved_stdout
os.remove(FNAME)
2015-08-11 20:16:02 -04:00
if __name__ == '__main__':
nose.runmodule()