xonsh/tests/test_history.py

73 lines
2 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 os
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
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
if __name__ == '__main__':
nose.runmodule()