From e87d1cafd26d71a932a92b984b8b04ec174f185a Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 16 Sep 2024 14:43:11 -0400 Subject: [PATCH] Increased code coverage in history tests and updated change log. --- CHANGELOG.md | 1 + tests/test_history.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 378fffcd..3f26178d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * add `allow_clipboard` initialization parameter and attribute to disable ability to add output to the operating system clipboard * Updated unit tests to be Python 3.12 compliant. + * Fall back to bz2 compression of history file when lzma is not installed. * Deletions (potentially breaking changes) * Removed `apply_style` from `Cmd.pwarning()`. diff --git a/tests/test_history.py b/tests/test_history.py index ceaa9f83..99069802 100755 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -903,7 +903,34 @@ def test_history_file_permission_error(mocker, capsys): cmd2.Cmd(persistent_history_file='/tmp/doesntmatter') out, err = capsys.readouterr() assert not out - assert 'Cannot read' in err + assert 'Cannot read persistent history file' in err + + +def test_history_file_bad_compression(mocker, capsys): + history_file = '/tmp/doesntmatter' + with open(history_file, "wb") as f: + f.write(b"THIS IS NOT COMPRESSED DATA") + + cmd2.Cmd(persistent_history_file=history_file) + out, err = capsys.readouterr() + assert not out + assert 'Error decompressing persistent history data' in err + + +def test_history_file_bad_json(mocker, capsys): + import lzma + + data = b"THIS IS NOT JSON" + compressed_data = lzma.compress(data) + + history_file = '/tmp/doesntmatter' + with open(history_file, "wb") as f: + f.write(compressed_data) + + cmd2.Cmd(persistent_history_file=history_file) + out, err = capsys.readouterr() + assert not out + assert 'Error processing persistent history data' in err def test_history_populates_readline(hist_file): @@ -960,4 +987,4 @@ def test_persist_history_permission_error(hist_file, mocker, capsys): app._persist_history() out, err = capsys.readouterr() assert not out - assert 'Cannot write' in err + assert 'Cannot write persistent history file' in err