@@ -132,6 +132,32 @@ def test_nonascii_history(self):
132132 self .assertEqual (readline .get_history_item (1 ), "entrée 1" )
133133 self .assertEqual (readline .get_history_item (2 ), "entrée 22" )
134134
135+ def test_write_read_limited_history (self ):
136+ previous_length = readline .get_history_length ()
137+ self .addCleanup (readline .set_history_length , previous_length )
138+
139+ readline .clear_history ()
140+ readline .add_history ("first line" )
141+ readline .add_history ("second line" )
142+ readline .add_history ("third line" )
143+
144+ readline .set_history_length (2 )
145+ self .assertEqual (readline .get_history_length (), 2 )
146+ readline .write_history_file (TESTFN )
147+ self .addCleanup (os .remove , TESTFN )
148+
149+ readline .clear_history ()
150+ self .assertEqual (readline .get_current_history_length (), 0 )
151+ self .assertEqual (readline .get_history_length (), 2 )
152+
153+ readline .read_history_file (TESTFN )
154+ self .assertEqual (readline .get_history_item (1 ), "second line" )
155+ self .assertEqual (readline .get_history_item (2 ), "third line" )
156+ self .assertEqual (readline .get_history_item (3 ), None )
157+
158+ # Readline seems to report an additional history element.
159+ self .assertIn (readline .get_current_history_length (), (2 , 3 ))
160+
135161
136162class TestReadline (unittest .TestCase ):
137163
@@ -323,6 +349,26 @@ def test_history_size(self):
323349 self .assertEqual (len (lines ), history_size )
324350 self .assertEqual (lines [- 1 ].strip (), b"last input" )
325351
352+ def test_write_read_limited_history (self ):
353+ previous_length = readline .get_history_length ()
354+ self .addCleanup (readline .set_history_length , previous_length )
355+
356+ readline .add_history ("first line" )
357+ readline .add_history ("second line" )
358+ readline .add_history ("third line" )
359+
360+ readline .set_history_length (2 )
361+ self .assertEqual (readline .get_history_length (), 2 )
362+ readline .write_history_file (TESTFN )
363+ self .addCleanup (os .remove , TESTFN )
364+
365+ readline .read_history_file (TESTFN )
366+ # Without clear_history() there's no good way to test if
367+ # the correct entries are present (we're combining history limiting and
368+ # possible deduplication with arbitrary previous content).
369+ # So, we've only tested that the read did not fail.
370+ # See TestHistoryManipulation for the full test.
371+
326372
327373if __name__ == "__main__" :
328374 unittest .main ()
0 commit comments