diff options
author | xNinjaKittyx <xNinjaKittyx@users.noreply.github.com> | 2020-12-15 17:21:33 -0800 |
---|---|---|
committer | xNinjaKittyx <xNinjaKittyx@users.noreply.github.com> | 2020-12-15 18:20:13 -0800 |
commit | 9aa54a5b27468d61337528cb1e1b5b9b11a80978 (patch) | |
tree | 567693115cc101efb9254a96d96d80e9f9ccd557 /tests/test_plugin.py | |
parent | 03c65c60b39e369958b056c5c844d36d515c8a63 (diff) | |
download | cmd2-git-ci_improvements.tar.gz |
Adds pre-commit config to run various lintersci_improvements
This ads black, isort, pyupgrade, and flake8 to pre-commit-config.yaml
There are also some small changes to travis.yml and tasks.py to reduce
some repeated configurations that should be consolidated into
setup.cfg. Most other changes are automated by the linter scripts.
Diffstat (limited to 'tests/test_plugin.py')
-rw-r--r-- | tests/test_plugin.py | 67 |
1 files changed, 60 insertions, 7 deletions
diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 279f2f79..6f2b2f32 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -18,9 +18,9 @@ except ImportError: from unittest import mock - class Plugin: """A mixin class for testing hook registration and calling""" + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.reset_counters() @@ -222,14 +222,16 @@ class Plugin: self.called_cmdfinalization += 1 raise ValueError - def cmdfinalization_hook_system_exit(self, data: cmd2.plugin.CommandFinalizationData) -> \ - cmd2.plugin.CommandFinalizationData: + def cmdfinalization_hook_system_exit( + self, data: cmd2.plugin.CommandFinalizationData + ) -> cmd2.plugin.CommandFinalizationData: """A command finalization hook which raises a SystemExit""" self.called_cmdfinalization += 1 raise SystemExit - def cmdfinalization_hook_keyboard_interrupt(self, data: cmd2.plugin.CommandFinalizationData) -> \ - cmd2.plugin.CommandFinalizationData: + def cmdfinalization_hook_keyboard_interrupt( + self, data: cmd2.plugin.CommandFinalizationData + ) -> cmd2.plugin.CommandFinalizationData: """A command finalization hook which raises a KeyboardInterrupt""" self.called_cmdfinalization += 1 raise KeyboardInterrupt @@ -238,8 +240,9 @@ class Plugin: """A command finalization hook with no parameters.""" pass - def cmdfinalization_hook_too_many_parameters(self, one: plugin.CommandFinalizationData, two: str) -> \ - plugin.CommandFinalizationData: + def cmdfinalization_hook_too_many_parameters( + self, one: plugin.CommandFinalizationData, two: str + ) -> plugin.CommandFinalizationData: """A command finalization hook with too many parameters.""" return one @@ -262,6 +265,7 @@ class Plugin: class PluggedApp(Plugin, cmd2.Cmd): """A sample app with a plugin mixed in""" + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -281,6 +285,7 @@ class PluggedApp(Plugin, cmd2.Cmd): """Repeat back the arguments""" self.poutput(namespace.cmd2_statement.get()) + ### # # test pre and postloop hooks @@ -291,11 +296,13 @@ def test_register_preloop_hook_too_many_parameters(): with pytest.raises(TypeError): app.register_preloop_hook(app.prepost_hook_too_many_parameters) + def test_register_preloop_hook_with_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_preloop_hook(app.prepost_hook_with_wrong_return_annotation) + def test_preloop_hook(capsys): # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args testargs = ["prog", "say hello", 'quit'] @@ -309,6 +316,7 @@ def test_preloop_hook(capsys): assert out == 'one\nhello\n' assert not err + def test_preloop_hooks(capsys): # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args testargs = ["prog", "say hello", 'quit'] @@ -323,16 +331,19 @@ def test_preloop_hooks(capsys): assert out == 'one\ntwo\nhello\n' assert not err + def test_register_postloop_hook_too_many_parameters(): app = PluggedApp() with pytest.raises(TypeError): app.register_postloop_hook(app.prepost_hook_too_many_parameters) + def test_register_postloop_hook_with_wrong_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postloop_hook(app.prepost_hook_with_wrong_return_annotation) + def test_postloop_hook(capsys): # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args testargs = ["prog", "say hello", 'quit'] @@ -346,6 +357,7 @@ def test_postloop_hook(capsys): assert out == 'hello\none\n' assert not err + def test_postloop_hooks(capsys): # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args testargs = ["prog", "say hello", 'quit'] @@ -360,6 +372,7 @@ def test_postloop_hooks(capsys): assert out == 'hello\none\ntwo\n' assert not err + ### # # test preparse hook @@ -374,6 +387,7 @@ def test_preparse(capsys): assert not err assert app.called_preparse == 1 + ### # # test postparsing hooks @@ -384,26 +398,31 @@ def test_postparsing_hook_too_many_parameters(): with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_too_many_parameters) + def test_postparsing_hook_undeclared_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_undeclared_parameter_annotation) + def test_postparsing_hook_wrong_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_wrong_parameter_annotation) + def test_postparsing_hook_undeclared_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_undeclared_return_annotation) + def test_postparsing_hook_wrong_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postparsing_hook(app.postparse_hook_wrong_return_annotation) + def test_postparsing_hook(capsys): app = PluggedApp() app.onecmd_plus_hooks('say hello') @@ -429,6 +448,7 @@ def test_postparsing_hook(capsys): assert not err assert app.called_postparsing == 2 + def test_postparsing_hook_stop_first(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook_stop) @@ -443,6 +463,7 @@ def test_postparsing_hook_stop_first(capsys): assert app.called_postparsing == 1 assert stop + def test_postparsing_hook_stop_second(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook) @@ -464,6 +485,7 @@ def test_postparsing_hook_stop_second(capsys): assert app.called_postparsing == 2 assert stop + def test_postparsing_hook_emptystatement_first(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook_emptystatement) @@ -484,6 +506,7 @@ def test_postparsing_hook_emptystatement_first(capsys): assert not err assert app.called_postparsing == 1 + def test_postparsing_hook_emptystatement_second(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook) @@ -514,6 +537,7 @@ def test_postparsing_hook_emptystatement_second(capsys): assert not err assert app.called_postparsing == 2 + def test_postparsing_hook_exception(capsys): app = PluggedApp() app.register_postparsing_hook(app.postparse_hook_exception) @@ -534,6 +558,7 @@ def test_postparsing_hook_exception(capsys): assert err assert app.called_postparsing == 1 + ### # # test precmd hooks @@ -546,26 +571,31 @@ def test_register_precmd_hook_parameter_count(): with pytest.raises(TypeError): app.register_precmd_hook(app.precmd_hook_too_many_parameters) + def test_register_precmd_hook_no_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_precmd_hook(app.precmd_hook_no_parameter_annotation) + def test_register_precmd_hook_wrong_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_precmd_hook(app.precmd_hook_wrong_parameter_annotation) + def test_register_precmd_hook_no_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_precmd_hook(app.precmd_hook_no_return_annotation) + def test_register_precmd_hook_wrong_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_precmd_hook(app.precmd_hook_wrong_return_annotation) + def test_precmd_hook(capsys): app = PluggedApp() app.onecmd_plus_hooks('say hello') @@ -594,6 +624,7 @@ def test_precmd_hook(capsys): # with two hooks registered, we should get precmd() and both hooks assert app.called_precmd == 3 + def test_precmd_hook_emptystatement_first(capsys): app = PluggedApp() app.register_precmd_hook(app.precmd_hook_emptystatement) @@ -619,6 +650,7 @@ def test_precmd_hook_emptystatement_first(capsys): # called assert app.called_precmd == 1 + def test_precmd_hook_emptystatement_second(capsys): app = PluggedApp() app.register_precmd_hook(app.precmd_hook) @@ -655,6 +687,7 @@ def test_precmd_hook_emptystatement_second(capsys): # if a registered hook throws an exception, precmd() is never called assert app.called_precmd == 2 + ### # # test postcmd hooks @@ -667,26 +700,31 @@ def test_register_postcmd_hook_parameter_count(): with pytest.raises(TypeError): app.register_postcmd_hook(app.postcmd_hook_too_many_parameters) + def test_register_postcmd_hook_no_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postcmd_hook(app.postcmd_hook_no_parameter_annotation) + def test_register_postcmd_hook_wrong_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postcmd_hook(app.postcmd_hook_wrong_parameter_annotation) + def test_register_postcmd_hook_no_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postcmd_hook(app.postcmd_hook_no_return_annotation) + def test_register_postcmd_hook_wrong_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_postcmd_hook(app.postcmd_hook_wrong_return_annotation) + def test_postcmd(capsys): app = PluggedApp() app.onecmd_plus_hooks('say hello') @@ -715,6 +753,7 @@ def test_postcmd(capsys): # with two hooks registered, we should get precmd() and both hooks assert app.called_postcmd == 3 + def test_postcmd_exception_first(capsys): app = PluggedApp() app.register_postcmd_hook(app.postcmd_hook_exception) @@ -741,6 +780,7 @@ def test_postcmd_exception_first(capsys): # called assert app.called_postcmd == 1 + def test_postcmd_exception_second(capsys): app = PluggedApp() app.register_postcmd_hook(app.postcmd_hook) @@ -766,6 +806,7 @@ def test_postcmd_exception_second(capsys): # the exception assert app.called_postcmd == 2 + ## # # command finalization @@ -778,26 +819,31 @@ def test_register_cmdfinalization_hook_parameter_count(): with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_too_many_parameters) + def test_register_cmdfinalization_hook_no_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_no_parameter_annotation) + def test_register_cmdfinalization_hook_wrong_parameter_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_wrong_parameter_annotation) + def test_register_cmdfinalization_hook_no_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_no_return_annotation) + def test_register_cmdfinalization_hook_wrong_return_annotation(): app = PluggedApp() with pytest.raises(TypeError): app.register_cmdfinalization_hook(app.cmdfinalization_hook_wrong_return_annotation) + def test_cmdfinalization(capsys): app = PluggedApp() app.onecmd_plus_hooks('say hello') @@ -822,6 +868,7 @@ def test_cmdfinalization(capsys): assert not err assert app.called_cmdfinalization == 2 + def test_cmdfinalization_stop_first(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook_stop) @@ -833,6 +880,7 @@ def test_cmdfinalization_stop_first(capsys): assert app.called_cmdfinalization == 2 assert stop + def test_cmdfinalization_stop_second(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook) @@ -844,6 +892,7 @@ def test_cmdfinalization_stop_second(capsys): assert app.called_cmdfinalization == 2 assert stop + def test_cmdfinalization_hook_exception(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook_exception) @@ -864,6 +913,7 @@ def test_cmdfinalization_hook_exception(capsys): assert err assert app.called_cmdfinalization == 1 + def test_cmdfinalization_hook_system_exit(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook_system_exit) @@ -871,6 +921,7 @@ def test_cmdfinalization_hook_system_exit(capsys): assert stop assert app.called_cmdfinalization == 1 + def test_cmdfinalization_hook_keyboard_interrupt(capsys): app = PluggedApp() app.register_cmdfinalization_hook(app.cmdfinalization_hook_keyboard_interrupt) @@ -893,6 +944,7 @@ def test_cmdfinalization_hook_keyboard_interrupt(capsys): assert stop assert app.called_cmdfinalization == 1 + def test_skip_postcmd_hooks(capsys): app = PluggedApp() app.register_postcmd_hook(app.postcmd_hook) @@ -905,6 +957,7 @@ def test_skip_postcmd_hooks(capsys): assert app.called_postcmd == 0 assert app.called_cmdfinalization == 1 + def test_cmd2_argparse_exception(capsys): """ Verify Cmd2ArgparseErrors raised after calling a command prevent postcmd events from |