summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test_parsing.py4
-rw-r--r--tests/test_plugin.py39
2 files changed, 40 insertions, 3 deletions
diff --git a/tests/test_parsing.py b/tests/test_parsing.py
index 435f22eb..5f363320 100755
--- a/tests/test_parsing.py
+++ b/tests/test_parsing.py
@@ -96,7 +96,7 @@ def test_tokenize(parser, line, tokens):
assert tokens_to_test == tokens
def test_tokenize_unclosed_quotes(parser):
- with pytest.raises(ValueError):
+ with pytest.raises(exceptions.Cmd2ShlexError):
_ = parser.tokenize('command with "unclosed quotes')
@pytest.mark.parametrize('tokens,command,args', [
@@ -583,7 +583,7 @@ def test_parse_redirect_to_unicode_filename(parser):
assert statement.output_to == 'café'
def test_parse_unclosed_quotes(parser):
- with pytest.raises(ValueError):
+ with pytest.raises(exceptions.Cmd2ShlexError):
_ = parser.tokenize("command with 'unclosed quotes")
def test_empty_statement_raises_exception():
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