@@ -132,6 +132,32 @@ def test_nonascii_history(self):
132
132
self .assertEqual (readline .get_history_item (1 ), "entrée 1" )
133
133
self .assertEqual (readline .get_history_item (2 ), "entrée 22" )
134
134
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
+
135
161
136
162
class TestReadline (unittest .TestCase ):
137
163
@@ -320,6 +346,26 @@ def test_history_size(self):
320
346
self .assertEqual (len (lines ), history_size )
321
347
self .assertEqual (lines [- 1 ].strip (), b"last input" )
322
348
349
+ def test_write_read_limited_history (self ):
350
+ previous_length = readline .get_history_length ()
351
+ self .addCleanup (readline .set_history_length , previous_length )
352
+
353
+ readline .add_history ("first line" )
354
+ readline .add_history ("second line" )
355
+ readline .add_history ("third line" )
356
+
357
+ readline .set_history_length (2 )
358
+ self .assertEqual (readline .get_history_length (), 2 )
359
+ readline .write_history_file (TESTFN )
360
+ self .addCleanup (os .remove , TESTFN )
361
+
362
+ readline .read_history_file (TESTFN )
363
+ # Without clear_history() there's no good way to test if
364
+ # the correct entries are present (we're combining history limiting and
365
+ # possible deduplication with arbitrary previous content).
366
+ # So, we've only tested that the read did not fail.
367
+ # See TestHistoryManipulation for the full test.
368
+
323
369
324
370
if __name__ == "__main__" :
325
371
unittest .main ()
0 commit comments