summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-06-14 11:05:24 -0400
committeranselor <anselor@gmail.com>2020-08-04 13:38:08 -0400
commit9ad174a173fdc610c661d588c1f42decbb3d7f5e (patch)
treed380a4101d126b0bdc40e49a81832bef06f2efef /tests
parentc88de7dfcfed716e81d06775b6e7929e4e01428c (diff)
downloadcmd2-git-9ad174a173fdc610c661d588c1f42decbb3d7f5e.tar.gz
Fixes issue with locating help_ annd complete_ functions when autoloading command functions
Adds handling of some edge cases. More thorough test coverage.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_commandset.py146
1 files changed, 136 insertions, 10 deletions
diff --git a/tests/test_commandset.py b/tests/test_commandset.py
index 02fff7b2..269c5de9 100644
--- a/tests/test_commandset.py
+++ b/tests/test_commandset.py
@@ -10,6 +10,11 @@ import pytest
import cmd2
from cmd2 import utils
+from .conftest import (
+ complete_tester,
+ run_cmd,
+)
+
# Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available
try:
@@ -41,14 +46,14 @@ def do_command_with_support(cmd: cmd2.Cmd, statement: cmd2.Statement):
:param statement:
:return:
"""
- cmd.poutput('Unbound Command: {}'.format(statement.args))
+ cmd.poutput('Command with support functions: {}'.format(statement.args))
def help_command_with_support(cmd: cmd2.Cmd):
cmd.poutput('Help for command_with_support')
-def complete_command_with_support(self, cmd: cmd2.Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]:
+def complete_command_with_support(cmd: cmd2.Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]:
"""Completion function for do_index_based"""
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
@@ -96,11 +101,29 @@ class WithCommandSets(cmd2.Cmd):
super().__init__(*args, **kwargs)
+@cmd2.with_default_category('Command Set B')
+class CommandSetB(cmd2.CommandSet):
+ def __init__(self, arg1):
+ super().__init__()
+ self._arg1 = arg1
+
+ def do_aardvark(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ cmd.poutput('Aardvark!')
+
+ def do_bat(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ """Banana Command"""
+ cmd.poutput('Bat!!')
+
+ def do_crocodile(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ cmd.poutput('Crocodile!!')
+
+
@pytest.fixture
def command_sets_app():
app = WithCommandSets()
return app
+
@pytest.fixture()
def command_sets_manual():
app = WithCommandSets(auto_load_commands=False)
@@ -120,28 +143,55 @@ def test_autoload_commands(command_sets_app):
assert 'cranberry' in cmds_cats['Command Set']
+def test_custom_construct_commandsets():
+ command_set = CommandSetB('foo')
+ app = WithCommandSets(command_sets=[command_set])
+
+ cmds_cats, cmds_doc, cmds_undoc, help_topics = app._build_command_info()
+ assert 'Command Set B' in cmds_cats
+
+ command_set_2 = CommandSetB('bar')
+ with pytest.raises(ValueError):
+ assert app.install_command_set(command_set_2)
+
+
def test_load_commands(command_sets_manual):
cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
- assert 'AAA' not in cmds_cats
+ # start by verifying none of the installable commands are present
+ assert 'AAA' not in cmds_cats
assert 'Alone' not in cmds_cats
-
assert 'Command Set' not in cmds_cats
- command_sets_manual.install_command_function('unbound', do_unbound, None, None)
+ # install the `unbound` command
+ command_sets_manual.install_registered_command('unbound')
+ with pytest.raises(KeyError):
+ assert command_sets_manual.install_registered_command('unbound')
+
+ with pytest.raises(KeyError):
+ assert command_sets_manual.install_registered_command('nonexistent_command')
+
+ def do_unbound(cmd: cmd2.Cmd, statement: cmd2.Statement):
+ """
+ This function duplicates an existing command
+ """
+ cmd.poutput('Unbound Command: {}'.format(statement.args))
+
+ with pytest.raises(KeyError):
+ assert cmd2.register_command(do_unbound)
+
+ # verify only the `unbound` command was installed
cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
assert 'AAA' in cmds_cats
assert 'unbound' in cmds_cats['AAA']
-
assert 'Alone' not in cmds_cats
-
assert 'Command Set' not in cmds_cats
+ # now install a command set and verify the commands are now present
cmd_set = CommandSetA()
-
command_sets_manual.install_command_set(cmd_set)
cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
@@ -155,6 +205,7 @@ def test_load_commands(command_sets_manual):
assert 'Command Set' in cmds_cats
assert 'cranberry' in cmds_cats['Command Set']
+ # uninstall the `unbound` command and verify only it was uninstalled
command_sets_manual.uninstall_command('unbound')
cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
@@ -167,12 +218,87 @@ def test_load_commands(command_sets_manual):
assert 'Command Set' in cmds_cats
assert 'cranberry' in cmds_cats['Command Set']
+ # uninstall the command set and verify it is now also no longer accessible
command_sets_manual.uninstall_command_set(cmd_set)
cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
assert 'AAA' not in cmds_cats
-
assert 'Alone' not in cmds_cats
-
assert 'Command Set' not in cmds_cats
+
+ # reinstall the command set and verifyt is accessible but the `unbound` command isn't
+ command_sets_manual.install_command_set(cmd_set)
+
+ cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
+
+ assert 'AAA' not in cmds_cats
+
+ assert 'Alone' in cmds_cats
+ assert 'elderberry' in cmds_cats['Alone']
+
+ assert 'Command Set' in cmds_cats
+ assert 'cranberry' in cmds_cats['Command Set']
+
+
+def test_command_functions(command_sets_manual):
+ cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
+ assert 'AAA' not in cmds_cats
+
+ out, err = run_cmd(command_sets_manual, 'command_with_support')
+ assert 'is not a recognized command, alias, or macro' in err[0]
+
+ out, err = run_cmd(command_sets_manual, 'help command_with_support')
+ assert 'No help on command_with_support' in err[0]
+
+ text = ''
+ line = 'command_with_support'
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
+ assert first_match is None
+
+ command_sets_manual.install_registered_command('command_with_support')
+
+ cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
+ assert 'AAA' in cmds_cats
+ assert 'command_with_support' in cmds_cats['AAA']
+
+ out, err = run_cmd(command_sets_manual, 'command_with_support')
+ assert 'Command with support functions' in out[0]
+
+ out, err = run_cmd(command_sets_manual, 'help command_with_support')
+ assert 'Help for command_with_support' in out[0]
+
+ first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
+ assert first_match == 'Ham'
+
+ text = ''
+ line = 'command_with_support Ham'
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
+
+ assert first_match == 'Basket'
+
+ command_sets_manual.uninstall_command('command_with_support')
+
+ cmds_cats, cmds_doc, cmds_undoc, help_topics = command_sets_manual._build_command_info()
+ assert 'AAA' not in cmds_cats
+
+ out, err = run_cmd(command_sets_manual, 'command_with_support')
+ assert 'is not a recognized command, alias, or macro' in err[0]
+
+ out, err = run_cmd(command_sets_manual, 'help command_with_support')
+ assert 'No help on command_with_support' in err[0]
+
+ text = ''
+ line = 'command_with_support'
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ first_match = complete_tester(text, line, begidx, endidx, command_sets_manual)
+ assert first_match is None
+