|
9 | 9 | import sys
|
10 | 10 | import tempfile
|
11 | 11 | import unittest
|
12 |
| -from test.support import import_module, unlink, TESTFN |
| 12 | +from test.support import import_module, unlink, temp_dir, TESTFN |
13 | 13 | from test.support.script_helper import assert_python_ok
|
14 | 14 |
|
15 | 15 | # Skip tests if there is no readline module
|
@@ -210,13 +210,50 @@ def display(substitution, matches, longest_match_length):
|
210 | 210 | self.assertIn(b"result " + expected + b"\r\n", output)
|
211 | 211 | self.assertIn(b"history " + expected + b"\r\n", output)
|
212 | 212 |
|
| 213 | + @unittest.skipIf(is_editline, |
| 214 | + "editline history size configuration is broken") |
| 215 | + def test_history_size(self): |
| 216 | + history_size = 10 |
| 217 | + with temp_dir() as test_dir: |
| 218 | + inputrc = os.path.join(test_dir, "inputrc") |
| 219 | + with open(inputrc, "wb") as f: |
| 220 | + f.write(b"set history-size %d\n" % history_size) |
| 221 | + |
| 222 | + history_file = os.path.join(test_dir, "history") |
| 223 | + with open(history_file, "wb") as f: |
| 224 | + # history_size * 2 items crashes readline |
| 225 | + data = b"".join(b"item %d\n" % i |
| 226 | + for i in range(history_size * 2)) |
| 227 | + f.write(data) |
| 228 | + |
| 229 | + script = """ |
| 230 | +import os |
| 231 | +import readline |
| 232 | +
|
| 233 | +history_file = os.environ["HISTORY_FILE"] |
| 234 | +readline.read_history_file(history_file) |
| 235 | +input() |
| 236 | +readline.write_history_file(history_file) |
| 237 | +""" |
| 238 | + |
| 239 | + env = dict(os.environ) |
| 240 | + env["INPUTRC"] = inputrc |
| 241 | + env["HISTORY_FILE"] = history_file |
| 242 | + |
| 243 | + run_pty(script, input=b"last input\r", env=env) |
| 244 | + |
| 245 | + with open(history_file, "rb") as f: |
| 246 | + lines = f.readlines() |
| 247 | + self.assertEqual(len(lines), history_size) |
| 248 | + self.assertEqual(lines[-1].strip(), b"last input") |
| 249 | + |
213 | 250 |
|
214 |
| -def run_pty(script, input=b"dummy input\r"): |
| 251 | +def run_pty(script, input=b"dummy input\r", env=None): |
215 | 252 | pty = import_module('pty')
|
216 | 253 | output = bytearray()
|
217 | 254 | [master, slave] = pty.openpty()
|
218 | 255 | args = (sys.executable, '-c', script)
|
219 |
| - proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave) |
| 256 | + proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave, env=env) |
220 | 257 | os.close(slave)
|
221 | 258 | with ExitStack() as cleanup:
|
222 | 259 | cleanup.enter_context(proc)
|
|
0 commit comments