|
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
|
@@ -195,13 +195,50 @@ def display(substitution, matches, longest_match_length):
|
195 | 195 | self.assertIn(b"result " + expected + b"\r\n", output)
|
196 | 196 | self.assertIn(b"history " + expected + b"\r\n", output)
|
197 | 197 |
|
| 198 | + @unittest.skipIf(is_editline, |
| 199 | + "editline history size configuration is broken") |
| 200 | + def test_history_size(self): |
| 201 | + history_size = 10 |
| 202 | + with temp_dir() as test_dir: |
| 203 | + inputrc = os.path.join(test_dir, "inputrc") |
| 204 | + with open(inputrc, "wb") as f: |
| 205 | + f.write(b"set history-size %d\n" % history_size) |
| 206 | + |
| 207 | + history_file = os.path.join(test_dir, "history") |
| 208 | + with open(history_file, "wb") as f: |
| 209 | + # history_size * 2 items crashes readline |
| 210 | + data = b"".join(b"item %d\n" % i |
| 211 | + for i in range(history_size * 2)) |
| 212 | + f.write(data) |
| 213 | + |
| 214 | + script = """ |
| 215 | +import os |
| 216 | +import readline |
| 217 | +
|
| 218 | +history_file = os.environ["HISTORY_FILE"] |
| 219 | +readline.read_history_file(history_file) |
| 220 | +input() |
| 221 | +readline.write_history_file(history_file) |
| 222 | +""" |
| 223 | + |
| 224 | + env = dict(os.environ) |
| 225 | + env["INPUTRC"] = inputrc |
| 226 | + env["HISTORY_FILE"] = history_file |
| 227 | + |
| 228 | + run_pty(script, input=b"last input\r", env=env) |
| 229 | + |
| 230 | + with open(history_file, "rb") as f: |
| 231 | + lines = f.readlines() |
| 232 | + self.assertEqual(len(lines), history_size) |
| 233 | + self.assertEqual(lines[-1].strip(), b"last input") |
| 234 | + |
198 | 235 |
|
199 |
| -def run_pty(script, input=b"dummy input\r"): |
| 236 | +def run_pty(script, input=b"dummy input\r", env=None): |
200 | 237 | pty = import_module('pty')
|
201 | 238 | output = bytearray()
|
202 | 239 | [master, slave] = pty.openpty()
|
203 | 240 | args = (sys.executable, '-c', script)
|
204 |
| - proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave) |
| 241 | + proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave, env=env) |
205 | 242 | os.close(slave)
|
206 | 243 | with ExitStack() as cleanup:
|
207 | 244 | cleanup.enter_context(proc)
|
|
0 commit comments