blob: 35330ab0761029209afa2c87c67df6a077f5684b [file] [log] [blame]
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001"""
2Very minimal unittests for parts of the readline module.
Ronald Oussoren2efd9242009-09-20 14:53:22 +00003"""
Victor Stinnera3c80ce2014-07-24 12:23:56 +02004import os
Benjamin Peterson33f8f152014-11-26 13:58:16 -06005import tempfile
Ronald Oussoren2efd9242009-09-20 14:53:22 +00006import unittest
Zachary Ware38c707e2015-04-13 15:00:43 -05007from test.support import import_module, unlink
Berker Peksagce643912015-05-06 06:33:17 +03008from test.support.script_helper import assert_python_ok
Ronald Oussoren2efd9242009-09-20 14:53:22 +00009
Mark Dickinson2d7062e2009-10-26 12:01:06 +000010# Skip tests if there is no readline module
11readline = import_module('readline')
Ronald Oussoren2efd9242009-09-20 14:53:22 +000012
13class TestHistoryManipulation (unittest.TestCase):
Victor Stinnera3c80ce2014-07-24 12:23:56 +020014 """
15 These tests were added to check that the libedit emulation on OSX and the
16 "real" readline have the same interface for history manipulation. That's
17 why the tests cover only a small subset of the interface.
18 """
R David Murrayf8b9dfd2011-03-14 17:10:22 -040019
Benjamin Peterson5b535072014-11-26 14:35:56 -060020 @unittest.skipUnless(hasattr(readline, "clear_history"),
21 "The history update test cannot be run because the "
22 "clear_history method is not available.")
Ronald Oussoren2efd9242009-09-20 14:53:22 +000023 def testHistoryUpdates(self):
Georg Brandl7b250a52012-08-11 11:02:14 +020024 readline.clear_history()
Ronald Oussoren2efd9242009-09-20 14:53:22 +000025
26 readline.add_history("first line")
27 readline.add_history("second line")
28
29 self.assertEqual(readline.get_history_item(0), None)
30 self.assertEqual(readline.get_history_item(1), "first line")
31 self.assertEqual(readline.get_history_item(2), "second line")
32
33 readline.replace_history_item(0, "replaced line")
34 self.assertEqual(readline.get_history_item(0), None)
35 self.assertEqual(readline.get_history_item(1), "replaced line")
36 self.assertEqual(readline.get_history_item(2), "second line")
37
38 self.assertEqual(readline.get_current_history_length(), 2)
39
40 readline.remove_history_item(0)
41 self.assertEqual(readline.get_history_item(0), None)
42 self.assertEqual(readline.get_history_item(1), "second line")
43
44 self.assertEqual(readline.get_current_history_length(), 1)
45
Ned Deily8007cbc2014-11-26 13:02:33 -080046 @unittest.skipUnless(hasattr(readline, "append_history_file"),
Benjamin Petersond1e22ba2014-11-26 14:35:12 -060047 "append_history not available")
Benjamin Peterson33f8f152014-11-26 13:58:16 -060048 def test_write_read_append(self):
49 hfile = tempfile.NamedTemporaryFile(delete=False)
50 hfile.close()
51 hfilename = hfile.name
52 self.addCleanup(unlink, hfilename)
53
54 # test write-clear-read == nop
55 readline.clear_history()
56 readline.add_history("first line")
57 readline.add_history("second line")
58 readline.write_history_file(hfilename)
59
60 readline.clear_history()
61 self.assertEqual(readline.get_current_history_length(), 0)
62
63 readline.read_history_file(hfilename)
64 self.assertEqual(readline.get_current_history_length(), 2)
65 self.assertEqual(readline.get_history_item(1), "first line")
66 self.assertEqual(readline.get_history_item(2), "second line")
67
68 # test append
69 readline.append_history_file(1, hfilename)
70 readline.clear_history()
71 readline.read_history_file(hfilename)
72 self.assertEqual(readline.get_current_history_length(), 3)
73 self.assertEqual(readline.get_history_item(1), "first line")
74 self.assertEqual(readline.get_history_item(2), "second line")
75 self.assertEqual(readline.get_history_item(3), "second line")
76
77 # test 'no such file' behaviour
78 os.unlink(hfilename)
79 with self.assertRaises(FileNotFoundError):
80 readline.append_history_file(1, hfilename)
81
82 # write_history_file can create the target
83 readline.write_history_file(hfilename)
84
Ronald Oussoren2efd9242009-09-20 14:53:22 +000085
Victor Stinnera3c80ce2014-07-24 12:23:56 +020086class TestReadline(unittest.TestCase):
Antoine Pitrou7e8b8672014-11-04 14:52:10 +010087
88 @unittest.skipIf(readline._READLINE_VERSION < 0x0600
89 and "libedit" not in readline.__doc__,
90 "not supported in this library version")
Victor Stinnera3c80ce2014-07-24 12:23:56 +020091 def test_init(self):
92 # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
93 # written into stdout when the readline module is imported and stdout
94 # is redirected to a pipe.
95 rc, stdout, stderr = assert_python_ok('-c', 'import readline',
96 TERM='xterm-256color')
97 self.assertEqual(stdout, b'')
98
99
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000100if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500101 unittest.main()