summaryrefslogtreecommitdiff
path: root/tests/test_plugin.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2020-03-13 08:10:51 -0400
committerGitHub <noreply@github.com>2020-03-13 08:10:51 -0400
commit77e2a0fab7620d22e0fde6be7374dbbf26706fd4 (patch)
tree7596897fa233204b7c7b84c94f26c71ccaf5ad5b /tests/test_plugin.py
parent59739aa5b6f253814fb019a9e777056a6efb61ca (diff)
parenta4160cfe9ab39402511c1a445f3b978099743bc9 (diff)
downloadcmd2-git-77e2a0fab7620d22e0fde6be7374dbbf26706fd4.tar.gz
Merge pull request #906 from python-cmd2/parsing_exception
Parsing exception
Diffstat (limited to 'tests/test_plugin.py')
-rw-r--r--tests/test_plugin.py39
1 files changed, 38 insertions, 1 deletions
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index c118b60d..bb7753f0 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -3,6 +3,7 @@
"""
Test plugin infrastructure and hooks.
"""
+import argparse
import sys
import pytest
@@ -14,7 +15,7 @@ except ImportError:
from unittest import mock
import cmd2
-from cmd2 import exceptions, plugin
+from cmd2 import exceptions, plugin, Cmd2ArgumentParser, with_argparser
class Plugin:
@@ -254,6 +255,14 @@ class PluggedApp(Plugin, cmd2.Cmd):
"""Repeat back the arguments"""
self.poutput(statement)
+ parser = Cmd2ArgumentParser(description="Test parser")
+ parser.add_argument("my_arg", help="some help text")
+
+ @with_argparser(parser)
+ def do_argparse_cmd(self, namespace: argparse.Namespace):
+ """Repeat back the arguments"""
+ self.poutput(namespace.__statement__)
+
###
#
# test pre and postloop hooks
@@ -836,3 +845,31 @@ def test_cmdfinalization_hook_exception(capsys):
assert out == 'hello\n'
assert err
assert app.called_cmdfinalization == 1
+
+
+def test_cmd2_argparse_exception(capsys):
+ """
+ Verify Cmd2ArgparseErrors raised after calling a command prevent postcmd events from
+ running but do not affect cmdfinalization events
+ """
+ app = PluggedApp()
+ app.register_postcmd_hook(app.postcmd_hook)
+ app.register_cmdfinalization_hook(app.cmdfinalization_hook)
+
+ # First generate no exception and make sure postcmd_hook, postcmd, and cmdfinalization_hook run
+ app.onecmd_plus_hooks('argparse_cmd arg_val')
+ out, err = capsys.readouterr()
+ assert out == 'arg_val\n'
+ assert not err
+ assert app.called_postcmd == 2
+ assert app.called_cmdfinalization == 1
+
+ app.reset_counters()
+
+ # Next cause an argparse exception and verify no postcmd stuff runs but cmdfinalization_hook still does
+ app.onecmd_plus_hooks('argparse_cmd')
+ out, err = capsys.readouterr()
+ assert not out
+ assert "Error: the following arguments are required: my_arg" in err
+ assert app.called_postcmd == 0
+ assert app.called_cmdfinalization == 1