blob: 67ee9b7f7cfb1f4f65ea839058c508511349c69e [file] [log] [blame]
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001"""
2Very minimal unittests for parts of the readline module.
Ronald Oussoren2efd9242009-09-20 14:53:22 +00003"""
Martin Panter37126862016-05-15 03:05:36 +00004from contextlib import ExitStack
Martin Panterf0dbf7a2016-05-15 01:26:25 +00005from errno import EIO
Victor Stinnerc495e792018-01-16 17:34:34 +01006import locale
Victor Stinnera3c80ce2014-07-24 12:23:56 +02007import os
Martin Panterf0dbf7a2016-05-15 01:26:25 +00008import selectors
9import subprocess
10import sys
Benjamin Peterson33f8f152014-11-26 13:58:16 -060011import tempfile
Ronald Oussoren2efd9242009-09-20 14:53:22 +000012import unittest
Victor Stinner1881bef2017-07-07 16:06:58 +020013from test.support import import_module, unlink, temp_dir, TESTFN, verbose
Berker Peksagce643912015-05-06 06:33:17 +030014from test.support.script_helper import assert_python_ok
Ronald Oussoren2efd9242009-09-20 14:53:22 +000015
Mark Dickinson2d7062e2009-10-26 12:01:06 +000016# Skip tests if there is no readline module
17readline = import_module('readline')
Ronald Oussoren2efd9242009-09-20 14:53:22 +000018
Victor Stinner1881bef2017-07-07 16:06:58 +020019if hasattr(readline, "_READLINE_LIBRARY_VERSION"):
20 is_editline = ("EditLine wrapper" in readline._READLINE_LIBRARY_VERSION)
21else:
22 is_editline = (readline.__doc__ and "libedit" in readline.__doc__)
23
24
25def setUpModule():
26 if verbose:
27 # Python implementations other than CPython may not have
28 # these private attributes
29 if hasattr(readline, "_READLINE_VERSION"):
30 print(f"readline version: {readline._READLINE_VERSION:#x}")
31 print(f"readline runtime version: {readline._READLINE_RUNTIME_VERSION:#x}")
32 if hasattr(readline, "_READLINE_LIBRARY_VERSION"):
33 print(f"readline library version: {readline._READLINE_LIBRARY_VERSION!r}")
34 print(f"use libedit emulation? {is_editline}")
35
Martin Panter056f76d2016-06-14 05:45:31 +000036
Martin Panterf00c49d2016-06-14 01:16:16 +000037@unittest.skipUnless(hasattr(readline, "clear_history"),
38 "The history update test cannot be run because the "
39 "clear_history method is not available.")
Ronald Oussoren2efd9242009-09-20 14:53:22 +000040class TestHistoryManipulation (unittest.TestCase):
Victor Stinnera3c80ce2014-07-24 12:23:56 +020041 """
42 These tests were added to check that the libedit emulation on OSX and the
43 "real" readline have the same interface for history manipulation. That's
44 why the tests cover only a small subset of the interface.
45 """
R David Murrayf8b9dfd2011-03-14 17:10:22 -040046
Ronald Oussoren2efd9242009-09-20 14:53:22 +000047 def testHistoryUpdates(self):
Georg Brandl7b250a52012-08-11 11:02:14 +020048 readline.clear_history()
Ronald Oussoren2efd9242009-09-20 14:53:22 +000049
50 readline.add_history("first line")
51 readline.add_history("second line")
52
53 self.assertEqual(readline.get_history_item(0), None)
54 self.assertEqual(readline.get_history_item(1), "first line")
55 self.assertEqual(readline.get_history_item(2), "second line")
56
57 readline.replace_history_item(0, "replaced line")
58 self.assertEqual(readline.get_history_item(0), None)
59 self.assertEqual(readline.get_history_item(1), "replaced line")
60 self.assertEqual(readline.get_history_item(2), "second line")
61
62 self.assertEqual(readline.get_current_history_length(), 2)
63
64 readline.remove_history_item(0)
65 self.assertEqual(readline.get_history_item(0), None)
66 self.assertEqual(readline.get_history_item(1), "second line")
67
68 self.assertEqual(readline.get_current_history_length(), 1)
69
Ned Deily8007cbc2014-11-26 13:02:33 -080070 @unittest.skipUnless(hasattr(readline, "append_history_file"),
Benjamin Petersond1e22ba2014-11-26 14:35:12 -060071 "append_history not available")
Benjamin Peterson33f8f152014-11-26 13:58:16 -060072 def test_write_read_append(self):
73 hfile = tempfile.NamedTemporaryFile(delete=False)
74 hfile.close()
75 hfilename = hfile.name
76 self.addCleanup(unlink, hfilename)
77
78 # test write-clear-read == nop
79 readline.clear_history()
80 readline.add_history("first line")
81 readline.add_history("second line")
82 readline.write_history_file(hfilename)
83
84 readline.clear_history()
85 self.assertEqual(readline.get_current_history_length(), 0)
86
87 readline.read_history_file(hfilename)
88 self.assertEqual(readline.get_current_history_length(), 2)
89 self.assertEqual(readline.get_history_item(1), "first line")
90 self.assertEqual(readline.get_history_item(2), "second line")
91
92 # test append
93 readline.append_history_file(1, hfilename)
94 readline.clear_history()
95 readline.read_history_file(hfilename)
96 self.assertEqual(readline.get_current_history_length(), 3)
97 self.assertEqual(readline.get_history_item(1), "first line")
98 self.assertEqual(readline.get_history_item(2), "second line")
99 self.assertEqual(readline.get_history_item(3), "second line")
100
101 # test 'no such file' behaviour
102 os.unlink(hfilename)
103 with self.assertRaises(FileNotFoundError):
104 readline.append_history_file(1, hfilename)
105
106 # write_history_file can create the target
107 readline.write_history_file(hfilename)
108
Martin Panterf00c49d2016-06-14 01:16:16 +0000109 def test_nonascii_history(self):
110 readline.clear_history()
111 try:
112 readline.add_history("entrée 1")
113 except UnicodeEncodeError as err:
114 self.skipTest("Locale cannot encode test data: " + format(err))
115 readline.add_history("entrée 2")
116 readline.replace_history_item(1, "entrée 22")
117 readline.write_history_file(TESTFN)
118 self.addCleanup(os.remove, TESTFN)
119 readline.clear_history()
120 readline.read_history_file(TESTFN)
Martin Panter056f76d2016-06-14 05:45:31 +0000121 if is_editline:
122 # An add_history() call seems to be required for get_history_
123 # item() to register items from the file
124 readline.add_history("dummy")
Martin Panterf00c49d2016-06-14 01:16:16 +0000125 self.assertEqual(readline.get_history_item(1), "entrée 1")
126 self.assertEqual(readline.get_history_item(2), "entrée 22")
127
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000128
Victor Stinnera3c80ce2014-07-24 12:23:56 +0200129class TestReadline(unittest.TestCase):
Antoine Pitrou7e8b8672014-11-04 14:52:10 +0100130
Martin Panterc427b8d2016-08-27 03:23:11 +0000131 @unittest.skipIf(readline._READLINE_VERSION < 0x0601 and not is_editline,
Antoine Pitrou7e8b8672014-11-04 14:52:10 +0100132 "not supported in this library version")
Victor Stinnera3c80ce2014-07-24 12:23:56 +0200133 def test_init(self):
134 # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
135 # written into stdout when the readline module is imported and stdout
136 # is redirected to a pipe.
137 rc, stdout, stderr = assert_python_ok('-c', 'import readline',
138 TERM='xterm-256color')
139 self.assertEqual(stdout, b'')
140
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000141 auto_history_script = """\
142import readline
143readline.set_auto_history({})
144input()
145print("History length:", readline.get_current_history_length())
146"""
147
148 def test_auto_history_enabled(self):
149 output = run_pty(self.auto_history_script.format(True))
150 self.assertIn(b"History length: 1\r\n", output)
151
152 def test_auto_history_disabled(self):
153 output = run_pty(self.auto_history_script.format(False))
154 self.assertIn(b"History length: 0\r\n", output)
155
Martin Panterf00c49d2016-06-14 01:16:16 +0000156 def test_nonascii(self):
Victor Stinnerc495e792018-01-16 17:34:34 +0100157 loc = locale.setlocale(locale.LC_CTYPE, None)
158 if loc in ('C', 'POSIX'):
159 # bpo-29240: On FreeBSD, if the LC_CTYPE locale is C or POSIX,
160 # writing and reading non-ASCII bytes into/from a TTY works, but
161 # readline or ncurses ignores non-ASCII bytes on read.
162 self.skipTest(f"the LC_CTYPE locale is {loc!r}")
163
Martin Panterf00c49d2016-06-14 01:16:16 +0000164 try:
165 readline.add_history("\xEB\xEF")
166 except UnicodeEncodeError as err:
167 self.skipTest("Locale cannot encode test data: " + format(err))
168
169 script = r"""import readline
170
Martin Panter6afbc652016-06-14 08:45:43 +0000171is_editline = readline.__doc__ and "libedit" in readline.__doc__
172inserted = "[\xEFnserted]"
173macro = "|t\xEB[after]"
174set_pre_input_hook = getattr(readline, "set_pre_input_hook", None)
175if is_editline or not set_pre_input_hook:
Martin Panter056f76d2016-06-14 05:45:31 +0000176 # The insert_line() call via pre_input_hook() does nothing with Editline,
177 # so include the extra text that would have been inserted here
Martin Panter6afbc652016-06-14 08:45:43 +0000178 macro = inserted + macro
179
180if is_editline:
181 readline.parse_and_bind(r'bind ^B ed-prev-char')
182 readline.parse_and_bind(r'bind "\t" rl_complete')
183 readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro))
Martin Panterf00c49d2016-06-14 01:16:16 +0000184else:
185 readline.parse_and_bind(r'Control-b: backward-char')
186 readline.parse_and_bind(r'"\t": complete')
187 readline.parse_and_bind(r'set disable-completion off')
188 readline.parse_and_bind(r'set show-all-if-ambiguous off')
189 readline.parse_and_bind(r'set show-all-if-unmodified off')
Martin Panter6afbc652016-06-14 08:45:43 +0000190 readline.parse_and_bind(r'Control-a: "{}"'.format(macro))
Martin Panterf00c49d2016-06-14 01:16:16 +0000191
192def pre_input_hook():
Martin Panter6afbc652016-06-14 08:45:43 +0000193 readline.insert_text(inserted)
Martin Panterf00c49d2016-06-14 01:16:16 +0000194 readline.redisplay()
Martin Panter6afbc652016-06-14 08:45:43 +0000195if set_pre_input_hook:
196 set_pre_input_hook(pre_input_hook)
Martin Panterf00c49d2016-06-14 01:16:16 +0000197
198def completer(text, state):
199 if text == "t\xEB":
200 if state == 0:
201 print("text", ascii(text))
202 print("line", ascii(readline.get_line_buffer()))
203 print("indexes", readline.get_begidx(), readline.get_endidx())
204 return "t\xEBnt"
205 if state == 1:
206 return "t\xEBxt"
207 if text == "t\xEBx" and state == 0:
208 return "t\xEBxt"
209 return None
210readline.set_completer(completer)
211
212def display(substitution, matches, longest_match_length):
213 print("substitution", ascii(substitution))
214 print("matches", ascii(matches))
215readline.set_completion_display_matches_hook(display)
216
217print("result", ascii(input()))
218print("history", ascii(readline.get_history_item(1)))
219"""
220
221 input = b"\x01" # Ctrl-A, expands to "|t\xEB[after]"
222 input += b"\x02" * len("[after]") # Move cursor back
223 input += b"\t\t" # Display possible completions
224 input += b"x\t" # Complete "t\xEBx" -> "t\xEBxt"
225 input += b"\r"
226 output = run_pty(script, input)
227 self.assertIn(b"text 't\\xeb'\r\n", output)
228 self.assertIn(b"line '[\\xefnserted]|t\\xeb[after]'\r\n", output)
229 self.assertIn(b"indexes 11 13\r\n", output)
Martin Pantera8cadb22016-06-14 11:29:31 +0000230 if not is_editline and hasattr(readline, "set_pre_input_hook"):
Martin Panter056f76d2016-06-14 05:45:31 +0000231 self.assertIn(b"substitution 't\\xeb'\r\n", output)
232 self.assertIn(b"matches ['t\\xebnt', 't\\xebxt']\r\n", output)
Martin Panterf00c49d2016-06-14 01:16:16 +0000233 expected = br"'[\xefnserted]|t\xebxt[after]'"
234 self.assertIn(b"result " + expected + b"\r\n", output)
235 self.assertIn(b"history " + expected + b"\r\n", output)
236
Nir Sofferaa6a4d62017-07-08 17:34:27 +0300237 # We have 2 reasons to skip this test:
238 # - readline: history size was added in 6.0
239 # See https://cnswww.cns.cwru.edu/php/chet/readline/CHANGES
240 # - editline: history size is broken on OS X 10.11.6.
241 # Newer versions were not tested yet.
242 @unittest.skipIf(readline._READLINE_VERSION < 0x600,
243 "this readline version does not support history-size")
Nir Sofferfae8f4a2017-07-07 09:10:46 +0300244 @unittest.skipIf(is_editline,
245 "editline history size configuration is broken")
246 def test_history_size(self):
247 history_size = 10
248 with temp_dir() as test_dir:
249 inputrc = os.path.join(test_dir, "inputrc")
250 with open(inputrc, "wb") as f:
251 f.write(b"set history-size %d\n" % history_size)
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000252
Nir Sofferfae8f4a2017-07-07 09:10:46 +0300253 history_file = os.path.join(test_dir, "history")
254 with open(history_file, "wb") as f:
255 # history_size * 2 items crashes readline
256 data = b"".join(b"item %d\n" % i
257 for i in range(history_size * 2))
258 f.write(data)
259
260 script = """
261import os
262import readline
263
264history_file = os.environ["HISTORY_FILE"]
265readline.read_history_file(history_file)
266input()
267readline.write_history_file(history_file)
268"""
269
270 env = dict(os.environ)
271 env["INPUTRC"] = inputrc
272 env["HISTORY_FILE"] = history_file
273
274 run_pty(script, input=b"last input\r", env=env)
275
276 with open(history_file, "rb") as f:
277 lines = f.readlines()
278 self.assertEqual(len(lines), history_size)
279 self.assertEqual(lines[-1].strip(), b"last input")
280
281
282def run_pty(script, input=b"dummy input\r", env=None):
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000283 pty = import_module('pty')
284 output = bytearray()
285 [master, slave] = pty.openpty()
286 args = (sys.executable, '-c', script)
Nir Sofferfae8f4a2017-07-07 09:10:46 +0300287 proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave, env=env)
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000288 os.close(slave)
Martin Panter37126862016-05-15 03:05:36 +0000289 with ExitStack() as cleanup:
290 cleanup.enter_context(proc)
Martin Panter79f561d2016-05-15 15:04:58 +0000291 def terminate(proc):
292 try:
293 proc.terminate()
294 except ProcessLookupError:
295 # Workaround for Open/Net BSD bug (Issue 16762)
296 pass
297 cleanup.callback(terminate, proc)
Martin Panter37126862016-05-15 03:05:36 +0000298 cleanup.callback(os.close, master)
Martin Panter79f561d2016-05-15 15:04:58 +0000299 # Avoid using DefaultSelector and PollSelector. Kqueue() does not
300 # work with pseudo-terminals on OS X < 10.9 (Issue 20365) and Open
301 # BSD (Issue 20667). Poll() does not work with OS X 10.6 or 10.4
302 # either (Issue 20472). Hopefully the file descriptor is low enough
303 # to use with select().
304 sel = cleanup.enter_context(selectors.SelectSelector())
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000305 sel.register(master, selectors.EVENT_READ | selectors.EVENT_WRITE)
306 os.set_blocking(master, False)
307 while True:
308 for [_, events] in sel.select():
309 if events & selectors.EVENT_READ:
310 try:
311 chunk = os.read(master, 0x10000)
312 except OSError as err:
Martin Panterf00c49d2016-06-14 01:16:16 +0000313 # Linux raises EIO when slave is closed (Issue 5380)
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000314 if err.errno != EIO:
315 raise
316 chunk = b""
317 if not chunk:
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000318 return output
319 output.extend(chunk)
320 if events & selectors.EVENT_WRITE:
Martin Panterf00c49d2016-06-14 01:16:16 +0000321 try:
322 input = input[os.write(master, input):]
323 except OSError as err:
324 # Apparently EIO means the slave was closed
325 if err.errno != EIO:
326 raise
327 input = b"" # Stop writing
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000328 if not input:
329 sel.modify(master, selectors.EVENT_READ)
330
Victor Stinnera3c80ce2014-07-24 12:23:56 +0200331
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000332if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500333 unittest.main()