summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-27 16:55:30 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-27 16:55:30 -0400
commit2721f8a619ca4e5447a4507658f09d3ba4e10a14 (patch)
tree86de18ae834cc3bf57b782c403bdcd98426e0d5b
parent01e16caa4d2fd2733588ad3c1e48a186f22ad376 (diff)
downloadcmd2-git-2721f8a619ca4e5447a4507658f09d3ba4e10a14.tar.gz
Added unit tests
-rw-r--r--cmd2/cmd2.py2
-rw-r--r--tests/test_history.py27
2 files changed, 21 insertions, 8 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 0dcbcb36..55c303af 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3407,7 +3407,7 @@ class Cmd(cmd.Cmd):
fobj.write('{}\n'.format(item.raw))
plural = 's' if len(history) > 1 else ''
except OSError as e:
- self.pexcept('Saving {!r} - {}'.format(args.output_file, e))
+ self.pexcept('Error saving {!r} - {}'.format(args.output_file, e))
else:
self.pfeedback('{} command{} saved to {}'.format(len(history), plural, args.output_file))
elif args.transcript:
diff --git a/tests/test_history.py b/tests/test_history.py
index 973d8cff..add93ea6 100644
--- a/tests/test_history.py
+++ b/tests/test_history.py
@@ -461,6 +461,17 @@ def test_history_output_file(base_app):
content = normalize(f.read())
assert content == expected
+def test_history_bad_output_file(base_app):
+ run_cmd(base_app, 'help')
+ run_cmd(base_app, 'shortcuts')
+ run_cmd(base_app, 'help history')
+
+ fname = os.path.join(os.path.sep, "fake", "fake", "fake")
+ out, err = run_cmd(base_app, 'history -o "{}"'.format(fname))
+
+ assert not out
+ assert "Error saving" in err[0]
+
def test_history_edit(base_app, monkeypatch):
# Set a fake editor just to make sure we have one. We aren't really
# going to call it due to the mock
@@ -492,21 +503,23 @@ def test_history_run_one_command(base_app):
out2, err2 = run_cmd(base_app, 'history -r 1')
assert out1 == out2
-def test_history_clear(base_app):
+def test_history_clear(hist_file):
# Add commands to history
- run_cmd(base_app, 'help')
- run_cmd(base_app, 'alias')
+ app = cmd2.Cmd(persistent_history_file=hist_file)
+ run_cmd(app, 'help')
+ run_cmd(app, 'alias')
# Make sure history has items
- out, err = run_cmd(base_app, 'history')
+ out, err = run_cmd(app, 'history')
assert out
# Clear the history
- run_cmd(base_app, 'history --clear')
+ run_cmd(app, 'history --clear')
- # Make sure history is empty
- out, err = run_cmd(base_app, 'history')
+ # Make sure history is empty and its file is gone
+ out, err = run_cmd(app, 'history')
assert out == []
+ assert not os.path.exists(hist_file)
def test_history_verbose_with_other_options(base_app):
# make sure -v shows a usage error if any other options are present