Skip to content

Commit 3292bf5

Browse files
committed
Added verify_help_text() helper function for tests and removed BASE_HELP and BASE_HELP_VERBOSE constants
The tests are now much more resilient to adding, removing, or renaming commands
1 parent f77c44d commit 3292bf5

File tree

3 files changed

+59
-138
lines changed

3 files changed

+59
-138
lines changed

tests/conftest.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
import sys
66
from contextlib import redirect_stdout, redirect_stderr
7-
from typing import Optional
7+
from typing import List, Optional, Union
88
from unittest import mock
99

1010
from pytest import fixture
@@ -25,31 +25,23 @@
2525
except ImportError:
2626
pass
2727

28-
# Help text for base cmd2.Cmd application
29-
BASE_HELP = """Documented commands (type help <topic>):
30-
========================================
31-
alias help load py quit run_script shell
32-
edit history macro pyscript run_pyscript set shortcuts
33-
""" # noqa: W291
34-
35-
BASE_HELP_VERBOSE = """
36-
Documented commands (type help <topic>):
37-
================================================================================
38-
alias Manage aliases
39-
edit Edit a file in a text editor
40-
help List available commands or provide detailed help for a specific command
41-
history View, run, edit, save, or clear previously entered commands
42-
load Run commands in script file that is encoded as either ASCII or UTF-8 text
43-
macro Manage macros
44-
py Invoke Python command or shell
45-
pyscript Run a Python script file inside the console
46-
quit Exit this application
47-
run_pyscript Run a Python script file inside the console
48-
run_script Run commands in script file that is encoded as either ASCII or UTF-8 text
49-
set Set a settable parameter or show current settings of parameters
50-
shell Execute a command as if at the OS prompt
51-
shortcuts List available shortcuts
52-
"""
28+
29+
def verify_help_text(cmd2_app: cmd2.Cmd, help_output: Union[str, List[str]]) -> None:
30+
"""This function verifies that all expected commands are present in the help text.
31+
32+
:param cmd2_app: instance of cmd2.Cmd
33+
:param help_output: output of help, either as a string or list of strings
34+
"""
35+
if isinstance(help_output, str):
36+
help_text = help_output
37+
else:
38+
help_text = ''.join(help_output)
39+
commands = cmd2_app.get_visible_commands()
40+
for command in commands:
41+
assert command in help_text
42+
43+
# TODO: Consider adding checks for categories and for verbose history
44+
5345

5446
# Help text for the history command
5547
HELP_HISTORY = """Usage: history [-h] [-r | -e | -o FILE | -t TRANSCRIPT | -c] [-s] [-x] [-v]

tests/test_cmd2.py

Lines changed: 38 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
import cmd2
2424
from cmd2 import clipboard, constants, utils
25-
from .conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \
26-
HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG
25+
from .conftest import run_cmd, normalize, verify_help_text, HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG
2726

2827
def CreateOutsimApp():
2928
c = cmd2.Cmd()
@@ -53,21 +52,20 @@ def test_empty_statement(base_app):
5352

5453
def test_base_help(base_app):
5554
out, err = run_cmd(base_app, 'help')
56-
expected = normalize(BASE_HELP)
57-
assert out == expected
55+
verify_help_text(base_app, out)
5856

5957
def test_base_help_verbose(base_app):
6058
out, err = run_cmd(base_app, 'help -v')
61-
expected = normalize(BASE_HELP_VERBOSE)
62-
assert out == expected
59+
verify_help_text(base_app, out)
6360

6461
# Make sure :param type lines are filtered out of help summary
6562
help_doc = base_app.do_help.__func__.__doc__
6663
help_doc += "\n:param fake param"
6764
base_app.do_help.__func__.__doc__ = help_doc
6865

6966
out, err = run_cmd(base_app, 'help --verbose')
70-
assert out == expected
67+
verify_help_text(base_app, out)
68+
assert ':param' not in ''.join(out)
7169

7270
def test_base_argparse_help(base_app):
7371
# Verify that "set -h" gives the same output as "help set" and that it starts in a way that makes sense
@@ -427,18 +425,17 @@ def test_output_redirection(base_app):
427425
try:
428426
# Verify that writing to a file works
429427
run_cmd(base_app, 'help > {}'.format(filename))
430-
expected = normalize(BASE_HELP)
431428
with open(filename) as f:
432-
content = normalize(f.read())
433-
assert content == expected
429+
content = f.read()
430+
verify_help_text(base_app, content)
434431

435432
# Verify that appending to a file also works
436433
run_cmd(base_app, 'help history >> {}'.format(filename))
437-
expected = normalize(BASE_HELP + '\n' + HELP_HISTORY)
438434
with open(filename) as f:
439-
content = normalize(f.read())
440-
assert content == expected
441-
except:
435+
appended_content = f.read()
436+
assert appended_content.startswith(content)
437+
assert len(appended_content) > len(content)
438+
except Exception:
442439
raise
443440
finally:
444441
os.remove(filename)
@@ -448,19 +445,18 @@ def test_output_redirection_to_nonexistent_directory(base_app):
448445

449446
# Verify that writing to a file in a non-existent directory doesn't work
450447
run_cmd(base_app, 'help > {}'.format(filename))
451-
expected = normalize(BASE_HELP)
452448
with pytest.raises(FileNotFoundError):
453449
with open(filename) as f:
454-
content = normalize(f.read())
455-
assert content == expected
450+
content = f.read()
451+
verify_help_text(base_app, content)
456452

457453
# Verify that appending to a file also works
458454
run_cmd(base_app, 'help history >> {}'.format(filename))
459-
expected = normalize(BASE_HELP + '\n' + HELP_HISTORY)
460455
with pytest.raises(FileNotFoundError):
461456
with open(filename) as f:
462-
content = normalize(f.read())
463-
assert content == expected
457+
appended_content = f.read()
458+
verify_help_text(base_app, appended_content)
459+
assert len(appended_content) > len(content)
464460

465461
def test_output_redirection_to_too_long_filename(base_app):
466462
filename = '~/sdkfhksdjfhkjdshfkjsdhfkjsdhfkjdshfkjdshfkjshdfkhdsfkjhewfuihewiufhweiufhiweufhiuewhiuewhfiuwehfia' \
@@ -471,19 +467,18 @@ def test_output_redirection_to_too_long_filename(base_app):
471467

472468
# Verify that writing to a file in a non-existent directory doesn't work
473469
run_cmd(base_app, 'help > {}'.format(filename))
474-
expected = normalize(BASE_HELP)
475470
with pytest.raises(OSError):
476471
with open(filename) as f:
477-
content = normalize(f.read())
478-
assert content == expected
472+
content = f.read()
473+
verify_help_text(base_app, content)
479474

480475
# Verify that appending to a file also works
481476
run_cmd(base_app, 'help history >> {}'.format(filename))
482-
expected = normalize(BASE_HELP + '\n' + HELP_HISTORY)
483477
with pytest.raises(OSError):
484478
with open(filename) as f:
485-
content = normalize(f.read())
486-
assert content == expected
479+
appended_content = f.read()
480+
verify_help_text(base_app, content)
481+
assert len(appended_content) > len(content)
487482

488483

489484
def test_feedback_to_output_true(base_app):
@@ -530,8 +525,7 @@ def test_disallow_redirection(base_app):
530525

531526
# Verify output wasn't redirected
532527
out, err = run_cmd(base_app, 'help > {}'.format(filename))
533-
expected = normalize(BASE_HELP)
534-
assert out == expected
528+
verify_help_text(base_app, out)
535529

536530
# Verify that no file got created
537531
assert not os.path.exists(filename)
@@ -574,13 +568,14 @@ def test_pipe_to_shell_error(base_app):
574568
def test_send_to_paste_buffer(base_app):
575569
# Test writing to the PasteBuffer/Clipboard
576570
run_cmd(base_app, 'help >')
577-
expected = normalize(BASE_HELP)
578-
assert normalize(cmd2.cmd2.get_paste_buffer()) == expected
571+
paste_contents = cmd2.cmd2.get_paste_buffer()
572+
verify_help_text(base_app, paste_contents)
579573

580574
# Test appending to the PasteBuffer/Clipboard
581575
run_cmd(base_app, 'help history >>')
582-
expected = normalize(BASE_HELP + '\n' + HELP_HISTORY)
583-
assert normalize(cmd2.cmd2.get_paste_buffer()) == expected
576+
appended_contents = cmd2.cmd2.get_paste_buffer()
577+
assert appended_contents.startswith(paste_contents)
578+
assert len(appended_contents) > len(paste_contents)
584579

585580

586581
def test_base_timing(base_app):
@@ -901,17 +896,7 @@ def test_custom_command_help(help_app):
901896

902897
def test_custom_help_menu(help_app):
903898
out, err = run_cmd(help_app, 'help')
904-
expected = normalize("""
905-
Documented commands (type help <topic>):
906-
========================================
907-
alias help load py quit run_script shell squat
908-
edit history macro pyscript run_pyscript set shortcuts
909-
910-
Undocumented commands:
911-
======================
912-
undoc
913-
""")
914-
assert out == expected
899+
verify_help_text(help_app, out)
915900

916901
def test_help_undocumented(help_app):
917902
out, err = run_cmd(help_app, 'help undoc')
@@ -962,62 +947,11 @@ def helpcat_app():
962947

963948
def test_help_cat_base(helpcat_app):
964949
out, err = run_cmd(helpcat_app, 'help')
965-
expected = normalize("""Documented commands (type help <topic>):
966-
967-
Custom Category
968-
===============
969-
edit squat
970-
971-
Some Category
972-
=============
973-
cat_nodoc diddly
974-
975-
Other
976-
=====
977-
alias history macro pyscript run_pyscript set shortcuts
978-
help load py quit run_script shell
979-
980-
Undocumented commands:
981-
======================
982-
undoc
983-
""")
984-
assert out == expected
950+
verify_help_text(helpcat_app, out)
985951

986952
def test_help_cat_verbose(helpcat_app):
987953
out, err = run_cmd(helpcat_app, 'help --verbose')
988-
expected = normalize("""Documented commands (type help <topic>):
989-
990-
Custom Category
991-
================================================================================
992-
edit This overrides the edit command and does nothing.
993-
squat This command does diddly squat...
994-
995-
Some Category
996-
================================================================================
997-
cat_nodoc
998-
diddly This command does diddly
999-
1000-
Other
1001-
================================================================================
1002-
alias Manage aliases
1003-
help List available commands or provide detailed help for a specific command
1004-
history View, run, edit, save, or clear previously entered commands
1005-
load Run commands in script file that is encoded as either ASCII or UTF-8 text
1006-
macro Manage macros
1007-
py Invoke Python command or shell
1008-
pyscript Run a Python script file inside the console
1009-
quit Exit this application
1010-
run_pyscript Run a Python script file inside the console
1011-
run_script Run commands in script file that is encoded as either ASCII or UTF-8 text
1012-
set Set a settable parameter or show current settings of parameters
1013-
shell Execute a command as if at the OS prompt
1014-
shortcuts List available shortcuts
1015-
1016-
Undocumented commands:
1017-
======================
1018-
undoc
1019-
""")
1020-
assert out == expected
954+
verify_help_text(helpcat_app, out)
1021955

1022956

1023957
class SelectApp(cmd2.Cmd):
@@ -1656,12 +1590,10 @@ def test_multiple_aliases(base_app):
16561590
run_cmd(base_app, 'alias create {} help'.format(alias1))
16571591
run_cmd(base_app, 'alias create {} help -v'.format(alias2))
16581592
out, err = run_cmd(base_app, alias1)
1659-
expected = normalize(BASE_HELP)
1660-
assert out == expected
1593+
verify_help_text(base_app, out)
16611594

16621595
out, err = run_cmd(base_app, alias2)
1663-
expected = normalize(BASE_HELP_VERBOSE)
1664-
assert out == expected
1596+
verify_help_text(base_app, out)
16651597

16661598
def test_macro_no_subcommand(base_app):
16671599
out, err = run_cmd(base_app, 'macro')
@@ -1716,8 +1648,7 @@ def test_macro_create_with_args(base_app):
17161648

17171649
# Run the macro
17181650
out, err = run_cmd(base_app, 'fake help -v')
1719-
expected = normalize(BASE_HELP_VERBOSE)
1720-
assert out == expected
1651+
verify_help_text(base_app, out)
17211652

17221653
def test_macro_create_with_escaped_args(base_app):
17231654
# Create the macro
@@ -1810,12 +1741,11 @@ def test_multiple_macros(base_app):
18101741
run_cmd(base_app, 'macro create {} help'.format(macro1))
18111742
run_cmd(base_app, 'macro create {} help -v'.format(macro2))
18121743
out, err = run_cmd(base_app, macro1)
1813-
expected = normalize(BASE_HELP)
1814-
assert out == expected
1744+
verify_help_text(base_app, out)
18151745

1816-
out, err = run_cmd(base_app, macro2)
1817-
expected = normalize(BASE_HELP_VERBOSE)
1818-
assert out == expected
1746+
out2, err2 = run_cmd(base_app, macro2)
1747+
verify_help_text(base_app, out2)
1748+
assert len(out2) > len(out)
18191749

18201750
def test_nonexistent_macro(base_app):
18211751
from cmd2.parsing import StatementParser
@@ -1878,7 +1808,7 @@ def test_onecmd_raw_str_continue(outsim_app):
18781808
stop = outsim_app.onecmd(line)
18791809
out = outsim_app.stdout.getvalue()
18801810
assert not stop
1881-
assert normalize(out) == normalize(BASE_HELP)
1811+
verify_help_text(outsim_app, out)
18821812

18831813
def test_onecmd_raw_str_quit(outsim_app):
18841814
line = "quit"

tests/test_transcript.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pytest
1515

1616
import cmd2
17-
from .conftest import run_cmd, BASE_HELP_VERBOSE
17+
from .conftest import run_cmd, verify_help_text
1818
from cmd2 import transcript
1919
from cmd2.utils import StdSim
2020

@@ -211,9 +211,8 @@ def test_run_script_record_transcript(base_app, request):
211211
with open(transcript_fname) as f:
212212
xscript = f.read()
213213

214-
expected = '(Cmd) help -v\n' + BASE_HELP_VERBOSE + '\n'
215-
216-
assert xscript == expected
214+
assert xscript.startswith('(Cmd) help -v\n')
215+
verify_help_text(base_app, xscript)
217216

218217

219218
def test_generate_transcript_stop(capsys):

0 commit comments

Comments
 (0)