@@ -1825,3 +1825,74 @@ def test_onecmd_raw_str_quit(base_app):
1825
1825
assert stop
1826
1826
assert out == ''
1827
1827
1828
+
1829
+ @pytest .fixture (scope = "session" )
1830
+ def hist_file ():
1831
+ fd , filename = tempfile .mkstemp (prefix = 'hist_file' , suffix = '.txt' )
1832
+ os .close (fd )
1833
+ yield filename
1834
+ # teardown code
1835
+ try :
1836
+ os .remove (filename )
1837
+ except FileNotFoundError :
1838
+ pass
1839
+
1840
+ def test_existing_history_file (hist_file , capsys ):
1841
+ import atexit
1842
+ import readline
1843
+
1844
+ # Create the history file before making cmd2 app
1845
+ with open (hist_file , 'w' ):
1846
+ pass
1847
+
1848
+ # Create a new cmd2 app
1849
+ app = cmd2 .Cmd (persistent_history_file = hist_file )
1850
+ out , err = capsys .readouterr ()
1851
+
1852
+ # Make sure there were no errors
1853
+ assert err == ''
1854
+
1855
+ # Unregister the call to write_history_file that cmd2 did
1856
+ atexit .unregister (readline .write_history_file )
1857
+
1858
+ # Remove created history file
1859
+ os .remove (hist_file )
1860
+
1861
+
1862
+ def test_new_history_file (hist_file , capsys ):
1863
+ import atexit
1864
+ import readline
1865
+
1866
+ # Remove any existing history file
1867
+ try :
1868
+ os .remove (hist_file )
1869
+ except OSError :
1870
+ pass
1871
+
1872
+ # Create a new cmd2 app
1873
+ app = cmd2 .Cmd (persistent_history_file = hist_file )
1874
+ out , err = capsys .readouterr ()
1875
+
1876
+ # Make sure there were no errors
1877
+ assert err == ''
1878
+
1879
+ # Unregister the call to write_history_file that cmd2 did
1880
+ atexit .unregister (readline .write_history_file )
1881
+
1882
+ # Remove created history file
1883
+ os .remove (hist_file )
1884
+
1885
+ def test_bad_history_file_path (capsys , request ):
1886
+ # Use a directory path as the history file
1887
+ test_dir = os .path .dirname (request .module .__file__ )
1888
+
1889
+ # Create a new cmd2 app
1890
+ app = cmd2 .Cmd (persistent_history_file = test_dir )
1891
+ out , err = capsys .readouterr ()
1892
+
1893
+ if sys .platform == 'win32' :
1894
+ # pyreadline masks the read exception. Therefore the bad path error occurs when trying to write the file.
1895
+ assert 'readline cannot write' in err
1896
+ else :
1897
+ # GNU readline raises an exception upon trying to read the directory as a file
1898
+ assert 'readline cannot read' in err
0 commit comments