##// END OF EJS Templates
Merge pull request #11470 from ellert/backport-11401...
Matthias Bussonnier -
r24826:ede2d270 merge
parent child Browse files
Show More
@@ -1,211 +1,215 b''
1 1 # coding: utf-8
2 2 """Tests for the IPython tab-completion machinery.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Module imports
6 6 #-----------------------------------------------------------------------------
7 7
8 8 # stdlib
9 9 import io
10 10 import os
11 11 import sys
12 12 import tempfile
13 13 from datetime import datetime
14 import sqlite3
14 15
15 16 # third party
16 17 import nose.tools as nt
17 18
18 19 # our own packages
19 20 from traitlets.config.loader import Config
20 21 from IPython.utils.tempdir import TemporaryDirectory
21 22 from IPython.core.history import HistoryManager, extract_hist_ranges
23 from IPython.testing.decorators import skipif
22 24 from IPython.utils import py3compat
23 25
24 26 def setUp():
25 27 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
26 28
29 @skipif(sqlite3.sqlite_version_info > (3,24,0))
27 30 def test_history():
28 31 ip = get_ipython()
29 32 with TemporaryDirectory() as tmpdir:
30 33 hist_manager_ori = ip.history_manager
31 34 hist_file = os.path.join(tmpdir, 'history.sqlite')
32 35 try:
33 36 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
34 37 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
35 38 for i, h in enumerate(hist, start=1):
36 39 ip.history_manager.store_inputs(i, h)
37 40
38 41 ip.history_manager.db_log_output = True
39 42 # Doesn't match the input, but we'll just check it's stored.
40 43 ip.history_manager.output_hist_reprs[3] = "spam"
41 44 ip.history_manager.store_output(3)
42 45
43 46 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
44
47
45 48 # Detailed tests for _get_range_session
46 49 grs = ip.history_manager._get_range_session
47 50 nt.assert_equal(list(grs(start=2,stop=-1)), list(zip([0], [2], hist[1:-1])))
48 51 nt.assert_equal(list(grs(start=-2)), list(zip([0,0], [2,3], hist[-2:])))
49 52 nt.assert_equal(list(grs(output=True)), list(zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam']))))
50 53
51 54 # Check whether specifying a range beyond the end of the current
52 55 # session results in an error (gh-804)
53 56 ip.magic('%hist 2-500')
54
57
55 58 # Check that we can write non-ascii characters to a file
56 59 ip.magic("%%hist -f %s" % os.path.join(tmpdir, "test1"))
57 60 ip.magic("%%hist -pf %s" % os.path.join(tmpdir, "test2"))
58 61 ip.magic("%%hist -nf %s" % os.path.join(tmpdir, "test3"))
59 62 ip.magic("%%save %s 1-10" % os.path.join(tmpdir, "test4"))
60 63
61 64 # New session
62 65 ip.history_manager.reset()
63 66 newcmds = [u"z=5",
64 67 u"class X(object):\n pass",
65 68 u"k='p'",
66 69 u"z=5"]
67 70 for i, cmd in enumerate(newcmds, start=1):
68 71 ip.history_manager.store_inputs(i, cmd)
69 72 gothist = ip.history_manager.get_range(start=1, stop=4)
70 73 nt.assert_equal(list(gothist), list(zip([0,0,0],[1,2,3], newcmds)))
71 74 # Previous session:
72 75 gothist = ip.history_manager.get_range(-1, 1, 4)
73 76 nt.assert_equal(list(gothist), list(zip([1,1,1],[1,2,3], hist)))
74 77
75 78 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
76 79
77 80 # Check get_hist_tail
78 81 gothist = ip.history_manager.get_tail(5, output=True,
79 82 include_latest=True)
80 83 expected = [(1, 3, (hist[-1], "spam"))] \
81 84 + [(s, n, (c, None)) for (s, n, c) in newhist]
82 85 nt.assert_equal(list(gothist), expected)
83 86
84 87 gothist = ip.history_manager.get_tail(2)
85 88 expected = newhist[-3:-1]
86 89 nt.assert_equal(list(gothist), expected)
87 90
88 91 # Check get_hist_search
92
89 93 gothist = ip.history_manager.search("*test*")
90 94 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
91 95
92 96 gothist = ip.history_manager.search("*=*")
93 97 nt.assert_equal(list(gothist),
94 98 [(1, 1, hist[0]),
95 99 (1, 2, hist[1]),
96 100 (1, 3, hist[2]),
97 101 newhist[0],
98 102 newhist[2],
99 103 newhist[3]])
100 104
101 105 gothist = ip.history_manager.search("*=*", n=4)
102 106 nt.assert_equal(list(gothist),
103 107 [(1, 3, hist[2]),
104 108 newhist[0],
105 109 newhist[2],
106 110 newhist[3]])
107 111
108 112 gothist = ip.history_manager.search("*=*", unique=True)
109 113 nt.assert_equal(list(gothist),
110 114 [(1, 1, hist[0]),
111 115 (1, 2, hist[1]),
112 116 (1, 3, hist[2]),
113 117 newhist[2],
114 118 newhist[3]])
115 119
116 120 gothist = ip.history_manager.search("*=*", unique=True, n=3)
117 121 nt.assert_equal(list(gothist),
118 122 [(1, 3, hist[2]),
119 123 newhist[2],
120 124 newhist[3]])
121 125
122 126 gothist = ip.history_manager.search("b*", output=True)
123 127 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
124 128
125 129 # Cross testing: check that magic %save can get previous session.
126 130 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
127 131 ip.magic("save " + testfilename + " ~1/1-3")
128 132 with io.open(testfilename, encoding='utf-8') as testfile:
129 133 nt.assert_equal(testfile.read(),
130 134 u"# coding: utf-8\n" + u"\n".join(hist)+u"\n")
131 135
132 136 # Duplicate line numbers - check that it doesn't crash, and
133 137 # gets a new session
134 138 ip.history_manager.store_inputs(1, "rogue")
135 139 ip.history_manager.writeout_cache()
136 140 nt.assert_equal(ip.history_manager.session_number, 3)
137 141 finally:
138 142 # Ensure saving thread is shut down before we try to clean up the files
139 143 ip.history_manager.save_thread.stop()
140 144 # Forcibly close database rather than relying on garbage collection
141 145 ip.history_manager.db.close()
142 146 # Restore history manager
143 147 ip.history_manager = hist_manager_ori
144 148
145 149
146 150 def test_extract_hist_ranges():
147 151 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/"
148 152 expected = [(0, 1, 2), # 0 == current session
149 153 (2, 3, 4),
150 154 (-4, 5, 7),
151 155 (-4, 7, 10),
152 156 (-9, 2, None), # None == to end
153 157 (-8, 1, None),
154 158 (-7, 1, 6),
155 159 (-10, 1, None)]
156 160 actual = list(extract_hist_ranges(instr))
157 161 nt.assert_equal(actual, expected)
158 162
159 163 def test_magic_rerun():
160 164 """Simple test for %rerun (no args -> rerun last line)"""
161 165 ip = get_ipython()
162 166 ip.run_cell("a = 10", store_history=True)
163 167 ip.run_cell("a += 1", store_history=True)
164 168 nt.assert_equal(ip.user_ns["a"], 11)
165 169 ip.run_cell("%rerun", store_history=True)
166 170 nt.assert_equal(ip.user_ns["a"], 12)
167 171
168 172 def test_timestamp_type():
169 173 ip = get_ipython()
170 174 info = ip.history_manager.get_session_info()
171 175 nt.assert_true(isinstance(info[1], datetime))
172 176
173 177 def test_hist_file_config():
174 178 cfg = Config()
175 179 tfile = tempfile.NamedTemporaryFile(delete=False)
176 180 cfg.HistoryManager.hist_file = tfile.name
177 181 try:
178 182 hm = HistoryManager(shell=get_ipython(), config=cfg)
179 183 nt.assert_equal(hm.hist_file, cfg.HistoryManager.hist_file)
180 184 finally:
181 185 try:
182 186 os.remove(tfile.name)
183 187 except OSError:
184 188 # same catch as in testing.tools.TempFileMixin
185 189 # On Windows, even though we close the file, we still can't
186 190 # delete it. I have no clue why
187 191 pass
188 192
189 193 def test_histmanager_disabled():
190 194 """Ensure that disabling the history manager doesn't create a database."""
191 195 cfg = Config()
192 196 cfg.HistoryAccessor.enabled = False
193 197
194 198 ip = get_ipython()
195 199 with TemporaryDirectory() as tmpdir:
196 200 hist_manager_ori = ip.history_manager
197 201 hist_file = os.path.join(tmpdir, 'history.sqlite')
198 202 cfg.HistoryManager.hist_file = hist_file
199 203 try:
200 204 ip.history_manager = HistoryManager(shell=ip, config=cfg)
201 205 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
202 206 for i, h in enumerate(hist, start=1):
203 207 ip.history_manager.store_inputs(i, h)
204 208 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
205 209 ip.history_manager.reset()
206 210 ip.history_manager.end_session()
207 211 finally:
208 212 ip.history_manager = hist_manager_ori
209 213
210 214 # hist_file should not be created
211 215 nt.assert_false(os.path.exists(hist_file))
General Comments 0
You need to be logged in to leave comments. Login now