Skip to content

Commit 7c9861f

Browse files
[3.13] gh-121160: Add some tests for readline.set_history_length (GH-121326) (GH-121856)
(cherry picked from commit 263c7e6) Co-authored-by: Petr Viktorin <[email protected]>
1 parent e64a9db commit 7c9861f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Lib/test/test_readline.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,32 @@ def test_nonascii_history(self):
132132
self.assertEqual(readline.get_history_item(1), "entrée 1")
133133
self.assertEqual(readline.get_history_item(2), "entrée 22")
134134

135+
def test_write_read_limited_history(self):
136+
previous_length = readline.get_history_length()
137+
self.addCleanup(readline.set_history_length, previous_length)
138+
139+
readline.clear_history()
140+
readline.add_history("first line")
141+
readline.add_history("second line")
142+
readline.add_history("third line")
143+
144+
readline.set_history_length(2)
145+
self.assertEqual(readline.get_history_length(), 2)
146+
readline.write_history_file(TESTFN)
147+
self.addCleanup(os.remove, TESTFN)
148+
149+
readline.clear_history()
150+
self.assertEqual(readline.get_current_history_length(), 0)
151+
self.assertEqual(readline.get_history_length(), 2)
152+
153+
readline.read_history_file(TESTFN)
154+
self.assertEqual(readline.get_history_item(1), "second line")
155+
self.assertEqual(readline.get_history_item(2), "third line")
156+
self.assertEqual(readline.get_history_item(3), None)
157+
158+
# Readline seems to report an additional history element.
159+
self.assertIn(readline.get_current_history_length(), (2, 3))
160+
135161

136162
class TestReadline(unittest.TestCase):
137163

@@ -323,6 +349,26 @@ def test_history_size(self):
323349
self.assertEqual(len(lines), history_size)
324350
self.assertEqual(lines[-1].strip(), b"last input")
325351

352+
def test_write_read_limited_history(self):
353+
previous_length = readline.get_history_length()
354+
self.addCleanup(readline.set_history_length, previous_length)
355+
356+
readline.add_history("first line")
357+
readline.add_history("second line")
358+
readline.add_history("third line")
359+
360+
readline.set_history_length(2)
361+
self.assertEqual(readline.get_history_length(), 2)
362+
readline.write_history_file(TESTFN)
363+
self.addCleanup(os.remove, TESTFN)
364+
365+
readline.read_history_file(TESTFN)
366+
# Without clear_history() there's no good way to test if
367+
# the correct entries are present (we're combining history limiting and
368+
# possible deduplication with arbitrary previous content).
369+
# So, we've only tested that the read did not fail.
370+
# See TestHistoryManipulation for the full test.
371+
326372

327373
if __name__ == "__main__":
328374
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add a test for :func:`readline.set_history_length`. Note that this test may
2+
fail on readline libraries.

0 commit comments

Comments
 (0)