Skip to content

Commit 9da34b2

Browse files
committed
Address review feedback on persisted history copy
Follow-up to #1796, applying review comments made before it merged. Comment why _persist_history() truncates a copy, so it is not later simplified back into truncating the live history. Trim the new tests. Drop the no-file, empty-history, and write-error cases, which either could not fail or duplicated existing tests, and instead assert unchanged live state in the existing permission-error test with a limit shorter than the history. Drop the reload half of the repeat/append test, which re-tested loading. Simplify the helpers: import lzma directly, move the History import to module level, and stop comparing span and search results that follow from the item identities and session boundary already compared.
1 parent 07a8f56 commit 9da34b2

2 files changed

Lines changed: 29 additions & 74 deletions

File tree

‎cmd2/cmd2.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5722,6 +5722,7 @@ def _persist_history(self) -> None:
57225722
except ModuleNotFoundError: # pragma: no cover
57235723
import bz2 as compress_lib # type: ignore[no-redef]
57245724

5725+
# Truncate a copy so live history numbering and the session boundary are unchanged.
57255726
history = History(self.history)
57265727
history.truncate(self._persistent_history_length)
57275728
history_json = history.to_json()

‎tests/test_history.py‎

Lines changed: 28 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pytest
1414

1515
import cmd2
16-
from cmd2.history import HistoryFormatError, HistoryIndexError
16+
from cmd2.history import History, HistoryFormatError, HistoryIndexError
1717
from cmd2.parsing import Statement
1818

1919
from .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
17691749
def 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

17761756
def 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

17881763
def 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])
18011798
def 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

Comments
 (0)