Skip to content

Commit 30b30cd

Browse files
committed
Added self.last_result unit tests for the history command.
1 parent 7b32a19 commit 30b30cd

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

tests/test_history.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@
2020
)
2121

2222

23+
def verify_hi_last_result(app: cmd2.Cmd, expected_length: int) -> None:
24+
"""Verifies app.last_result when it contains a dictionary of HistoryItems"""
25+
assert len(app.last_result) == expected_length
26+
27+
# Make sure last_result items match their respective indexes in the real history
28+
for key in app.last_result:
29+
# Keys in app.last_result are 1-based indexes into the app.history list
30+
assert app.last_result[key] == app.history[key - 1]
31+
32+
2333
#
2434
# readline tests
2535
#
26-
def test_readline_remove_history_item(base_app):
36+
def test_readline_remove_history_item():
2737
from cmd2.rl_utils import (
2838
readline,
2939
)
@@ -475,6 +485,7 @@ def test_base_history(base_app):
475485
"""
476486
)
477487
assert out == expected
488+
verify_hi_last_result(base_app, 1)
478489

479490
out, err = run_cmd(base_app, 'history sh')
480491
expected = normalize(
@@ -483,6 +494,7 @@ def test_base_history(base_app):
483494
"""
484495
)
485496
assert out == expected
497+
verify_hi_last_result(base_app, 1)
486498

487499

488500
def test_history_script_format(base_app):
@@ -496,6 +508,7 @@ def test_history_script_format(base_app):
496508
"""
497509
)
498510
assert out == expected
511+
verify_hi_last_result(base_app, 2)
499512

500513

501514
def test_history_with_string_argument(base_app):
@@ -510,6 +523,7 @@ def test_history_with_string_argument(base_app):
510523
"""
511524
)
512525
assert out == expected
526+
verify_hi_last_result(base_app, 2)
513527

514528

515529
def test_history_expanded_with_string_argument(base_app):
@@ -526,6 +540,7 @@ def test_history_expanded_with_string_argument(base_app):
526540
"""
527541
)
528542
assert out == expected
543+
verify_hi_last_result(base_app, 2)
529544

530545

531546
def test_history_expanded_with_regex_argument(base_app):
@@ -542,6 +557,7 @@ def test_history_expanded_with_regex_argument(base_app):
542557
"""
543558
)
544559
assert out == expected
560+
verify_hi_last_result(base_app, 2)
545561

546562

547563
def test_history_with_integer_argument(base_app):
@@ -554,6 +570,7 @@ def test_history_with_integer_argument(base_app):
554570
"""
555571
)
556572
assert out == expected
573+
verify_hi_last_result(base_app, 1)
557574

558575

559576
def test_history_with_integer_span(base_app):
@@ -568,6 +585,7 @@ def test_history_with_integer_span(base_app):
568585
"""
569586
)
570587
assert out == expected
588+
verify_hi_last_result(base_app, 2)
571589

572590

573591
def test_history_with_span_start(base_app):
@@ -582,6 +600,7 @@ def test_history_with_span_start(base_app):
582600
"""
583601
)
584602
assert out == expected
603+
verify_hi_last_result(base_app, 2)
585604

586605

587606
def test_history_with_span_end(base_app):
@@ -596,6 +615,7 @@ def test_history_with_span_end(base_app):
596615
"""
597616
)
598617
assert out == expected
618+
verify_hi_last_result(base_app, 2)
599619

600620

601621
def test_history_with_span_index_error(base_app):
@@ -616,6 +636,8 @@ def test_history_output_file():
616636
fd, fname = tempfile.mkstemp(prefix='', suffix='.txt')
617637
os.close(fd)
618638
run_cmd(app, 'history -o "{}"'.format(fname))
639+
assert app.last_result is True
640+
619641
expected = normalize('\n'.join(['help', 'shortcuts', 'help history', 'alias create my_alias history;']))
620642
with open(fname) as f:
621643
content = normalize(f.read())
@@ -632,6 +654,7 @@ def test_history_bad_output_file(base_app):
632654

633655
assert not out
634656
assert "Error saving" in err[0]
657+
assert base_app.last_result is False
635658

636659

637660
def test_history_edit(monkeypatch):
@@ -664,17 +687,17 @@ def test_history_run_all_commands(base_app):
664687
# make sure we refuse to run all commands as a default
665688
run_cmd(base_app, 'shortcuts')
666689
out, err = run_cmd(base_app, 'history -r')
667-
# this should generate an error, but we don't currently have a way to
668-
# capture stderr in these tests. So we assume that if we got nothing on
669-
# standard out, that the error occurred because if the command executed
670-
# then we should have a list of shortcuts in our output
671-
assert out == []
690+
691+
assert not out
692+
assert err[0].startswith("Cowardly refusing to run all")
693+
assert base_app.last_result is False
672694

673695

674696
def test_history_run_one_command(base_app):
675697
out1, err1 = run_cmd(base_app, 'help')
676698
out2, err2 = run_cmd(base_app, 'history -r 1')
677699
assert out1 == out2
700+
assert base_app.last_result is True
678701

679702

680703
def test_history_clear(mocker, hist_file):
@@ -686,17 +709,21 @@ def test_history_clear(mocker, hist_file):
686709
# Make sure history has items
687710
out, err = run_cmd(app, 'history')
688711
assert out
712+
verify_hi_last_result(app, 2)
689713

690714
# Clear the history
691715
run_cmd(app, 'history --clear')
716+
assert app.last_result is True
692717

693718
# Make sure history is empty and its file is gone
694719
out, err = run_cmd(app, 'history')
695720
assert out == []
696721
assert not os.path.exists(hist_file)
722+
verify_hi_last_result(app, 0)
697723

698724
# Clear the history again and make sure the FileNotFoundError from trying to delete missing history file is silent
699725
run_cmd(app, 'history --clear')
726+
assert app.last_result is True
700727

701728
# Cause os.remove to fail and make sure error gets printed
702729
mock_remove = mocker.patch('os.remove')
@@ -705,6 +732,7 @@ def test_history_clear(mocker, hist_file):
705732
out, err = run_cmd(app, 'history --clear')
706733
assert out == []
707734
assert 'Error removing history file' in err[0]
735+
assert app.last_result is False
708736

709737

710738
def test_history_verbose_with_other_options(base_app):
@@ -715,15 +743,24 @@ def test_history_verbose_with_other_options(base_app):
715743
assert len(out) == 4
716744
assert out[0] == '-v cannot be used with any other options'
717745
assert out[1].startswith('Usage:')
746+
assert base_app.last_result is False
718747

719748

720749
def test_history_verbose(base_app):
721750
# validate function of -v option
722751
run_cmd(base_app, 'alias create s shortcuts')
723752
run_cmd(base_app, 's')
724753
out, err = run_cmd(base_app, 'history -v')
725-
assert len(out) == 3
726-
# TODO test for basic formatting once we figure it out
754+
755+
expected = normalize(
756+
"""
757+
1 alias create s shortcuts
758+
2 s
759+
2x shortcuts
760+
"""
761+
)
762+
assert out == expected
763+
verify_hi_last_result(base_app, 2)
727764

728765

729766
def test_history_script_with_invalid_options(base_app):
@@ -734,6 +771,7 @@ def test_history_script_with_invalid_options(base_app):
734771
assert len(out) == 4
735772
assert out[0] == '-s and -x cannot be used with -c, -r, -e, -o, or -t'
736773
assert out[1].startswith('Usage:')
774+
assert base_app.last_result is False
737775

738776

739777
def test_history_script(base_app):
@@ -742,6 +780,7 @@ def test_history_script(base_app):
742780
run_cmd(base_app, cmd)
743781
out, err = run_cmd(base_app, 'history -s')
744782
assert out == cmds
783+
verify_hi_last_result(base_app, 2)
745784

746785

747786
def test_history_expanded_with_invalid_options(base_app):
@@ -752,6 +791,7 @@ def test_history_expanded_with_invalid_options(base_app):
752791
assert len(out) == 4
753792
assert out[0] == '-s and -x cannot be used with -c, -r, -e, -o, or -t'
754793
assert out[1].startswith('Usage:')
794+
assert base_app.last_result is False
755795

756796

757797
def test_history_expanded(base_app):
@@ -762,6 +802,7 @@ def test_history_expanded(base_app):
762802
out, err = run_cmd(base_app, 'history -x')
763803
expected = [' 1 alias create s shortcuts', ' 2 shortcuts']
764804
assert out == expected
805+
verify_hi_last_result(base_app, 2)
765806

766807

767808
def test_history_script_expanded(base_app):
@@ -772,20 +813,23 @@ def test_history_script_expanded(base_app):
772813
out, err = run_cmd(base_app, 'history -sx')
773814
expected = ['alias create s shortcuts', 'shortcuts']
774815
assert out == expected
816+
verify_hi_last_result(base_app, 2)
775817

776818

777819
def test_base_help_history(base_app):
778820
out, err = run_cmd(base_app, 'help history')
779821
assert out == normalize(HELP_HISTORY)
780822

781823

782-
def test_exclude_from_history(base_app, monkeypatch):
824+
def test_exclude_from_history(base_app):
783825
# Run history command
784826
run_cmd(base_app, 'history')
827+
verify_hi_last_result(base_app, 0)
785828

786829
# Verify that the history is empty
787830
out, err = run_cmd(base_app, 'history')
788831
assert out == []
832+
verify_hi_last_result(base_app, 0)
789833

790834
# Now run a command which isn't excluded from the history
791835
run_cmd(base_app, 'help')
@@ -794,6 +838,7 @@ def test_exclude_from_history(base_app, monkeypatch):
794838
out, err = run_cmd(base_app, 'history')
795839
expected = normalize(""" 1 help""")
796840
assert out == expected
841+
verify_hi_last_result(base_app, 1)
797842

798843

799844
#

0 commit comments

Comments
 (0)