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