Skip to content

[3.12] gh-121160: Add some tests for readline.set_history_length (GH-121326) #121857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Lib/test/test_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,32 @@ def test_nonascii_history(self):
self.assertEqual(readline.get_history_item(1), "entrée 1")
self.assertEqual(readline.get_history_item(2), "entrée 22")

def test_write_read_limited_history(self):
previous_length = readline.get_history_length()
self.addCleanup(readline.set_history_length, previous_length)

readline.clear_history()
readline.add_history("first line")
readline.add_history("second line")
readline.add_history("third line")

readline.set_history_length(2)
self.assertEqual(readline.get_history_length(), 2)
readline.write_history_file(TESTFN)
self.addCleanup(os.remove, TESTFN)

readline.clear_history()
self.assertEqual(readline.get_current_history_length(), 0)
self.assertEqual(readline.get_history_length(), 2)

readline.read_history_file(TESTFN)
self.assertEqual(readline.get_history_item(1), "second line")
self.assertEqual(readline.get_history_item(2), "third line")
self.assertEqual(readline.get_history_item(3), None)

# Readline seems to report an additional history element.
self.assertIn(readline.get_current_history_length(), (2, 3))


class TestReadline(unittest.TestCase):

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

def test_write_read_limited_history(self):
previous_length = readline.get_history_length()
self.addCleanup(readline.set_history_length, previous_length)

readline.add_history("first line")
readline.add_history("second line")
readline.add_history("third line")

readline.set_history_length(2)
self.assertEqual(readline.get_history_length(), 2)
readline.write_history_file(TESTFN)
self.addCleanup(os.remove, TESTFN)

readline.read_history_file(TESTFN)
# Without clear_history() there's no good way to test if
# the correct entries are present (we're combining history limiting and
# possible deduplication with arbitrary previous content).
# So, we've only tested that the read did not fail.
# See TestHistoryManipulation for the full test.


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a test for :func:`readline.set_history_length`. Note that this test may
fail on readline libraries.
Loading