| 1 | n/a | """ |
|---|
| 2 | n/a | Very minimal unittests for parts of the readline module. |
|---|
| 3 | n/a | """ |
|---|
| 4 | n/a | from contextlib import ExitStack |
|---|
| 5 | n/a | from errno import EIO |
|---|
| 6 | n/a | import os |
|---|
| 7 | n/a | import selectors |
|---|
| 8 | n/a | import subprocess |
|---|
| 9 | n/a | import sys |
|---|
| 10 | n/a | import tempfile |
|---|
| 11 | n/a | import unittest |
|---|
| 12 | n/a | from test.support import import_module, unlink, TESTFN |
|---|
| 13 | n/a | from test.support.script_helper import assert_python_ok |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | # Skip tests if there is no readline module |
|---|
| 16 | n/a | readline = import_module('readline') |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | is_editline = readline.__doc__ and "libedit" in readline.__doc__ |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | @unittest.skipUnless(hasattr(readline, "clear_history"), |
|---|
| 21 | n/a | "The history update test cannot be run because the " |
|---|
| 22 | n/a | "clear_history method is not available.") |
|---|
| 23 | n/a | class TestHistoryManipulation (unittest.TestCase): |
|---|
| 24 | n/a | """ |
|---|
| 25 | n/a | These tests were added to check that the libedit emulation on OSX and the |
|---|
| 26 | n/a | "real" readline have the same interface for history manipulation. That's |
|---|
| 27 | n/a | why the tests cover only a small subset of the interface. |
|---|
| 28 | n/a | """ |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | def testHistoryUpdates(self): |
|---|
| 31 | n/a | readline.clear_history() |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | readline.add_history("first line") |
|---|
| 34 | n/a | readline.add_history("second line") |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | self.assertEqual(readline.get_history_item(0), None) |
|---|
| 37 | n/a | self.assertEqual(readline.get_history_item(1), "first line") |
|---|
| 38 | n/a | self.assertEqual(readline.get_history_item(2), "second line") |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | readline.replace_history_item(0, "replaced line") |
|---|
| 41 | n/a | self.assertEqual(readline.get_history_item(0), None) |
|---|
| 42 | n/a | self.assertEqual(readline.get_history_item(1), "replaced line") |
|---|
| 43 | n/a | self.assertEqual(readline.get_history_item(2), "second line") |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | self.assertEqual(readline.get_current_history_length(), 2) |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | readline.remove_history_item(0) |
|---|
| 48 | n/a | self.assertEqual(readline.get_history_item(0), None) |
|---|
| 49 | n/a | self.assertEqual(readline.get_history_item(1), "second line") |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | self.assertEqual(readline.get_current_history_length(), 1) |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | @unittest.skipUnless(hasattr(readline, "append_history_file"), |
|---|
| 54 | n/a | "append_history not available") |
|---|
| 55 | n/a | def test_write_read_append(self): |
|---|
| 56 | n/a | hfile = tempfile.NamedTemporaryFile(delete=False) |
|---|
| 57 | n/a | hfile.close() |
|---|
| 58 | n/a | hfilename = hfile.name |
|---|
| 59 | n/a | self.addCleanup(unlink, hfilename) |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | # test write-clear-read == nop |
|---|
| 62 | n/a | readline.clear_history() |
|---|
| 63 | n/a | readline.add_history("first line") |
|---|
| 64 | n/a | readline.add_history("second line") |
|---|
| 65 | n/a | readline.write_history_file(hfilename) |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | readline.clear_history() |
|---|
| 68 | n/a | self.assertEqual(readline.get_current_history_length(), 0) |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | readline.read_history_file(hfilename) |
|---|
| 71 | n/a | self.assertEqual(readline.get_current_history_length(), 2) |
|---|
| 72 | n/a | self.assertEqual(readline.get_history_item(1), "first line") |
|---|
| 73 | n/a | self.assertEqual(readline.get_history_item(2), "second line") |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | # test append |
|---|
| 76 | n/a | readline.append_history_file(1, hfilename) |
|---|
| 77 | n/a | readline.clear_history() |
|---|
| 78 | n/a | readline.read_history_file(hfilename) |
|---|
| 79 | n/a | self.assertEqual(readline.get_current_history_length(), 3) |
|---|
| 80 | n/a | self.assertEqual(readline.get_history_item(1), "first line") |
|---|
| 81 | n/a | self.assertEqual(readline.get_history_item(2), "second line") |
|---|
| 82 | n/a | self.assertEqual(readline.get_history_item(3), "second line") |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | # test 'no such file' behaviour |
|---|
| 85 | n/a | os.unlink(hfilename) |
|---|
| 86 | n/a | with self.assertRaises(FileNotFoundError): |
|---|
| 87 | n/a | readline.append_history_file(1, hfilename) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | # write_history_file can create the target |
|---|
| 90 | n/a | readline.write_history_file(hfilename) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | def test_nonascii_history(self): |
|---|
| 93 | n/a | readline.clear_history() |
|---|
| 94 | n/a | try: |
|---|
| 95 | n/a | readline.add_history("entrée 1") |
|---|
| 96 | n/a | except UnicodeEncodeError as err: |
|---|
| 97 | n/a | self.skipTest("Locale cannot encode test data: " + format(err)) |
|---|
| 98 | n/a | readline.add_history("entrée 2") |
|---|
| 99 | n/a | readline.replace_history_item(1, "entrée 22") |
|---|
| 100 | n/a | readline.write_history_file(TESTFN) |
|---|
| 101 | n/a | self.addCleanup(os.remove, TESTFN) |
|---|
| 102 | n/a | readline.clear_history() |
|---|
| 103 | n/a | readline.read_history_file(TESTFN) |
|---|
| 104 | n/a | if is_editline: |
|---|
| 105 | n/a | # An add_history() call seems to be required for get_history_ |
|---|
| 106 | n/a | # item() to register items from the file |
|---|
| 107 | n/a | readline.add_history("dummy") |
|---|
| 108 | n/a | self.assertEqual(readline.get_history_item(1), "entrée 1") |
|---|
| 109 | n/a | self.assertEqual(readline.get_history_item(2), "entrée 22") |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | class TestReadline(unittest.TestCase): |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | @unittest.skipIf(readline._READLINE_VERSION < 0x0601 and not is_editline, |
|---|
| 115 | n/a | "not supported in this library version") |
|---|
| 116 | n/a | def test_init(self): |
|---|
| 117 | n/a | # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not |
|---|
| 118 | n/a | # written into stdout when the readline module is imported and stdout |
|---|
| 119 | n/a | # is redirected to a pipe. |
|---|
| 120 | n/a | rc, stdout, stderr = assert_python_ok('-c', 'import readline', |
|---|
| 121 | n/a | TERM='xterm-256color') |
|---|
| 122 | n/a | self.assertEqual(stdout, b'') |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | auto_history_script = """\ |
|---|
| 125 | n/a | import readline |
|---|
| 126 | n/a | readline.set_auto_history({}) |
|---|
| 127 | n/a | input() |
|---|
| 128 | n/a | print("History length:", readline.get_current_history_length()) |
|---|
| 129 | n/a | """ |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def test_auto_history_enabled(self): |
|---|
| 132 | n/a | output = run_pty(self.auto_history_script.format(True)) |
|---|
| 133 | n/a | self.assertIn(b"History length: 1\r\n", output) |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | def test_auto_history_disabled(self): |
|---|
| 136 | n/a | output = run_pty(self.auto_history_script.format(False)) |
|---|
| 137 | n/a | self.assertIn(b"History length: 0\r\n", output) |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | def test_nonascii(self): |
|---|
| 140 | n/a | try: |
|---|
| 141 | n/a | readline.add_history("\xEB\xEF") |
|---|
| 142 | n/a | except UnicodeEncodeError as err: |
|---|
| 143 | n/a | self.skipTest("Locale cannot encode test data: " + format(err)) |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | script = r"""import readline |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | is_editline = readline.__doc__ and "libedit" in readline.__doc__ |
|---|
| 148 | n/a | inserted = "[\xEFnserted]" |
|---|
| 149 | n/a | macro = "|t\xEB[after]" |
|---|
| 150 | n/a | set_pre_input_hook = getattr(readline, "set_pre_input_hook", None) |
|---|
| 151 | n/a | if is_editline or not set_pre_input_hook: |
|---|
| 152 | n/a | # The insert_line() call via pre_input_hook() does nothing with Editline, |
|---|
| 153 | n/a | # so include the extra text that would have been inserted here |
|---|
| 154 | n/a | macro = inserted + macro |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | if is_editline: |
|---|
| 157 | n/a | readline.parse_and_bind(r'bind ^B ed-prev-char') |
|---|
| 158 | n/a | readline.parse_and_bind(r'bind "\t" rl_complete') |
|---|
| 159 | n/a | readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro)) |
|---|
| 160 | n/a | else: |
|---|
| 161 | n/a | readline.parse_and_bind(r'Control-b: backward-char') |
|---|
| 162 | n/a | readline.parse_and_bind(r'"\t": complete') |
|---|
| 163 | n/a | readline.parse_and_bind(r'set disable-completion off') |
|---|
| 164 | n/a | readline.parse_and_bind(r'set show-all-if-ambiguous off') |
|---|
| 165 | n/a | readline.parse_and_bind(r'set show-all-if-unmodified off') |
|---|
| 166 | n/a | readline.parse_and_bind(r'Control-a: "{}"'.format(macro)) |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | def pre_input_hook(): |
|---|
| 169 | n/a | readline.insert_text(inserted) |
|---|
| 170 | n/a | readline.redisplay() |
|---|
| 171 | n/a | if set_pre_input_hook: |
|---|
| 172 | n/a | set_pre_input_hook(pre_input_hook) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | def completer(text, state): |
|---|
| 175 | n/a | if text == "t\xEB": |
|---|
| 176 | n/a | if state == 0: |
|---|
| 177 | n/a | print("text", ascii(text)) |
|---|
| 178 | n/a | print("line", ascii(readline.get_line_buffer())) |
|---|
| 179 | n/a | print("indexes", readline.get_begidx(), readline.get_endidx()) |
|---|
| 180 | n/a | return "t\xEBnt" |
|---|
| 181 | n/a | if state == 1: |
|---|
| 182 | n/a | return "t\xEBxt" |
|---|
| 183 | n/a | if text == "t\xEBx" and state == 0: |
|---|
| 184 | n/a | return "t\xEBxt" |
|---|
| 185 | n/a | return None |
|---|
| 186 | n/a | readline.set_completer(completer) |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | def display(substitution, matches, longest_match_length): |
|---|
| 189 | n/a | print("substitution", ascii(substitution)) |
|---|
| 190 | n/a | print("matches", ascii(matches)) |
|---|
| 191 | n/a | readline.set_completion_display_matches_hook(display) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | print("result", ascii(input())) |
|---|
| 194 | n/a | print("history", ascii(readline.get_history_item(1))) |
|---|
| 195 | n/a | """ |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | input = b"\x01" # Ctrl-A, expands to "|t\xEB[after]" |
|---|
| 198 | n/a | input += b"\x02" * len("[after]") # Move cursor back |
|---|
| 199 | n/a | input += b"\t\t" # Display possible completions |
|---|
| 200 | n/a | input += b"x\t" # Complete "t\xEBx" -> "t\xEBxt" |
|---|
| 201 | n/a | input += b"\r" |
|---|
| 202 | n/a | output = run_pty(script, input) |
|---|
| 203 | n/a | self.assertIn(b"text 't\\xeb'\r\n", output) |
|---|
| 204 | n/a | self.assertIn(b"line '[\\xefnserted]|t\\xeb[after]'\r\n", output) |
|---|
| 205 | n/a | self.assertIn(b"indexes 11 13\r\n", output) |
|---|
| 206 | n/a | if not is_editline and hasattr(readline, "set_pre_input_hook"): |
|---|
| 207 | n/a | self.assertIn(b"substitution 't\\xeb'\r\n", output) |
|---|
| 208 | n/a | self.assertIn(b"matches ['t\\xebnt', 't\\xebxt']\r\n", output) |
|---|
| 209 | n/a | expected = br"'[\xefnserted]|t\xebxt[after]'" |
|---|
| 210 | n/a | self.assertIn(b"result " + expected + b"\r\n", output) |
|---|
| 211 | n/a | self.assertIn(b"history " + expected + b"\r\n", output) |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | def run_pty(script, input=b"dummy input\r"): |
|---|
| 215 | n/a | pty = import_module('pty') |
|---|
| 216 | n/a | output = bytearray() |
|---|
| 217 | n/a | [master, slave] = pty.openpty() |
|---|
| 218 | n/a | args = (sys.executable, '-c', script) |
|---|
| 219 | n/a | proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave) |
|---|
| 220 | n/a | os.close(slave) |
|---|
| 221 | n/a | with ExitStack() as cleanup: |
|---|
| 222 | n/a | cleanup.enter_context(proc) |
|---|
| 223 | n/a | def terminate(proc): |
|---|
| 224 | n/a | try: |
|---|
| 225 | n/a | proc.terminate() |
|---|
| 226 | n/a | except ProcessLookupError: |
|---|
| 227 | n/a | # Workaround for Open/Net BSD bug (Issue 16762) |
|---|
| 228 | n/a | pass |
|---|
| 229 | n/a | cleanup.callback(terminate, proc) |
|---|
| 230 | n/a | cleanup.callback(os.close, master) |
|---|
| 231 | n/a | # Avoid using DefaultSelector and PollSelector. Kqueue() does not |
|---|
| 232 | n/a | # work with pseudo-terminals on OS X < 10.9 (Issue 20365) and Open |
|---|
| 233 | n/a | # BSD (Issue 20667). Poll() does not work with OS X 10.6 or 10.4 |
|---|
| 234 | n/a | # either (Issue 20472). Hopefully the file descriptor is low enough |
|---|
| 235 | n/a | # to use with select(). |
|---|
| 236 | n/a | sel = cleanup.enter_context(selectors.SelectSelector()) |
|---|
| 237 | n/a | sel.register(master, selectors.EVENT_READ | selectors.EVENT_WRITE) |
|---|
| 238 | n/a | os.set_blocking(master, False) |
|---|
| 239 | n/a | while True: |
|---|
| 240 | n/a | for [_, events] in sel.select(): |
|---|
| 241 | n/a | if events & selectors.EVENT_READ: |
|---|
| 242 | n/a | try: |
|---|
| 243 | n/a | chunk = os.read(master, 0x10000) |
|---|
| 244 | n/a | except OSError as err: |
|---|
| 245 | n/a | # Linux raises EIO when slave is closed (Issue 5380) |
|---|
| 246 | n/a | if err.errno != EIO: |
|---|
| 247 | n/a | raise |
|---|
| 248 | n/a | chunk = b"" |
|---|
| 249 | n/a | if not chunk: |
|---|
| 250 | n/a | return output |
|---|
| 251 | n/a | output.extend(chunk) |
|---|
| 252 | n/a | if events & selectors.EVENT_WRITE: |
|---|
| 253 | n/a | try: |
|---|
| 254 | n/a | input = input[os.write(master, input):] |
|---|
| 255 | n/a | except OSError as err: |
|---|
| 256 | n/a | # Apparently EIO means the slave was closed |
|---|
| 257 | n/a | if err.errno != EIO: |
|---|
| 258 | n/a | raise |
|---|
| 259 | n/a | input = b"" # Stop writing |
|---|
| 260 | n/a | if not input: |
|---|
| 261 | n/a | sel.modify(master, selectors.EVENT_READ) |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | if __name__ == "__main__": |
|---|
| 265 | n/a | unittest.main() |
|---|