Show More
@@ -13,9 +13,6 b' import tempfile' | |||
|
13 | 13 | from datetime import datetime |
|
14 | 14 | import sqlite3 |
|
15 | 15 | |
|
16 | # third party | |
|
17 | import nose.tools as nt | |
|
18 | ||
|
19 | 16 | # our own packages |
|
20 | 17 | from traitlets.config.loader import Config |
|
21 | 18 | from IPython.utils.tempdir import TemporaryDirectory |
@@ -23,7 +20,7 b' from IPython.core.history import HistoryManager, extract_hist_ranges' | |||
|
23 | 20 | from IPython.testing.decorators import skipif |
|
24 | 21 | |
|
25 | 22 | def test_proper_default_encoding(): |
|
26 |
|
|
|
23 | assert sys.getdefaultencoding() == "utf-8" | |
|
27 | 24 | |
|
28 | 25 | @skipif(sqlite3.sqlite_version_info > (3,24,0)) |
|
29 | 26 | def test_history(): |
@@ -34,7 +31,7 b' def test_history():' | |||
|
34 | 31 | hist_file = tmp_path / "history.sqlite" |
|
35 | 32 | try: |
|
36 | 33 | ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file) |
|
37 |
hist = [ |
|
|
34 | hist = ["a=1", "def f():\n test = 1\n return test", "b='€Æ¾÷ß'"] | |
|
38 | 35 | for i, h in enumerate(hist, start=1): |
|
39 | 36 | ip.history_manager.store_inputs(i, h) |
|
40 | 37 | |
@@ -43,13 +40,15 b' def test_history():' | |||
|
43 | 40 | ip.history_manager.output_hist_reprs[3] = "spam" |
|
44 | 41 | ip.history_manager.store_output(3) |
|
45 | 42 | |
|
46 |
|
|
|
43 | assert ip.history_manager.input_hist_raw == [""] + hist | |
|
47 | 44 | |
|
48 | 45 | # Detailed tests for _get_range_session |
|
49 | 46 | grs = ip.history_manager._get_range_session |
|
50 |
|
|
|
51 |
|
|
|
52 | nt.assert_equal(list(grs(output=True)), list(zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam'])))) | |
|
47 | assert list(grs(start=2, stop=-1)) == list(zip([0], [2], hist[1:-1])) | |
|
48 | assert list(grs(start=-2)) == list(zip([0, 0], [2, 3], hist[-2:])) | |
|
49 | assert list(grs(output=True)) == list( | |
|
50 | zip([0, 0, 0], [1, 2, 3], zip(hist, [None, None, "spam"])) | |
|
51 | ) | |
|
53 | 52 | |
|
54 | 53 | # Check whether specifying a range beyond the end of the current |
|
55 | 54 | # session results in an error (gh-804) |
@@ -63,17 +62,14 b' def test_history():' | |||
|
63 | 62 | |
|
64 | 63 | # New session |
|
65 | 64 | ip.history_manager.reset() |
|
66 | newcmds = [u"z=5", | |
|
67 | u"class X(object):\n pass", | |
|
68 | u"k='p'", | |
|
69 | u"z=5"] | |
|
65 | newcmds = ["z=5", "class X(object):\n pass", "k='p'", "z=5"] | |
|
70 | 66 | for i, cmd in enumerate(newcmds, start=1): |
|
71 | 67 | ip.history_manager.store_inputs(i, cmd) |
|
72 | 68 | gothist = ip.history_manager.get_range(start=1, stop=4) |
|
73 |
|
|
|
69 | assert list(gothist) == list(zip([0, 0, 0], [1, 2, 3], newcmds)) | |
|
74 | 70 | # Previous session: |
|
75 | 71 | gothist = ip.history_manager.get_range(-1, 1, 4) |
|
76 |
|
|
|
72 | assert list(gothist) == list(zip([1, 1, 1], [1, 2, 3], hist)) | |
|
77 | 73 | |
|
78 | 74 | newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)] |
|
79 | 75 | |
@@ -82,62 +78,61 b' def test_history():' | |||
|
82 | 78 | include_latest=True) |
|
83 | 79 | expected = [(1, 3, (hist[-1], "spam"))] \ |
|
84 | 80 | + [(s, n, (c, None)) for (s, n, c) in newhist] |
|
85 |
|
|
|
81 | assert list(gothist) == expected | |
|
86 | 82 | |
|
87 | 83 | gothist = ip.history_manager.get_tail(2) |
|
88 | 84 | expected = newhist[-3:-1] |
|
89 |
|
|
|
85 | assert list(gothist) == expected | |
|
90 | 86 | |
|
91 | 87 | # Check get_hist_search |
|
92 | 88 | |
|
93 | 89 | gothist = ip.history_manager.search("*test*") |
|
94 |
|
|
|
90 | assert list(gothist) == [(1, 2, hist[1])] | |
|
95 | 91 | |
|
96 | 92 | gothist = ip.history_manager.search("*=*") |
|
97 |
|
|
|
98 |
|
|
|
93 | assert list(gothist) == [ | |
|
94 | (1, 1, hist[0]), | |
|
99 | 95 |
|
|
100 | 96 |
|
|
101 | 97 |
|
|
102 | 98 |
|
|
103 |
|
|
|
99 | newhist[3], | |
|
100 | ] | |
|
104 | 101 | |
|
105 | 102 | gothist = ip.history_manager.search("*=*", n=4) |
|
106 |
|
|
|
107 |
|
|
|
103 | assert list(gothist) == [ | |
|
104 | (1, 3, hist[2]), | |
|
108 | 105 |
|
|
109 | 106 |
|
|
110 |
|
|
|
107 | newhist[3], | |
|
108 | ] | |
|
111 | 109 | |
|
112 | 110 | gothist = ip.history_manager.search("*=*", unique=True) |
|
113 |
|
|
|
114 |
|
|
|
111 | assert list(gothist) == [ | |
|
112 | (1, 1, hist[0]), | |
|
115 | 113 |
|
|
116 | 114 |
|
|
117 | 115 |
|
|
118 |
|
|
|
116 | newhist[3], | |
|
117 | ] | |
|
119 | 118 | |
|
120 | 119 | gothist = ip.history_manager.search("*=*", unique=True, n=3) |
|
121 | nt.assert_equal(list(gothist), | |
|
122 | [(1, 3, hist[2]), | |
|
123 | newhist[2], | |
|
124 | newhist[3]]) | |
|
120 | assert list(gothist) == [(1, 3, hist[2]), newhist[2], newhist[3]] | |
|
125 | 121 | |
|
126 | 122 | gothist = ip.history_manager.search("b*", output=True) |
|
127 |
|
|
|
123 | assert list(gothist) == [(1, 3, (hist[2], "spam"))] | |
|
128 | 124 | |
|
129 | 125 | # Cross testing: check that magic %save can get previous session. |
|
130 | 126 | testfilename = (tmp_path / "test.py").resolve() |
|
131 | 127 | ip.magic("save " + str(testfilename) + " ~1/1-3") |
|
132 |
with io.open(testfilename, encoding= |
|
|
133 | nt.assert_equal(testfile.read(), | |
|
134 | u"# coding: utf-8\n" + u"\n".join(hist)+u"\n") | |
|
128 | with io.open(testfilename, encoding="utf-8") as testfile: | |
|
129 | assert testfile.read() == "# coding: utf-8\n" + "\n".join(hist) + "\n" | |
|
135 | 130 | |
|
136 | 131 | # Duplicate line numbers - check that it doesn't crash, and |
|
137 | 132 | # gets a new session |
|
138 | 133 | ip.history_manager.store_inputs(1, "rogue") |
|
139 | 134 | ip.history_manager.writeout_cache() |
|
140 |
|
|
|
135 | assert ip.history_manager.session_number == 3 | |
|
141 | 136 | finally: |
|
142 | 137 | # Ensure saving thread is shut down before we try to clean up the files |
|
143 | 138 | ip.history_manager.save_thread.stop() |
@@ -158,14 +153,14 b' def test_extract_hist_ranges():' | |||
|
158 | 153 | (-7, 1, 6), |
|
159 | 154 | (-10, 1, None)] |
|
160 | 155 | actual = list(extract_hist_ranges(instr)) |
|
161 |
|
|
|
156 | assert actual == expected | |
|
162 | 157 | |
|
163 | 158 | |
|
164 | 159 | def test_extract_hist_ranges_empty_str(): |
|
165 | 160 | instr = "" |
|
166 | 161 | expected = [(0, 1, None)] # 0 == current session, None == to end |
|
167 | 162 | actual = list(extract_hist_ranges(instr)) |
|
168 |
|
|
|
163 | assert actual == expected | |
|
169 | 164 | |
|
170 | 165 | |
|
171 | 166 | def test_magic_rerun(): |
@@ -173,14 +168,14 b' def test_magic_rerun():' | |||
|
173 | 168 | ip = get_ipython() |
|
174 | 169 | ip.run_cell("a = 10", store_history=True) |
|
175 | 170 | ip.run_cell("a += 1", store_history=True) |
|
176 |
|
|
|
171 | assert ip.user_ns["a"] == 11 | |
|
177 | 172 | ip.run_cell("%rerun", store_history=True) |
|
178 |
|
|
|
173 | assert ip.user_ns["a"] == 12 | |
|
179 | 174 | |
|
180 | 175 | def test_timestamp_type(): |
|
181 | 176 | ip = get_ipython() |
|
182 | 177 | info = ip.history_manager.get_session_info() |
|
183 |
|
|
|
178 | assert isinstance(info[1], datetime) | |
|
184 | 179 | |
|
185 | 180 | def test_hist_file_config(): |
|
186 | 181 | cfg = Config() |
@@ -188,7 +183,7 b' def test_hist_file_config():' | |||
|
188 | 183 | cfg.HistoryManager.hist_file = Path(tfile.name) |
|
189 | 184 | try: |
|
190 | 185 | hm = HistoryManager(shell=get_ipython(), config=cfg) |
|
191 |
|
|
|
186 | assert hm.hist_file == cfg.HistoryManager.hist_file | |
|
192 | 187 | finally: |
|
193 | 188 | try: |
|
194 | 189 | Path(tfile.name).unlink() |
@@ -210,14 +205,14 b' def test_histmanager_disabled():' | |||
|
210 | 205 | cfg.HistoryManager.hist_file = hist_file |
|
211 | 206 | try: |
|
212 | 207 | ip.history_manager = HistoryManager(shell=ip, config=cfg) |
|
213 |
hist = [ |
|
|
208 | hist = ["a=1", "def f():\n test = 1\n return test", "b='€Æ¾÷ß'"] | |
|
214 | 209 | for i, h in enumerate(hist, start=1): |
|
215 | 210 | ip.history_manager.store_inputs(i, h) |
|
216 |
|
|
|
211 | assert ip.history_manager.input_hist_raw == [""] + hist | |
|
217 | 212 | ip.history_manager.reset() |
|
218 | 213 | ip.history_manager.end_session() |
|
219 | 214 | finally: |
|
220 | 215 | ip.history_manager = hist_manager_ori |
|
221 | 216 | |
|
222 | 217 | # hist_file should not be created |
|
223 |
|
|
|
218 | assert hist_file.exists() is False |
General Comments 0
You need to be logged in to leave comments.
Login now