diff options
Diffstat (limited to 'Lib/test/test_readline.py')
-rw-r--r-- | Lib/test/test_readline.py | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py index 0b2b0a5..e2063b1 100644 --- a/Lib/test/test_readline.py +++ b/Lib/test/test_readline.py @@ -2,8 +2,9 @@ Very minimal unittests for parts of the readline module. """ import os +import tempfile import unittest -from test.support import run_unittest, import_module +from test.support import import_module, unlink from test.script_helper import assert_python_ok # Skip tests if there is no readline module @@ -42,6 +43,45 @@ class TestHistoryManipulation (unittest.TestCase): self.assertEqual(readline.get_current_history_length(), 1) + @unittest.skipUnless(hasattr(readline, "append_history_file"), + "append_history not available") + def test_write_read_append(self): + hfile = tempfile.NamedTemporaryFile(delete=False) + hfile.close() + hfilename = hfile.name + self.addCleanup(unlink, hfilename) + + # test write-clear-read == nop + readline.clear_history() + readline.add_history("first line") + readline.add_history("second line") + readline.write_history_file(hfilename) + + readline.clear_history() + self.assertEqual(readline.get_current_history_length(), 0) + + readline.read_history_file(hfilename) + self.assertEqual(readline.get_current_history_length(), 2) + self.assertEqual(readline.get_history_item(1), "first line") + self.assertEqual(readline.get_history_item(2), "second line") + + # test append + readline.append_history_file(1, hfilename) + readline.clear_history() + readline.read_history_file(hfilename) + self.assertEqual(readline.get_current_history_length(), 3) + self.assertEqual(readline.get_history_item(1), "first line") + self.assertEqual(readline.get_history_item(2), "second line") + self.assertEqual(readline.get_history_item(3), "second line") + + # test 'no such file' behaviour + os.unlink(hfilename) + with self.assertRaises(FileNotFoundError): + readline.append_history_file(1, hfilename) + + # write_history_file can create the target + readline.write_history_file(hfilename) + class TestReadline(unittest.TestCase): @@ -57,8 +97,5 @@ class TestReadline(unittest.TestCase): self.assertEqual(stdout, b'') -def test_main(): - run_unittest(TestHistoryManipulation, TestReadline) - if __name__ == "__main__": - test_main() + unittest.main() |