@@ -66,3 +66,40 @@ def test_read_history_file(readline_wrapper, tmp_path):
6666 histfile .write_bytes (b"foo\n bar\n " )
6767 readline_wrapper .read_history_file (str (histfile ))
6868 assert readline_wrapper .reader .history == ["foo" , "bar" ]
69+
70+
71+ def test_write_history_file (readline_wrapper , tmp_path ):
72+ histfile = tmp_path / "history"
73+
74+ reader = readline_wrapper .get_reader ()
75+ history = reader .history
76+ assert history == []
77+ history .extend (["foo" , "bar" ])
78+
79+ readline_wrapper .write_history_file (str (histfile ))
80+
81+ assert open (str (histfile ), "r" ).readlines () == ["foo\n " , "bar\n " ]
82+
83+
84+ def test_write_history_file_with_exception (readline_wrapper , tmp_path ):
85+ """The history file should not get nuked on inner exceptions.
86+
87+ This was the case with unicode decoding previously."""
88+ histfile = tmp_path / "history"
89+ histfile .write_bytes (b"foo\n bar\n " )
90+
91+ class BadEntryException (Exception ):
92+ pass
93+
94+ class BadEntry (object ):
95+ @classmethod
96+ def replace (cls , * args ):
97+ raise BadEntryException
98+
99+ history = readline_wrapper .get_reader ().history
100+ history .extend ([BadEntry ])
101+
102+ with pytest .raises (BadEntryException ):
103+ readline_wrapper .write_history_file (str (histfile ))
104+
105+ assert open (str (histfile ), "r" ).readlines () == ["foo\n " , "bar\n " ]
0 commit comments