test_history.py
187 lines
| 7.5 KiB
| text/x-python
|
PythonLexer
|
r3449 | # coding: utf-8 | ||
|
r3240 | """Tests for the IPython tab-completion machinery. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Module imports | ||||
#----------------------------------------------------------------------------- | ||||
# stdlib | ||||
import os | ||||
import sys | ||||
|
r5233 | import tempfile | ||
|
r4487 | from datetime import datetime | ||
|
r5233 | |||
|
r3240 | # third party | ||
import nose.tools as nt | ||||
# our own packages | ||||
|
r5233 | from IPython.config.loader import Config | ||
|
r3240 | from IPython.utils.tempdir import TemporaryDirectory | ||
|
r3396 | from IPython.core.history import HistoryManager, extract_hist_ranges | ||
|
r4731 | from IPython.utils import py3compat | ||
|
r3240 | |||
|
r3458 | def setUp(): | ||
|
r4731 | nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii") | ||
|
r3458 | |||
def test_history(): | ||||
|
r3240 | ip = get_ipython() | ||
with TemporaryDirectory() as tmpdir: | ||||
hist_manager_ori = ip.history_manager | ||||
|
r3715 | hist_file = os.path.join(tmpdir, 'history.sqlite') | ||
|
r3240 | try: | ||
|
r3715 | ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file) | ||
|
r6075 | hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"] | ||
|
r3396 | for i, h in enumerate(hist, start=1): | ||
ip.history_manager.store_inputs(i, h) | ||||
|
r4872 | |||
|
r3400 | ip.history_manager.db_log_output = True | ||
# Doesn't match the input, but we'll just check it's stored. | ||||
|
r3741 | ip.history_manager.output_hist_reprs[3] = "spam" | ||
|
r3415 | ip.history_manager.store_output(3) | ||
|
r4872 | |||
|
r3396 | nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist) | ||
|
r4889 | |||
# Detailed tests for _get_range_session | ||||
grs = ip.history_manager._get_range_session | ||||
|
r13394 | nt.assert_equal(list(grs(start=2,stop=-1)), list(zip([0], [2], hist[1:-1]))) | ||
nt.assert_equal(list(grs(start=-2)), list(zip([0,0], [2,3], hist[-2:]))) | ||||
nt.assert_equal(list(grs(output=True)), list(zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam'])))) | ||||
|
r4872 | |||
# Check whether specifying a range beyond the end of the current | ||||
|
r4859 | # session results in an error (gh-804) | ||
ip.magic('%hist 2-500') | ||||
|
r6074 | |||
# Check that we can write non-ascii characters to a file | ||||
|
r6075 | ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1")) | ||
ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2")) | ||||
ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3")) | ||||
ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4")) | ||||
|
r4872 | |||
|
r3396 | # New session | ||
ip.history_manager.reset() | ||||
|
r8779 | newcmds = [u"z=5", | ||
u"class X(object):\n pass", | ||||
u"k='p'", | ||||
u"z=5"] | ||||
|
r3400 | for i, cmd in enumerate(newcmds, start=1): | ||
|
r3396 | ip.history_manager.store_inputs(i, cmd) | ||
|
r3435 | gothist = ip.history_manager.get_range(start=1, stop=4) | ||
|
r13394 | nt.assert_equal(list(gothist), list(zip([0,0,0],[1,2,3], newcmds))) | ||
|
r3396 | # Previous session: | ||
|
r3435 | gothist = ip.history_manager.get_range(-1, 1, 4) | ||
|
r13394 | nt.assert_equal(list(gothist), list(zip([1,1,1],[1,2,3], hist))) | ||
|
r4872 | |||
|
r8785 | newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)] | ||
|
r8779 | |||
|
r3400 | # Check get_hist_tail | ||
|
r8779 | gothist = ip.history_manager.get_tail(5, output=True, | ||
|
r3420 | include_latest=True) | ||
|
r8779 | expected = [(1, 3, (hist[-1], "spam"))] \ | ||
+ [(s, n, (c, None)) for (s, n, c) in newhist] | ||||
|
r3400 | nt.assert_equal(list(gothist), expected) | ||
|
r4872 | |||
|
r3435 | gothist = ip.history_manager.get_tail(2) | ||
|
r8779 | expected = newhist[-3:-1] | ||
|
r3420 | nt.assert_equal(list(gothist), expected) | ||
|
r4872 | |||
|
r3400 | # Check get_hist_search | ||
|
r3435 | gothist = ip.history_manager.search("*test*") | ||
|
r3400 | nt.assert_equal(list(gothist), [(1,2,hist[1])] ) | ||
|
r8781 | |||
|
r8400 | gothist = ip.history_manager.search("*=*") | ||
nt.assert_equal(list(gothist), | ||||
[(1, 1, hist[0]), | ||||
(1, 2, hist[1]), | ||||
(1, 3, hist[2]), | ||||
|
r8779 | newhist[0], | ||
newhist[2], | ||||
newhist[3]]) | ||||
|
r8781 | |||
|
r8779 | gothist = ip.history_manager.search("*=*", n=4) | ||
|
r8400 | nt.assert_equal(list(gothist), | ||
[(1, 3, hist[2]), | ||||
|
r8779 | newhist[0], | ||
newhist[2], | ||||
newhist[3]]) | ||||
|
r8781 | |||
gothist = ip.history_manager.search("*=*", unique=True) | ||||
nt.assert_equal(list(gothist), | ||||
[(1, 1, hist[0]), | ||||
(1, 2, hist[1]), | ||||
(1, 3, hist[2]), | ||||
newhist[2], | ||||
newhist[3]]) | ||||
|
r8782 | gothist = ip.history_manager.search("*=*", unique=True, n=3) | ||
nt.assert_equal(list(gothist), | ||||
[(1, 3, hist[2]), | ||||
newhist[2], | ||||
newhist[3]]) | ||||
|
r3435 | gothist = ip.history_manager.search("b*", output=True) | ||
|
r3741 | nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] ) | ||
|
r4872 | |||
|
r3396 | # Cross testing: check that magic %save can get previous session. | ||
|
r3383 | testfilename = os.path.realpath(os.path.join(tmpdir, "test.py")) | ||
|
r6915 | ip.magic("save " + testfilename + " ~1/1-3") | ||
|
r7648 | with py3compat.open(testfilename, encoding='utf-8') as testfile: | ||
|
r4896 | nt.assert_equal(testfile.read(), | ||
|
r7853 | u"# coding: utf-8\n" + u"\n".join(hist)+u"\n") | ||
|
r4872 | |||
|
r3438 | # Duplicate line numbers - check that it doesn't crash, and | ||
# gets a new session | ||||
ip.history_manager.store_inputs(1, "rogue") | ||||
|
r3715 | ip.history_manager.writeout_cache() | ||
|
r3438 | nt.assert_equal(ip.history_manager.session_number, 3) | ||
|
r3240 | finally: | ||
|
r16768 | # Ensure saving thread is shut down before we try to clean up the files | ||
ip.history_manager.save_thread.stop() | ||||
|
r16771 | # Forcibly close database rather than relying on garbage collection | ||
ip.history_manager.db.close() | ||||
|
r3240 | # Restore history manager | ||
ip.history_manager = hist_manager_ori | ||||
|
r3396 | |||
|
r3447 | |||
|
r3396 | def test_extract_hist_ranges(): | ||
|
r9797 | instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/" | ||
|
r3396 | expected = [(0, 1, 2), # 0 == current session | ||
(2, 3, 4), | ||||
(-4, 5, 7), | ||||
(-4, 7, 10), | ||||
(-9, 2, None), # None == to end | ||||
(-8, 1, None), | ||||
|
r9798 | (-7, 1, 6), | ||
|
r9797 | (-10, 1, None)] | ||
|
r3396 | actual = list(extract_hist_ranges(instr)) | ||
nt.assert_equal(actual, expected) | ||||
|
r4872 | |||
|
r3420 | def test_magic_rerun(): | ||
"""Simple test for %rerun (no args -> rerun last line)""" | ||||
ip = get_ipython() | ||||
|
r4995 | ip.run_cell("a = 10", store_history=True) | ||
ip.run_cell("a += 1", store_history=True) | ||||
|
r3420 | nt.assert_equal(ip.user_ns["a"], 11) | ||
|
r4995 | ip.run_cell("%rerun", store_history=True) | ||
|
r3420 | nt.assert_equal(ip.user_ns["a"], 12) | ||
|
r4487 | |||
def test_timestamp_type(): | ||||
ip = get_ipython() | ||||
info = ip.history_manager.get_session_info() | ||||
nt.assert_true(isinstance(info[1], datetime)) | ||||
|
r5233 | |||
def test_hist_file_config(): | ||||
cfg = Config() | ||||
|
r5242 | tfile = tempfile.NamedTemporaryFile(delete=False) | ||
cfg.HistoryManager.hist_file = tfile.name | ||||
try: | ||||
hm = HistoryManager(shell=get_ipython(), config=cfg) | ||||
|
r7875 | nt.assert_equal(hm.hist_file, cfg.HistoryManager.hist_file) | ||
|
r5242 | finally: | ||
|
r5297 | try: | ||
os.remove(tfile.name) | ||||
except OSError: | ||||
# same catch as in testing.tools.TempFileMixin | ||||
# On Windows, even though we close the file, we still can't | ||||
# delete it. I have no clue why | ||||
pass | ||||
|
r5233 | |||