summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/cmd2.py3
-rwxr-xr-xtests/test_history.py13
2 files changed, 15 insertions, 1 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index ab72f1a6..51670235 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3993,6 +3993,9 @@ class Cmd(cmd.Cmd):
os.remove(self.persistent_history_file)
except FileNotFoundError:
pass
+ except OSError as ex:
+ self.pexcept("Error removing history file '{}': {}".format(self.persistent_history_file, ex))
+ return
if rl_type != RlType.NONE:
readline.clear_history()
diff --git a/tests/test_history.py b/tests/test_history.py
index ac8c6cb6..3021109c 100755
--- a/tests/test_history.py
+++ b/tests/test_history.py
@@ -520,7 +520,7 @@ def test_history_run_one_command(base_app):
out2, err2 = run_cmd(base_app, 'history -r 1')
assert out1 == out2
-def test_history_clear(hist_file):
+def test_history_clear(mocker, hist_file):
# Add commands to history
app = cmd2.Cmd(persistent_history_file=hist_file)
run_cmd(app, 'help')
@@ -538,6 +538,17 @@ def test_history_clear(hist_file):
assert out == []
assert not os.path.exists(hist_file)
+ # Clear the history again and make sure the FileNotFoundError from trying to delete missing history file is silent
+ run_cmd(app, 'history --clear')
+
+ # Cause os.remove to fail and make sure error gets printed
+ mock_remove = mocker.patch('os.remove')
+ mock_remove.side_effect = OSError
+
+ out, err = run_cmd(app, 'history --clear')
+ assert out == []
+ assert 'Error removing history file' in err[0]
+
def test_history_verbose_with_other_options(base_app):
# make sure -v shows a usage error if any other options are present
options_to_test = ['-r', '-e', '-o file', '-t file', '-c', '-x']