Skip to content

Commit a65b2da

Browse files
encukoumiss-islington
authored andcommitted
pythongh-121160: Add some tests for readline.set_history_length (pythonGH-121326)
(cherry picked from commit 263c7e6) Co-authored-by: Petr Viktorin <[email protected]>
1 parent 106d645 commit a65b2da

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

@@ -320,6 +346,26 @@ def test_history_size(self):
320346
self.assertEqual(len(lines), history_size)
321347
self.assertEqual(lines[-1].strip(), b"last input")
322348

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

324370
if __name__ == "__main__":
325371
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)