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