##// END OF EJS Templates
Fix failing test that did direct magic access.
Fernando Perez -
Show More
@@ -1,151 +1,151
1 # coding: utf-8
1 # coding: utf-8
2 """Tests for the IPython tab-completion machinery.
2 """Tests for the IPython tab-completion machinery.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Module imports
5 # Module imports
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7
7
8 # stdlib
8 # stdlib
9 import os
9 import os
10 import shutil
10 import shutil
11 import sys
11 import sys
12 import tempfile
12 import tempfile
13 import unittest
13 import unittest
14 from datetime import datetime
14 from datetime import datetime
15
15
16 # third party
16 # third party
17 import nose.tools as nt
17 import nose.tools as nt
18
18
19 # our own packages
19 # our own packages
20 from IPython.config.loader import Config
20 from IPython.config.loader import Config
21 from IPython.utils.tempdir import TemporaryDirectory
21 from IPython.utils.tempdir import TemporaryDirectory
22 from IPython.core.history import HistoryManager, extract_hist_ranges
22 from IPython.core.history import HistoryManager, extract_hist_ranges
23 from IPython.utils import py3compat
23 from IPython.utils import py3compat
24
24
25 def setUp():
25 def setUp():
26 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
26 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
27
27
28 def test_history():
28 def test_history():
29 ip = get_ipython()
29 ip = get_ipython()
30 with TemporaryDirectory() as tmpdir:
30 with TemporaryDirectory() as tmpdir:
31 hist_manager_ori = ip.history_manager
31 hist_manager_ori = ip.history_manager
32 hist_file = os.path.join(tmpdir, 'history.sqlite')
32 hist_file = os.path.join(tmpdir, 'history.sqlite')
33 try:
33 try:
34 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
34 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
35 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='β‚¬Γ†ΒΎΓ·ΓŸ'"]
35 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='β‚¬Γ†ΒΎΓ·ΓŸ'"]
36 for i, h in enumerate(hist, start=1):
36 for i, h in enumerate(hist, start=1):
37 ip.history_manager.store_inputs(i, h)
37 ip.history_manager.store_inputs(i, h)
38
38
39 ip.history_manager.db_log_output = True
39 ip.history_manager.db_log_output = True
40 # Doesn't match the input, but we'll just check it's stored.
40 # Doesn't match the input, but we'll just check it's stored.
41 ip.history_manager.output_hist_reprs[3] = "spam"
41 ip.history_manager.output_hist_reprs[3] = "spam"
42 ip.history_manager.store_output(3)
42 ip.history_manager.store_output(3)
43
43
44 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
44 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
45
45
46 # Detailed tests for _get_range_session
46 # Detailed tests for _get_range_session
47 grs = ip.history_manager._get_range_session
47 grs = ip.history_manager._get_range_session
48 nt.assert_equal(list(grs(start=2,stop=-1)), zip([0], [2], hist[1:-1]))
48 nt.assert_equal(list(grs(start=2,stop=-1)), zip([0], [2], hist[1:-1]))
49 nt.assert_equal(list(grs(start=-2)), zip([0,0], [2,3], hist[-2:]))
49 nt.assert_equal(list(grs(start=-2)), zip([0,0], [2,3], hist[-2:]))
50 nt.assert_equal(list(grs(output=True)), zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam'])))
50 nt.assert_equal(list(grs(output=True)), zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam'])))
51
51
52 # Check whether specifying a range beyond the end of the current
52 # Check whether specifying a range beyond the end of the current
53 # session results in an error (gh-804)
53 # session results in an error (gh-804)
54 ip.magic('%hist 2-500')
54 ip.magic('%hist 2-500')
55
55
56 # Check that we can write non-ascii characters to a file
56 # Check that we can write non-ascii characters to a file
57 ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
57 ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
58 ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
58 ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
59 ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
59 ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
60 ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))
60 ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))
61
61
62 # New session
62 # New session
63 ip.history_manager.reset()
63 ip.history_manager.reset()
64 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
64 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
65 for i, cmd in enumerate(newcmds, start=1):
65 for i, cmd in enumerate(newcmds, start=1):
66 ip.history_manager.store_inputs(i, cmd)
66 ip.history_manager.store_inputs(i, cmd)
67 gothist = ip.history_manager.get_range(start=1, stop=4)
67 gothist = ip.history_manager.get_range(start=1, stop=4)
68 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
68 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
69 # Previous session:
69 # Previous session:
70 gothist = ip.history_manager.get_range(-1, 1, 4)
70 gothist = ip.history_manager.get_range(-1, 1, 4)
71 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
71 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
72
72
73 # Check get_hist_tail
73 # Check get_hist_tail
74 gothist = ip.history_manager.get_tail(4, output=True,
74 gothist = ip.history_manager.get_tail(4, output=True,
75 include_latest=True)
75 include_latest=True)
76 expected = [(1, 3, (hist[-1], "spam")),
76 expected = [(1, 3, (hist[-1], "spam")),
77 (2, 1, (newcmds[0], None)),
77 (2, 1, (newcmds[0], None)),
78 (2, 2, (newcmds[1], None)),
78 (2, 2, (newcmds[1], None)),
79 (2, 3, (newcmds[2], None)),]
79 (2, 3, (newcmds[2], None)),]
80 nt.assert_equal(list(gothist), expected)
80 nt.assert_equal(list(gothist), expected)
81
81
82 gothist = ip.history_manager.get_tail(2)
82 gothist = ip.history_manager.get_tail(2)
83 expected = [(2, 1, newcmds[0]),
83 expected = [(2, 1, newcmds[0]),
84 (2, 2, newcmds[1])]
84 (2, 2, newcmds[1])]
85 nt.assert_equal(list(gothist), expected)
85 nt.assert_equal(list(gothist), expected)
86
86
87 # Check get_hist_search
87 # Check get_hist_search
88 gothist = ip.history_manager.search("*test*")
88 gothist = ip.history_manager.search("*test*")
89 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
89 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
90 gothist = ip.history_manager.search("b*", output=True)
90 gothist = ip.history_manager.search("b*", output=True)
91 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
91 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
92
92
93 # Cross testing: check that magic %save can get previous session.
93 # Cross testing: check that magic %save can get previous session.
94 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
94 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
95 ip.magic_save(testfilename + " ~1/1-3")
95 ip.magic("save " + testfilename + " ~1/1-3")
96 with py3compat.open(testfilename) as testfile:
96 with py3compat.open(testfilename) as testfile:
97 nt.assert_equal(testfile.read(),
97 nt.assert_equal(testfile.read(),
98 u"# coding: utf-8\n" + u"\n".join(hist))
98 u"# coding: utf-8\n" + u"\n".join(hist))
99
99
100 # Duplicate line numbers - check that it doesn't crash, and
100 # Duplicate line numbers - check that it doesn't crash, and
101 # gets a new session
101 # gets a new session
102 ip.history_manager.store_inputs(1, "rogue")
102 ip.history_manager.store_inputs(1, "rogue")
103 ip.history_manager.writeout_cache()
103 ip.history_manager.writeout_cache()
104 nt.assert_equal(ip.history_manager.session_number, 3)
104 nt.assert_equal(ip.history_manager.session_number, 3)
105 finally:
105 finally:
106 # Restore history manager
106 # Restore history manager
107 ip.history_manager = hist_manager_ori
107 ip.history_manager = hist_manager_ori
108
108
109
109
110 def test_extract_hist_ranges():
110 def test_extract_hist_ranges():
111 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5"
111 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5"
112 expected = [(0, 1, 2), # 0 == current session
112 expected = [(0, 1, 2), # 0 == current session
113 (2, 3, 4),
113 (2, 3, 4),
114 (-4, 5, 7),
114 (-4, 5, 7),
115 (-4, 7, 10),
115 (-4, 7, 10),
116 (-9, 2, None), # None == to end
116 (-9, 2, None), # None == to end
117 (-8, 1, None),
117 (-8, 1, None),
118 (-7, 1, 6)]
118 (-7, 1, 6)]
119 actual = list(extract_hist_ranges(instr))
119 actual = list(extract_hist_ranges(instr))
120 nt.assert_equal(actual, expected)
120 nt.assert_equal(actual, expected)
121
121
122 def test_magic_rerun():
122 def test_magic_rerun():
123 """Simple test for %rerun (no args -> rerun last line)"""
123 """Simple test for %rerun (no args -> rerun last line)"""
124 ip = get_ipython()
124 ip = get_ipython()
125 ip.run_cell("a = 10", store_history=True)
125 ip.run_cell("a = 10", store_history=True)
126 ip.run_cell("a += 1", store_history=True)
126 ip.run_cell("a += 1", store_history=True)
127 nt.assert_equal(ip.user_ns["a"], 11)
127 nt.assert_equal(ip.user_ns["a"], 11)
128 ip.run_cell("%rerun", store_history=True)
128 ip.run_cell("%rerun", store_history=True)
129 nt.assert_equal(ip.user_ns["a"], 12)
129 nt.assert_equal(ip.user_ns["a"], 12)
130
130
131 def test_timestamp_type():
131 def test_timestamp_type():
132 ip = get_ipython()
132 ip = get_ipython()
133 info = ip.history_manager.get_session_info()
133 info = ip.history_manager.get_session_info()
134 nt.assert_true(isinstance(info[1], datetime))
134 nt.assert_true(isinstance(info[1], datetime))
135
135
136 def test_hist_file_config():
136 def test_hist_file_config():
137 cfg = Config()
137 cfg = Config()
138 tfile = tempfile.NamedTemporaryFile(delete=False)
138 tfile = tempfile.NamedTemporaryFile(delete=False)
139 cfg.HistoryManager.hist_file = tfile.name
139 cfg.HistoryManager.hist_file = tfile.name
140 try:
140 try:
141 hm = HistoryManager(shell=get_ipython(), config=cfg)
141 hm = HistoryManager(shell=get_ipython(), config=cfg)
142 nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
142 nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
143 finally:
143 finally:
144 try:
144 try:
145 os.remove(tfile.name)
145 os.remove(tfile.name)
146 except OSError:
146 except OSError:
147 # same catch as in testing.tools.TempFileMixin
147 # same catch as in testing.tools.TempFileMixin
148 # On Windows, even though we close the file, we still can't
148 # On Windows, even though we close the file, we still can't
149 # delete it. I have no clue why
149 # delete it. I have no clue why
150 pass
150 pass
151
151
General Comments 0
You need to be logged in to leave comments. Login now