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