1313import pytest
1414
1515import cmd2
16- from cmd2 .history import HistoryFormatError , HistoryIndexError
16+ from cmd2 .history import History , HistoryFormatError , HistoryIndexError
1717from cmd2 .parsing import Statement
1818
1919from .conftest import (
@@ -1745,26 +1745,6 @@ def test_history_populates_pt(hist_file) -> None:
17451745# we assume that the atexit module will call this method
17461746# properly
17471747#
1748- def test_persist_history_ensure_no_error_if_no_histfile (base_app , capsys ) -> None :
1749- # make sure if there is no persistent history file and someone
1750- # calls the private method call that we don't get an error
1751- base_app ._persist_history ()
1752- out , err = capsys .readouterr ()
1753- assert not out
1754- assert not err
1755-
1756-
1757- def test_persist_history_permission_error (hist_file , mocker , capsys ) -> None :
1758- app = cmd2 .Cmd (persistent_history_file = hist_file )
1759- run_cmd (app , "help" )
1760- mock_open = mocker .patch ("builtins.open" )
1761- mock_open .side_effect = PermissionError
1762- app ._persist_history ()
1763- out , err = capsys .readouterr ()
1764- assert not out
1765- assert "Cannot write persistent history file" in err
1766-
1767-
17681748@pytest .fixture
17691749def persistence_app (base_app , hist , tmp_path ):
17701750 base_app .history = hist
@@ -1774,15 +1754,10 @@ def persistence_app(base_app, hist, tmp_path):
17741754
17751755
17761756def read_persisted_history (app ):
1777- try :
1778- import lzma as compress_lib
1779- except ModuleNotFoundError :
1780- import bz2 as compress_lib
1781-
1782- from cmd2 .history import History
1757+ import lzma
17831758
17841759 with open (app .persistent_history_file , "rb" ) as stream :
1785- return History .from_json (compress_lib .decompress (stream .read ()).decode ("utf-8" ))
1760+ return History .from_json (lzma .decompress (stream .read ()).decode ("utf-8" ))
17861761
17871762
17881763def history_state (app ):
@@ -1792,11 +1767,33 @@ def history_state(app):
17921767 tuple (id (item ) for item in history ),
17931768 history .to_json (),
17941769 history .session_start_index ,
1795- history .span (":" ),
1796- history .str_search ("third" ),
17971770 )
17981771
17991772
1773+ def test_persist_history_ensure_no_error_if_no_histfile (base_app , capsys ) -> None :
1774+ # make sure if there is no persistent history file and someone
1775+ # calls the private method call that we don't get an error
1776+ base_app ._persist_history ()
1777+ out , err = capsys .readouterr ()
1778+ assert not out
1779+ assert not err
1780+
1781+
1782+ def test_persist_history_permission_error (hist_file , mocker , capsys ) -> None :
1783+ app = cmd2 .Cmd (persistent_history_file = hist_file )
1784+ run_cmd (app , "help" )
1785+ run_cmd (app , "shortcuts" )
1786+ app ._persistent_history_length = 1
1787+ before = history_state (app )
1788+ mock_open = mocker .patch ("builtins.open" )
1789+ mock_open .side_effect = PermissionError
1790+ app ._persist_history ()
1791+ out , err = capsys .readouterr ()
1792+ assert not out
1793+ assert "Cannot write persistent history file" in err
1794+ assert history_state (app ) == before
1795+
1796+
18001797@pytest .mark .parametrize ("limit" , [- 1 , 0 , 1 , 2 , 4 , 6 ])
18011798def test_persist_history_preserves_live_state (persistence_app , limit ) -> None :
18021799 app = persistence_app
@@ -1810,17 +1807,7 @@ def test_persist_history_preserves_live_state(persistence_app, limit) -> None:
18101807 assert history_state (app ) == before
18111808
18121809
1813- def test_persist_history_empty (persistence_app ) -> None :
1814- app = persistence_app
1815- app .history .clear ()
1816- app ._persistent_history_length = 2
1817- before = history_state (app )
1818- app ._persist_history ()
1819- assert not read_persisted_history (app )
1820- assert history_state (app ) == before
1821-
1822-
1823- def test_persist_history_repeat_append_and_reload (persistence_app , mocker ) -> None :
1810+ def test_persist_history_repeat_and_append (persistence_app ) -> None :
18241811 app = persistence_app
18251812 app ._persistent_history_length = 2
18261813 before = history_state (app )
@@ -1833,36 +1820,3 @@ def test_persist_history_repeat_append_and_reload(persistence_app, mocker) -> No
18331820 app ._persist_history ()
18341821 assert [item .raw for item in read_persisted_history (app )] == ["fourth" , "fifth café" ]
18351822 assert history_state (app ) == before
1836- assert app .history .get (1 ).raw == "first"
1837- assert list (app .history .span (":" )) == [3 , 4 , 5 ]
1838-
1839- # The reload is temporary and must not register a later write to a deleted file.
1840- mocker .patch ("atexit.register" )
1841- reloaded = cmd2 .Cmd (persistent_history_file = app .persistent_history_file )
1842- assert [item .raw for item in reloaded .history ] == ["fourth" , "fifth café" ]
1843- assert reloaded .history .session_start_index == 2
1844- assert reloaded .main_session .history .get_strings () == ["fourth" , "fifth café" ]
1845- assert history_state (app ) == before
1846-
1847-
1848- def test_persist_history_no_file_preserves_live_state (persistence_app , mocker ) -> None :
1849- app = persistence_app
1850- app .persistent_history_file = ""
1851- app ._persistent_history_length = 1
1852- before = history_state (app )
1853- open_mock = mocker .patch ("builtins.open" )
1854- app ._persist_history ()
1855- open_mock .assert_not_called ()
1856- assert history_state (app ) == before
1857-
1858-
1859- def test_persist_history_write_error_preserves_live_state (persistence_app , mocker ) -> None :
1860- app = persistence_app
1861- app ._persistent_history_length = 1
1862- before = history_state (app )
1863- mocker .patch ("builtins.open" , side_effect = PermissionError ("synthetic write failure" ))
1864- error = mocker .patch .object (app , "perror" )
1865- app ._persist_history ()
1866- assert "Cannot write persistent history file" in error .call_args .args [0 ]
1867- assert "synthetic write failure" in error .call_args .args [0 ]
1868- assert history_state (app ) == before
0 commit comments