summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-07-16 15:15:51 (GMT)
committerGitHub <noreply@github.com>2024-07-16 15:15:51 (GMT)
commit7c9861f9d98975e12cefed40708650742efccb2c (patch)
tree2856a59ae81d6795e6795a7955d2f272e84f02bb
parente64a9db73dd51c72064ef462b19146e9b9419633 (diff)
downloadcpython-7c9861f9d98975e12cefed40708650742efccb2c.zip
cpython-7c9861f9d98975e12cefed40708650742efccb2c.tar.gz
cpython-7c9861f9d98975e12cefed40708650742efccb2c.tar.bz2
[3.13] gh-121160: Add some tests for readline.set_history_length (GH-121326) (GH-121856)
(cherry picked from commit 263c7e611bb24715e513d457a3477a61fff15162) Co-authored-by: Petr Viktorin <encukou@gmail.com>
-rw-r--r--Lib/test/test_readline.py46
-rw-r--r--Misc/NEWS.d/next/Tests/2024-07-03-14-41-00.gh-issue-121160.LEtiTd.rst2
2 files changed, 48 insertions, 0 deletions
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
index 5e0e6f8..91fd7dd 100644
--- a/Lib/test/test_readline.py
+++ b/Lib/test/test_readline.py
@@ -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()
diff --git a/Misc/NEWS.d/next/Tests/2024-07-03-14-41-00.gh-issue-121160.LEtiTd.rst b/Misc/NEWS.d/next/Tests/2024-07-03-14-41-00.gh-issue-121160.LEtiTd.rst
new file mode 100644
index 0000000..2c8c9ac
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2024-07-03-14-41-00.gh-issue-121160.LEtiTd.rst
@@ -0,0 +1,2 @@
+Add a test for :func:`readline.set_history_length`. Note that this test may
+fail on readline libraries.