[3.13] gh-121160: Add some tests for readline.set_history_length (GH-121326) (GH-121856)

(cherry picked from commit 263c7e611b)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-07-16 17:15:51 +02:00 committed by GitHub
parent e64a9db73d
commit 7c9861f9d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 0 deletions

View file

@ -132,6 +132,32 @@ class TestHistoryManipulation (unittest.TestCase):
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):
@ -323,6 +349,26 @@ readline.write_history_file(history_file)
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()