Skip to content

Commit 2721f8a

Browse files
committed
Added unit tests
1 parent 01e16ca commit 2721f8a

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

cmd2/cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3407,7 +3407,7 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
34073407
fobj.write('{}\n'.format(item.raw))
34083408
plural = 's' if len(history) > 1 else ''
34093409
except OSError as e:
3410-
self.pexcept('Saving {!r} - {}'.format(args.output_file, e))
3410+
self.pexcept('Error saving {!r} - {}'.format(args.output_file, e))
34113411
else:
34123412
self.pfeedback('{} command{} saved to {}'.format(len(history), plural, args.output_file))
34133413
elif args.transcript:

tests/test_history.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,17 @@ def test_history_output_file(base_app):
461461
content = normalize(f.read())
462462
assert content == expected
463463

464+
def test_history_bad_output_file(base_app):
465+
run_cmd(base_app, 'help')
466+
run_cmd(base_app, 'shortcuts')
467+
run_cmd(base_app, 'help history')
468+
469+
fname = os.path.join(os.path.sep, "fake", "fake", "fake")
470+
out, err = run_cmd(base_app, 'history -o "{}"'.format(fname))
471+
472+
assert not out
473+
assert "Error saving" in err[0]
474+
464475
def test_history_edit(base_app, monkeypatch):
465476
# Set a fake editor just to make sure we have one. We aren't really
466477
# going to call it due to the mock
@@ -492,21 +503,23 @@ def test_history_run_one_command(base_app):
492503
out2, err2 = run_cmd(base_app, 'history -r 1')
493504
assert out1 == out2
494505

495-
def test_history_clear(base_app):
506+
def test_history_clear(hist_file):
496507
# Add commands to history
497-
run_cmd(base_app, 'help')
498-
run_cmd(base_app, 'alias')
508+
app = cmd2.Cmd(persistent_history_file=hist_file)
509+
run_cmd(app, 'help')
510+
run_cmd(app, 'alias')
499511

500512
# Make sure history has items
501-
out, err = run_cmd(base_app, 'history')
513+
out, err = run_cmd(app, 'history')
502514
assert out
503515

504516
# Clear the history
505-
run_cmd(base_app, 'history --clear')
517+
run_cmd(app, 'history --clear')
506518

507-
# Make sure history is empty
508-
out, err = run_cmd(base_app, 'history')
519+
# Make sure history is empty and its file is gone
520+
out, err = run_cmd(app, 'history')
509521
assert out == []
522+
assert not os.path.exists(hist_file)
510523

511524
def test_history_verbose_with_other_options(base_app):
512525
# make sure -v shows a usage error if any other options are present

0 commit comments

Comments
 (0)