| 1 |
"""Test plugin infrastructure and hooks.""" |
| 2 |
|
| 3 |
import argparse |
| 4 |
import sys |
| 5 |
from typing import NoReturn |
| 6 |
from unittest import ( |
| 7 |
mock, |
| 8 |
) |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
import cmd2 |
| 13 |
from cmd2 import ( |
| 14 |
Cmd2ArgumentParser, |
| 15 |
exceptions, |
| 16 |
plugin, |
| 17 |
with_argparser, |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
class Plugin: |
| 22 |
"""A mixin class for testing hook registration and calling""" |
| 23 |
|
| 24 |
def __init__(self, *args, **kwargs) -> None: |
| 25 |
super().__init__(*args, **kwargs) |
| 26 |
self.reset_counters() |
| 27 |
|
| 28 |
def reset_counters(self) -> None: |
| 29 |
self.called_preparse = 0 |
| 30 |
self.called_postparsing = 0 |
| 31 |
self.called_precmd = 0 |
| 32 |
self.called_postcmd = 0 |
| 33 |
self.called_cmdfinalization = 0 |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
def prepost_hook_one(self) -> None: |
| 42 |
"""Method used for preloop or postloop hooks""" |
| 43 |
self.poutput("one") |
| 44 |
|
| 45 |
def prepost_hook_two(self) -> None: |
| 46 |
"""Another method used for preloop or postloop hooks""" |
| 47 |
self.poutput("two") |
| 48 |
|
| 49 |
def prepost_hook_too_many_parameters(self, param) -> None: |
| 50 |
"""A preloop or postloop hook with too many parameters""" |
| 51 |
|
| 52 |
def prepost_hook_with_wrong_return_annotation(self) -> bool: |
| 53 |
"""A preloop or postloop hook with incorrect return type""" |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
def preparse(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData: |
| 61 |
"""Preparsing hook""" |
| 62 |
self.called_preparse += 1 |
| 63 |
return data |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
def postparse_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData: |
| 71 |
"""A postparsing hook""" |
| 72 |
self.called_postparsing += 1 |
| 73 |
return data |
| 74 |
|
| 75 |
def postparse_hook_stop(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData: |
| 76 |
"""A postparsing hook with requests application exit""" |
| 77 |
self.called_postparsing += 1 |
| 78 |
data.stop = True |
| 79 |
return data |
| 80 |
|
| 81 |
def postparse_hook_emptystatement(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData: |
| 82 |
"""A postparsing hook with raises an EmptyStatement exception""" |
| 83 |
self.called_postparsing += 1 |
| 84 |
raise exceptions.EmptyStatement |
| 85 |
|
| 86 |
def postparse_hook_exception(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData: |
| 87 |
"""A postparsing hook which raises an exception""" |
| 88 |
self.called_postparsing += 1 |
| 89 |
raise ValueError |
| 90 |
|
| 91 |
def postparse_hook_too_many_parameters(self, data1, data2) -> cmd2.plugin.PostparsingData: |
| 92 |
"""A postparsing hook with too many parameters""" |
| 93 |
|
| 94 |
def postparse_hook_undeclared_parameter_annotation(self, data) -> cmd2.plugin.PostparsingData: |
| 95 |
"""A postparsing hook with an undeclared parameter type""" |
| 96 |
|
| 97 |
def postparse_hook_wrong_parameter_annotation(self, data: str) -> cmd2.plugin.PostparsingData: |
| 98 |
"""A postparsing hook with the wrong parameter type""" |
| 99 |
|
| 100 |
def postparse_hook_undeclared_return_annotation(self, data: cmd2.plugin.PostparsingData) -> None: |
| 101 |
"""A postparsing hook with an undeclared return type""" |
| 102 |
|
| 103 |
def postparse_hook_wrong_return_annotation(self, data: cmd2.plugin.PostparsingData) -> str: |
| 104 |
"""A postparsing hook with the wrong return type""" |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
def precmd(self, statement: cmd2.Statement) -> cmd2.Statement: |
| 112 |
"""Override cmd.Cmd method""" |
| 113 |
self.called_precmd += 1 |
| 114 |
return statement |
| 115 |
|
| 116 |
def precmd_hook(self, data: plugin.PrecommandData) -> plugin.PrecommandData: |
| 117 |
"""A precommand hook""" |
| 118 |
self.called_precmd += 1 |
| 119 |
return data |
| 120 |
|
| 121 |
def precmd_hook_emptystatement(self, data: plugin.PrecommandData) -> plugin.PrecommandData: |
| 122 |
"""A precommand hook which raises an EmptyStatement exception""" |
| 123 |
self.called_precmd += 1 |
| 124 |
raise exceptions.EmptyStatement |
| 125 |
|
| 126 |
def precmd_hook_exception(self, data: plugin.PrecommandData) -> plugin.PrecommandData: |
| 127 |
"""A precommand hook which raises an exception""" |
| 128 |
self.called_precmd += 1 |
| 129 |
raise ValueError |
| 130 |
|
| 131 |
def precmd_hook_not_enough_parameters(self) -> plugin.PrecommandData: |
| 132 |
"""A precommand hook with no parameters""" |
| 133 |
|
| 134 |
def precmd_hook_too_many_parameters(self, one: plugin.PrecommandData, two: str) -> plugin.PrecommandData: |
| 135 |
"""A precommand hook with too many parameters""" |
| 136 |
return one |
| 137 |
|
| 138 |
def precmd_hook_no_parameter_annotation(self, data) -> plugin.PrecommandData: |
| 139 |
"""A precommand hook with no type annotation on the parameter""" |
| 140 |
return data |
| 141 |
|
| 142 |
def precmd_hook_wrong_parameter_annotation(self, data: str) -> plugin.PrecommandData: |
| 143 |
"""A precommand hook with the incorrect type annotation on the parameter""" |
| 144 |
return data |
| 145 |
|
| 146 |
def precmd_hook_no_return_annotation(self, data: plugin.PrecommandData): |
| 147 |
"""A precommand hook with no type annotation on the return value""" |
| 148 |
return data |
| 149 |
|
| 150 |
def precmd_hook_wrong_return_annotation(self, data: plugin.PrecommandData) -> cmd2.Statement: |
| 151 |
return self.statement_parser.parse("hi there") |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
def postcmd(self, stop: bool, statement: cmd2.Statement) -> bool: |
| 159 |
"""Override cmd.Cmd method""" |
| 160 |
self.called_postcmd += 1 |
| 161 |
return stop |
| 162 |
|
| 163 |
def postcmd_hook(self, data: plugin.PostcommandData) -> plugin.PostcommandData: |
| 164 |
"""A postcommand hook""" |
| 165 |
self.called_postcmd += 1 |
| 166 |
return data |
| 167 |
|
| 168 |
def postcmd_hook_exception(self, data: plugin.PostcommandData) -> plugin.PostcommandData: |
| 169 |
"""A postcommand hook with raises an exception""" |
| 170 |
self.called_postcmd += 1 |
| 171 |
raise ZeroDivisionError |
| 172 |
|
| 173 |
def postcmd_hook_not_enough_parameters(self) -> plugin.PostcommandData: |
| 174 |
"""A precommand hook with no parameters""" |
| 175 |
|
| 176 |
def postcmd_hook_too_many_parameters(self, one: plugin.PostcommandData, two: str) -> plugin.PostcommandData: |
| 177 |
"""A precommand hook with too many parameters""" |
| 178 |
return one |
| 179 |
|
| 180 |
def postcmd_hook_no_parameter_annotation(self, data) -> plugin.PostcommandData: |
| 181 |
"""A precommand hook with no type annotation on the parameter""" |
| 182 |
return data |
| 183 |
|
| 184 |
def postcmd_hook_wrong_parameter_annotation(self, data: str) -> plugin.PostcommandData: |
| 185 |
"""A precommand hook with the incorrect type annotation on the parameter""" |
| 186 |
return data |
| 187 |
|
| 188 |
def postcmd_hook_no_return_annotation(self, data: plugin.PostcommandData): |
| 189 |
"""A precommand hook with no type annotation on the return value""" |
| 190 |
return data |
| 191 |
|
| 192 |
def postcmd_hook_wrong_return_annotation(self, data: plugin.PostcommandData) -> cmd2.Statement: |
| 193 |
return self.statement_parser.parse("hi there") |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
def cmdfinalization_hook(self, data: plugin.CommandFinalizationData) -> plugin.CommandFinalizationData: |
| 201 |
"""A command finalization hook.""" |
| 202 |
self.called_cmdfinalization += 1 |
| 203 |
return data |
| 204 |
|
| 205 |
def cmdfinalization_hook_stop(self, data: cmd2.plugin.CommandFinalizationData) -> cmd2.plugin.CommandFinalizationData: |
| 206 |
"""A command finalization hook which requests application exit""" |
| 207 |
self.called_cmdfinalization += 1 |
| 208 |
data.stop = True |
| 209 |
return data |
| 210 |
|
| 211 |
def cmdfinalization_hook_exception(self, data: cmd2.plugin.CommandFinalizationData) -> cmd2.plugin.CommandFinalizationData: |
| 212 |
"""A command finalization hook which raises an exception""" |
| 213 |
self.called_cmdfinalization += 1 |
| 214 |
raise ValueError |
| 215 |
|
| 216 |
def cmdfinalization_hook_system_exit( |
| 217 |
self, data: cmd2.plugin.CommandFinalizationData |
| 218 |
) -> cmd2.plugin.CommandFinalizationData: |
| 219 |
"""A command finalization hook which raises a SystemExit""" |
| 220 |
self.called_cmdfinalization += 1 |
| 221 |
raise SystemExit(5) |
| 222 |
|
| 223 |
def cmdfinalization_hook_keyboard_interrupt( |
| 224 |
self, data: cmd2.plugin.CommandFinalizationData |
| 225 |
) -> cmd2.plugin.CommandFinalizationData: |
| 226 |
"""A command finalization hook which raises a KeyboardInterrupt""" |
| 227 |
self.called_cmdfinalization += 1 |
| 228 |
raise KeyboardInterrupt |
| 229 |
|
| 230 |
def cmdfinalization_hook_passthrough_exception( |
| 231 |
self, data: cmd2.plugin.CommandFinalizationData |
| 232 |
) -> cmd2.plugin.CommandFinalizationData: |
| 233 |
"""A command finalization hook which raises a PassThroughException""" |
| 234 |
self.called_cmdfinalization += 1 |
| 235 |
wrapped_ex = OSError("Pass me up") |
| 236 |
raise exceptions.PassThroughException(wrapped_ex=wrapped_ex) |
| 237 |
|
| 238 |
def cmdfinalization_hook_not_enough_parameters(self) -> plugin.CommandFinalizationData: |
| 239 |
"""A command finalization hook with no parameters.""" |
| 240 |
|
| 241 |
def cmdfinalization_hook_too_many_parameters( |
| 242 |
self, one: plugin.CommandFinalizationData, two: str |
| 243 |
) -> plugin.CommandFinalizationData: |
| 244 |
"""A command finalization hook with too many parameters.""" |
| 245 |
return one |
| 246 |
|
| 247 |
def cmdfinalization_hook_no_parameter_annotation(self, data) -> plugin.CommandFinalizationData: |
| 248 |
"""A command finalization hook with no type annotation on the parameter.""" |
| 249 |
return data |
| 250 |
|
| 251 |
def cmdfinalization_hook_wrong_parameter_annotation(self, data: str) -> plugin.CommandFinalizationData: |
| 252 |
"""A command finalization hook with the incorrect type annotation on the parameter.""" |
| 253 |
return data |
| 254 |
|
| 255 |
def cmdfinalization_hook_no_return_annotation(self, data: plugin.CommandFinalizationData): |
| 256 |
"""A command finalizationhook with no type annotation on the return value.""" |
| 257 |
return data |
| 258 |
|
| 259 |
def cmdfinalization_hook_wrong_return_annotation(self, data: plugin.CommandFinalizationData) -> cmd2.Statement: |
| 260 |
"""A command finalization hook with the wrong return type annotation.""" |
| 261 |
return self.statement_parser.parse("hi there") |
| 262 |
|
| 263 |
|
| 264 |
class PluggedApp(Plugin, cmd2.Cmd): |
| 265 |
"""A sample app with a plugin mixed in""" |
| 266 |
|
| 267 |
def __init__(self, *args, **kwargs) -> None: |
| 268 |
super().__init__(*args, **kwargs) |
| 269 |
|
| 270 |
def do_say(self, statement) -> None: |
| 271 |
"""Repeat back the arguments""" |
| 272 |
self.poutput(statement) |
| 273 |
|
| 274 |
def do_skip_postcmd_hooks(self, _) -> NoReturn: |
| 275 |
self.poutput("In do_skip_postcmd_hooks") |
| 276 |
raise exceptions.SkipPostcommandHooks |
| 277 |
|
| 278 |
parser = Cmd2ArgumentParser(description="Test parser") |
| 279 |
parser.add_argument("my_arg", help="some help text") |
| 280 |
|
| 281 |
@with_argparser(parser) |
| 282 |
def do_argparse_cmd(self, namespace: argparse.Namespace) -> None: |
| 283 |
"""Repeat back the arguments""" |
| 284 |
self.poutput(namespace.cmd2_statement) |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
def test_register_preloop_hook_too_many_parameters() -> None: |
| 293 |
app = PluggedApp() |
| 294 |
with pytest.raises(TypeError): |
| 295 |
app.register_preloop_hook(app.prepost_hook_too_many_parameters) |
| 296 |
|
| 297 |
|
| 298 |
def test_register_preloop_hook_with_return_annotation() -> None: |
| 299 |
app = PluggedApp() |
| 300 |
with pytest.raises(TypeError): |
| 301 |
app.register_preloop_hook(app.prepost_hook_with_wrong_return_annotation) |
| 302 |
|
| 303 |
|
| 304 |
def test_preloop_hook(capsys) -> None: |
| 305 |
|
| 306 |
testargs = ["prog", "say hello", "quit"] |
| 307 |
|
| 308 |
with mock.patch.object(sys, "argv", testargs): |
| 309 |
app = PluggedApp() |
| 310 |
|
| 311 |
app.register_preloop_hook(app.prepost_hook_one) |
| 312 |
app.cmdloop() |
| 313 |
out, err = capsys.readouterr() |
| 314 |
assert out == "one\nhello\n" |
| 315 |
assert not err |
| 316 |
|
| 317 |
|
| 318 |
def test_preloop_hooks(capsys) -> None: |
| 319 |
|
| 320 |
testargs = ["prog", "say hello", "quit"] |
| 321 |
|
| 322 |
with mock.patch.object(sys, "argv", testargs): |
| 323 |
app = PluggedApp() |
| 324 |
|
| 325 |
app.register_preloop_hook(app.prepost_hook_one) |
| 326 |
app.register_preloop_hook(app.prepost_hook_two) |
| 327 |
app.cmdloop() |
| 328 |
out, err = capsys.readouterr() |
| 329 |
assert out == "one\ntwo\nhello\n" |
| 330 |
assert not err |
| 331 |
|
| 332 |
|
| 333 |
def test_register_postloop_hook_too_many_parameters() -> None: |
| 334 |
app = PluggedApp() |
| 335 |
with pytest.raises(TypeError): |
| 336 |
app.register_postloop_hook(app.prepost_hook_too_many_parameters) |
| 337 |
|
| 338 |
|
| 339 |
def test_register_postloop_hook_with_wrong_return_annotation() -> None: |
| 340 |
app = PluggedApp() |
| 341 |
with pytest.raises(TypeError): |
| 342 |
app.register_postloop_hook(app.prepost_hook_with_wrong_return_annotation) |
| 343 |
|
| 344 |
|
| 345 |
def test_postloop_hook(capsys) -> None: |
| 346 |
|
| 347 |
testargs = ["prog", "say hello", "quit"] |
| 348 |
|
| 349 |
with mock.patch.object(sys, "argv", testargs): |
| 350 |
app = PluggedApp() |
| 351 |
|
| 352 |
app.register_postloop_hook(app.prepost_hook_one) |
| 353 |
app.cmdloop() |
| 354 |
out, err = capsys.readouterr() |
| 355 |
assert out == "hello\none\n" |
| 356 |
assert not err |
| 357 |
|
| 358 |
|
| 359 |
def test_postloop_hooks(capsys) -> None: |
| 360 |
|
| 361 |
testargs = ["prog", "say hello", "quit"] |
| 362 |
|
| 363 |
with mock.patch.object(sys, "argv", testargs): |
| 364 |
app = PluggedApp() |
| 365 |
|
| 366 |
app.register_postloop_hook(app.prepost_hook_one) |
| 367 |
app.register_postloop_hook(app.prepost_hook_two) |
| 368 |
app.cmdloop() |
| 369 |
out, err = capsys.readouterr() |
| 370 |
assert out == "hello\none\ntwo\n" |
| 371 |
assert not err |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
def test_preparse(capsys) -> None: |
| 380 |
app = PluggedApp() |
| 381 |
app.register_postparsing_hook(app.preparse) |
| 382 |
app.onecmd_plus_hooks("say hello") |
| 383 |
out, err = capsys.readouterr() |
| 384 |
assert out == "hello\n" |
| 385 |
assert not err |
| 386 |
assert app.called_preparse == 1 |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
def test_postparsing_hook_too_many_parameters() -> None: |
| 395 |
app = PluggedApp() |
| 396 |
with pytest.raises(TypeError): |
| 397 |
app.register_postparsing_hook(app.postparse_hook_too_many_parameters) |
| 398 |
|
| 399 |
|
| 400 |
def test_postparsing_hook_undeclared_parameter_annotation() -> None: |
| 401 |
app = PluggedApp() |
| 402 |
with pytest.raises(TypeError): |
| 403 |
app.register_postparsing_hook(app.postparse_hook_undeclared_parameter_annotation) |
| 404 |
|
| 405 |
|
| 406 |
def test_postparsing_hook_wrong_parameter_annotation() -> None: |
| 407 |
app = PluggedApp() |
| 408 |
with pytest.raises(TypeError): |
| 409 |
app.register_postparsing_hook(app.postparse_hook_wrong_parameter_annotation) |
| 410 |
|
| 411 |
|
| 412 |
def test_postparsing_hook_undeclared_return_annotation() -> None: |
| 413 |
app = PluggedApp() |
| 414 |
with pytest.raises(TypeError): |
| 415 |
app.register_postparsing_hook(app.postparse_hook_undeclared_return_annotation) |
| 416 |
|
| 417 |
|
| 418 |
def test_postparsing_hook_wrong_return_annotation() -> None: |
| 419 |
app = PluggedApp() |
| 420 |
with pytest.raises(TypeError): |
| 421 |
app.register_postparsing_hook(app.postparse_hook_wrong_return_annotation) |
| 422 |
|
| 423 |
|
| 424 |
def test_postparsing_hook(capsys) -> None: |
| 425 |
app = PluggedApp() |
| 426 |
app.onecmd_plus_hooks("say hello") |
| 427 |
out, err = capsys.readouterr() |
| 428 |
assert out == "hello\n" |
| 429 |
assert not err |
| 430 |
assert not app.called_postparsing |
| 431 |
|
| 432 |
app.reset_counters() |
| 433 |
app.register_postparsing_hook(app.postparse_hook) |
| 434 |
app.onecmd_plus_hooks("say hello") |
| 435 |
out, err = capsys.readouterr() |
| 436 |
assert out == "hello\n" |
| 437 |
assert not err |
| 438 |
assert app.called_postparsing == 1 |
| 439 |
|
| 440 |
|
| 441 |
app.reset_counters() |
| 442 |
app.register_postparsing_hook(app.postparse_hook) |
| 443 |
app.onecmd_plus_hooks("say hello") |
| 444 |
out, err = capsys.readouterr() |
| 445 |
assert out == "hello\n" |
| 446 |
assert not err |
| 447 |
assert app.called_postparsing == 2 |
| 448 |
|
| 449 |
|
| 450 |
def test_postparsing_hook_stop_first(capsys) -> None: |
| 451 |
app = PluggedApp() |
| 452 |
app.register_postparsing_hook(app.postparse_hook_stop) |
| 453 |
stop = app.onecmd_plus_hooks("say hello") |
| 454 |
assert app.called_postparsing == 1 |
| 455 |
assert stop |
| 456 |
|
| 457 |
|
| 458 |
app.reset_counters() |
| 459 |
app.register_postparsing_hook(app.postparse_hook) |
| 460 |
stop = app.onecmd_plus_hooks("say hello") |
| 461 |
assert app.called_postparsing == 1 |
| 462 |
assert stop |
| 463 |
|
| 464 |
|
| 465 |
def test_postparsing_hook_stop_second(capsys) -> None: |
| 466 |
app = PluggedApp() |
| 467 |
app.register_postparsing_hook(app.postparse_hook) |
| 468 |
stop = app.onecmd_plus_hooks("say hello") |
| 469 |
assert app.called_postparsing == 1 |
| 470 |
assert not stop |
| 471 |
|
| 472 |
|
| 473 |
app.reset_counters() |
| 474 |
app.register_postparsing_hook(app.postparse_hook_stop) |
| 475 |
stop = app.onecmd_plus_hooks("say hello") |
| 476 |
assert app.called_postparsing == 2 |
| 477 |
assert stop |
| 478 |
|
| 479 |
|
| 480 |
app.reset_counters() |
| 481 |
app.register_postparsing_hook(app.postparse_hook) |
| 482 |
stop = app.onecmd_plus_hooks("say hello") |
| 483 |
assert app.called_postparsing == 2 |
| 484 |
assert stop |
| 485 |
|
| 486 |
|
| 487 |
def test_postparsing_hook_emptystatement_first(capsys) -> None: |
| 488 |
app = PluggedApp() |
| 489 |
app.register_postparsing_hook(app.postparse_hook_emptystatement) |
| 490 |
stop = app.onecmd_plus_hooks("say hello") |
| 491 |
out, err = capsys.readouterr() |
| 492 |
assert not stop |
| 493 |
assert not out |
| 494 |
assert not err |
| 495 |
assert app.called_postparsing == 1 |
| 496 |
|
| 497 |
|
| 498 |
app.reset_counters() |
| 499 |
stop = app.register_postparsing_hook(app.postparse_hook) |
| 500 |
app.onecmd_plus_hooks("say hello") |
| 501 |
out, err = capsys.readouterr() |
| 502 |
assert not stop |
| 503 |
assert not out |
| 504 |
assert not err |
| 505 |
assert app.called_postparsing == 1 |
| 506 |
|
| 507 |
|
| 508 |
def test_postparsing_hook_emptystatement_second(capsys) -> None: |
| 509 |
app = PluggedApp() |
| 510 |
app.register_postparsing_hook(app.postparse_hook) |
| 511 |
stop = app.onecmd_plus_hooks("say hello") |
| 512 |
out, err = capsys.readouterr() |
| 513 |
assert not stop |
| 514 |
assert out == "hello\n" |
| 515 |
assert not err |
| 516 |
assert app.called_postparsing == 1 |
| 517 |
|
| 518 |
|
| 519 |
app.reset_counters() |
| 520 |
app.register_postparsing_hook(app.postparse_hook_emptystatement) |
| 521 |
stop = app.onecmd_plus_hooks("say hello") |
| 522 |
out, err = capsys.readouterr() |
| 523 |
assert not stop |
| 524 |
assert not out |
| 525 |
assert not err |
| 526 |
assert app.called_postparsing == 2 |
| 527 |
|
| 528 |
|
| 529 |
app.reset_counters() |
| 530 |
app.register_postparsing_hook(app.postparse_hook) |
| 531 |
stop = app.onecmd_plus_hooks("say hello") |
| 532 |
out, err = capsys.readouterr() |
| 533 |
assert not stop |
| 534 |
assert not out |
| 535 |
assert not err |
| 536 |
assert app.called_postparsing == 2 |
| 537 |
|
| 538 |
|
| 539 |
def test_postparsing_hook_exception(capsys) -> None: |
| 540 |
app = PluggedApp() |
| 541 |
app.register_postparsing_hook(app.postparse_hook_exception) |
| 542 |
stop = app.onecmd_plus_hooks("say hello") |
| 543 |
out, err = capsys.readouterr() |
| 544 |
assert not stop |
| 545 |
assert not out |
| 546 |
assert err |
| 547 |
assert app.called_postparsing == 1 |
| 548 |
|
| 549 |
|
| 550 |
app.reset_counters() |
| 551 |
app.register_postparsing_hook(app.postparse_hook) |
| 552 |
stop = app.onecmd_plus_hooks("say hello") |
| 553 |
out, err = capsys.readouterr() |
| 554 |
assert not stop |
| 555 |
assert not out |
| 556 |
assert err |
| 557 |
assert app.called_postparsing == 1 |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
def test_register_precmd_hook_parameter_count() -> None: |
| 566 |
app = PluggedApp() |
| 567 |
with pytest.raises(TypeError): |
| 568 |
app.register_precmd_hook(app.precmd_hook_not_enough_parameters) |
| 569 |
with pytest.raises(TypeError): |
| 570 |
app.register_precmd_hook(app.precmd_hook_too_many_parameters) |
| 571 |
|
| 572 |
|
| 573 |
def test_register_precmd_hook_no_parameter_annotation() -> None: |
| 574 |
app = PluggedApp() |
| 575 |
with pytest.raises(TypeError): |
| 576 |
app.register_precmd_hook(app.precmd_hook_no_parameter_annotation) |
| 577 |
|
| 578 |
|
| 579 |
def test_register_precmd_hook_wrong_parameter_annotation() -> None: |
| 580 |
app = PluggedApp() |
| 581 |
with pytest.raises(TypeError): |
| 582 |
app.register_precmd_hook(app.precmd_hook_wrong_parameter_annotation) |
| 583 |
|
| 584 |
|
| 585 |
def test_register_precmd_hook_no_return_annotation() -> None: |
| 586 |
app = PluggedApp() |
| 587 |
with pytest.raises(TypeError): |
| 588 |
app.register_precmd_hook(app.precmd_hook_no_return_annotation) |
| 589 |
|
| 590 |
|
| 591 |
def test_register_precmd_hook_wrong_return_annotation() -> None: |
| 592 |
app = PluggedApp() |
| 593 |
with pytest.raises(TypeError): |
| 594 |
app.register_precmd_hook(app.precmd_hook_wrong_return_annotation) |
| 595 |
|
| 596 |
|
| 597 |
def test_precmd_hook(capsys) -> None: |
| 598 |
app = PluggedApp() |
| 599 |
app.onecmd_plus_hooks("say hello") |
| 600 |
out, err = capsys.readouterr() |
| 601 |
assert out == "hello\n" |
| 602 |
assert not err |
| 603 |
|
| 604 |
assert app.called_precmd == 1 |
| 605 |
|
| 606 |
app.reset_counters() |
| 607 |
app.register_precmd_hook(app.precmd_hook) |
| 608 |
app.onecmd_plus_hooks("say hello") |
| 609 |
out, err = capsys.readouterr() |
| 610 |
assert out == "hello\n" |
| 611 |
assert not err |
| 612 |
|
| 613 |
assert app.called_precmd == 2 |
| 614 |
|
| 615 |
|
| 616 |
app.reset_counters() |
| 617 |
app.register_precmd_hook(app.precmd_hook) |
| 618 |
app.onecmd_plus_hooks("say hello") |
| 619 |
out, err = capsys.readouterr() |
| 620 |
assert out == "hello\n" |
| 621 |
assert not err |
| 622 |
|
| 623 |
assert app.called_precmd == 3 |
| 624 |
|
| 625 |
|
| 626 |
def test_precmd_hook_emptystatement_first(capsys) -> None: |
| 627 |
app = PluggedApp() |
| 628 |
app.register_precmd_hook(app.precmd_hook_emptystatement) |
| 629 |
stop = app.onecmd_plus_hooks("say hello") |
| 630 |
out, err = capsys.readouterr() |
| 631 |
assert not stop |
| 632 |
assert not out |
| 633 |
assert not err |
| 634 |
|
| 635 |
|
| 636 |
assert app.called_precmd == 1 |
| 637 |
|
| 638 |
|
| 639 |
app.reset_counters() |
| 640 |
stop = app.register_precmd_hook(app.precmd_hook) |
| 641 |
app.onecmd_plus_hooks("say hello") |
| 642 |
out, err = capsys.readouterr() |
| 643 |
assert not stop |
| 644 |
assert not out |
| 645 |
assert not err |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
assert app.called_precmd == 1 |
| 650 |
|
| 651 |
|
| 652 |
def test_precmd_hook_emptystatement_second(capsys) -> None: |
| 653 |
app = PluggedApp() |
| 654 |
app.register_precmd_hook(app.precmd_hook) |
| 655 |
stop = app.onecmd_plus_hooks("say hello") |
| 656 |
out, err = capsys.readouterr() |
| 657 |
assert not stop |
| 658 |
assert out == "hello\n" |
| 659 |
assert not err |
| 660 |
|
| 661 |
assert app.called_precmd == 2 |
| 662 |
|
| 663 |
|
| 664 |
app.reset_counters() |
| 665 |
app.register_precmd_hook(app.precmd_hook_emptystatement) |
| 666 |
stop = app.onecmd_plus_hooks("say hello") |
| 667 |
out, err = capsys.readouterr() |
| 668 |
assert not stop |
| 669 |
assert not out |
| 670 |
assert not err |
| 671 |
|
| 672 |
|
| 673 |
assert app.called_precmd == 2 |
| 674 |
|
| 675 |
|
| 676 |
app.reset_counters() |
| 677 |
app.register_precmd_hook(app.precmd_hook) |
| 678 |
stop = app.onecmd_plus_hooks("say hello") |
| 679 |
out, err = capsys.readouterr() |
| 680 |
assert not stop |
| 681 |
assert not out |
| 682 |
assert not err |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
assert app.called_precmd == 2 |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
def test_register_postcmd_hook_parameter_count() -> None: |
| 695 |
app = PluggedApp() |
| 696 |
with pytest.raises(TypeError): |
| 697 |
app.register_postcmd_hook(app.postcmd_hook_not_enough_parameters) |
| 698 |
with pytest.raises(TypeError): |
| 699 |
app.register_postcmd_hook(app.postcmd_hook_too_many_parameters) |
| 700 |
|
| 701 |
|
| 702 |
def test_register_postcmd_hook_no_parameter_annotation() -> None: |
| 703 |
app = PluggedApp() |
| 704 |
with pytest.raises(TypeError): |
| 705 |
app.register_postcmd_hook(app.postcmd_hook_no_parameter_annotation) |
| 706 |
|
| 707 |
|
| 708 |
def test_register_postcmd_hook_wrong_parameter_annotation() -> None: |
| 709 |
app = PluggedApp() |
| 710 |
with pytest.raises(TypeError): |
| 711 |
app.register_postcmd_hook(app.postcmd_hook_wrong_parameter_annotation) |
| 712 |
|
| 713 |
|
| 714 |
def test_register_postcmd_hook_no_return_annotation() -> None: |
| 715 |
app = PluggedApp() |
| 716 |
with pytest.raises(TypeError): |
| 717 |
app.register_postcmd_hook(app.postcmd_hook_no_return_annotation) |
| 718 |
|
| 719 |
|
| 720 |
def test_register_postcmd_hook_wrong_return_annotation() -> None: |
| 721 |
app = PluggedApp() |
| 722 |
with pytest.raises(TypeError): |
| 723 |
app.register_postcmd_hook(app.postcmd_hook_wrong_return_annotation) |
| 724 |
|
| 725 |
|
| 726 |
def test_postcmd(capsys) -> None: |
| 727 |
app = PluggedApp() |
| 728 |
app.onecmd_plus_hooks("say hello") |
| 729 |
out, err = capsys.readouterr() |
| 730 |
assert out == "hello\n" |
| 731 |
assert not err |
| 732 |
|
| 733 |
assert app.called_postcmd == 1 |
| 734 |
|
| 735 |
app.reset_counters() |
| 736 |
app.register_postcmd_hook(app.postcmd_hook) |
| 737 |
app.onecmd_plus_hooks("say hello") |
| 738 |
out, err = capsys.readouterr() |
| 739 |
assert out == "hello\n" |
| 740 |
assert not err |
| 741 |
|
| 742 |
assert app.called_postcmd == 2 |
| 743 |
|
| 744 |
|
| 745 |
app.reset_counters() |
| 746 |
app.register_postcmd_hook(app.postcmd_hook) |
| 747 |
app.onecmd_plus_hooks("say hello") |
| 748 |
out, err = capsys.readouterr() |
| 749 |
assert out == "hello\n" |
| 750 |
assert not err |
| 751 |
|
| 752 |
assert app.called_postcmd == 3 |
| 753 |
|
| 754 |
|
| 755 |
def test_postcmd_exception_first(capsys) -> None: |
| 756 |
app = PluggedApp() |
| 757 |
app.register_postcmd_hook(app.postcmd_hook_exception) |
| 758 |
stop = app.onecmd_plus_hooks("say hello") |
| 759 |
out, err = capsys.readouterr() |
| 760 |
assert not stop |
| 761 |
assert out == "hello\n" |
| 762 |
assert err |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
assert app.called_postcmd == 1 |
| 767 |
|
| 768 |
|
| 769 |
app.reset_counters() |
| 770 |
stop = app.register_postcmd_hook(app.postcmd_hook) |
| 771 |
app.onecmd_plus_hooks("say hello") |
| 772 |
out, err = capsys.readouterr() |
| 773 |
assert not stop |
| 774 |
assert out == "hello\n" |
| 775 |
assert err |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
assert app.called_postcmd == 1 |
| 780 |
|
| 781 |
|
| 782 |
def test_postcmd_exception_second(capsys) -> None: |
| 783 |
app = PluggedApp() |
| 784 |
app.register_postcmd_hook(app.postcmd_hook) |
| 785 |
stop = app.onecmd_plus_hooks("say hello") |
| 786 |
out, err = capsys.readouterr() |
| 787 |
assert not stop |
| 788 |
assert out == "hello\n" |
| 789 |
assert not err |
| 790 |
|
| 791 |
assert app.called_postcmd == 2 |
| 792 |
|
| 793 |
|
| 794 |
app.reset_counters() |
| 795 |
stop = app.register_postcmd_hook(app.postcmd_hook_exception) |
| 796 |
app.onecmd_plus_hooks("say hello") |
| 797 |
out, err = capsys.readouterr() |
| 798 |
assert not stop |
| 799 |
assert out == "hello\n" |
| 800 |
assert err |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
assert app.called_postcmd == 2 |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
def test_register_cmdfinalization_hook_parameter_count() -> None: |
| 814 |
app = PluggedApp() |
| 815 |
with pytest.raises(TypeError): |
| 816 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_not_enough_parameters) |
| 817 |
with pytest.raises(TypeError): |
| 818 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_too_many_parameters) |
| 819 |
|
| 820 |
|
| 821 |
def test_register_cmdfinalization_hook_no_parameter_annotation() -> None: |
| 822 |
app = PluggedApp() |
| 823 |
with pytest.raises(TypeError): |
| 824 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_no_parameter_annotation) |
| 825 |
|
| 826 |
|
| 827 |
def test_register_cmdfinalization_hook_wrong_parameter_annotation() -> None: |
| 828 |
app = PluggedApp() |
| 829 |
with pytest.raises(TypeError): |
| 830 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_wrong_parameter_annotation) |
| 831 |
|
| 832 |
|
| 833 |
def test_register_cmdfinalization_hook_no_return_annotation() -> None: |
| 834 |
app = PluggedApp() |
| 835 |
with pytest.raises(TypeError): |
| 836 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_no_return_annotation) |
| 837 |
|
| 838 |
|
| 839 |
def test_register_cmdfinalization_hook_wrong_return_annotation() -> None: |
| 840 |
app = PluggedApp() |
| 841 |
with pytest.raises(TypeError): |
| 842 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_wrong_return_annotation) |
| 843 |
|
| 844 |
|
| 845 |
def test_cmdfinalization(capsys) -> None: |
| 846 |
app = PluggedApp() |
| 847 |
app.onecmd_plus_hooks("say hello") |
| 848 |
out, err = capsys.readouterr() |
| 849 |
assert out == "hello\n" |
| 850 |
assert not err |
| 851 |
assert app.called_cmdfinalization == 0 |
| 852 |
|
| 853 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook) |
| 854 |
app.onecmd_plus_hooks("say hello") |
| 855 |
out, err = capsys.readouterr() |
| 856 |
assert out == "hello\n" |
| 857 |
assert not err |
| 858 |
assert app.called_cmdfinalization == 1 |
| 859 |
|
| 860 |
|
| 861 |
app.reset_counters() |
| 862 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook) |
| 863 |
app.onecmd_plus_hooks("say hello") |
| 864 |
out, err = capsys.readouterr() |
| 865 |
assert out == "hello\n" |
| 866 |
assert not err |
| 867 |
assert app.called_cmdfinalization == 2 |
| 868 |
|
| 869 |
|
| 870 |
def test_cmdfinalization_stop_first(capsys) -> None: |
| 871 |
app = PluggedApp() |
| 872 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_stop) |
| 873 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook) |
| 874 |
stop = app.onecmd_plus_hooks("say hello") |
| 875 |
out, err = capsys.readouterr() |
| 876 |
assert out == "hello\n" |
| 877 |
assert not err |
| 878 |
assert app.called_cmdfinalization == 2 |
| 879 |
assert stop |
| 880 |
|
| 881 |
|
| 882 |
def test_cmdfinalization_stop_second(capsys) -> None: |
| 883 |
app = PluggedApp() |
| 884 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook) |
| 885 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_stop) |
| 886 |
stop = app.onecmd_plus_hooks("say hello") |
| 887 |
out, err = capsys.readouterr() |
| 888 |
assert out == "hello\n" |
| 889 |
assert not err |
| 890 |
assert app.called_cmdfinalization == 2 |
| 891 |
assert stop |
| 892 |
|
| 893 |
|
| 894 |
def test_cmdfinalization_hook_exception(capsys) -> None: |
| 895 |
app = PluggedApp() |
| 896 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_exception) |
| 897 |
stop = app.onecmd_plus_hooks("say hello") |
| 898 |
out, err = capsys.readouterr() |
| 899 |
assert not stop |
| 900 |
assert out == "hello\n" |
| 901 |
assert err |
| 902 |
assert app.called_cmdfinalization == 1 |
| 903 |
|
| 904 |
|
| 905 |
app.reset_counters() |
| 906 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook) |
| 907 |
stop = app.onecmd_plus_hooks("say hello") |
| 908 |
out, err = capsys.readouterr() |
| 909 |
assert not stop |
| 910 |
assert out == "hello\n" |
| 911 |
assert err |
| 912 |
assert app.called_cmdfinalization == 1 |
| 913 |
|
| 914 |
|
| 915 |
def test_cmdfinalization_hook_system_exit() -> None: |
| 916 |
app = PluggedApp() |
| 917 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_system_exit) |
| 918 |
stop = app.onecmd_plus_hooks("say hello") |
| 919 |
assert stop |
| 920 |
assert app.called_cmdfinalization == 1 |
| 921 |
assert app.exit_code == 5 |
| 922 |
|
| 923 |
|
| 924 |
def test_cmdfinalization_hook_keyboard_interrupt() -> None: |
| 925 |
app = PluggedApp() |
| 926 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_keyboard_interrupt) |
| 927 |
|
| 928 |
|
| 929 |
stop = app.onecmd_plus_hooks("say hello", raise_keyboard_interrupt=False) |
| 930 |
assert not stop |
| 931 |
assert app.called_cmdfinalization == 1 |
| 932 |
|
| 933 |
|
| 934 |
app.reset_counters() |
| 935 |
with pytest.raises(KeyboardInterrupt): |
| 936 |
stop = app.onecmd_plus_hooks("say hello", raise_keyboard_interrupt=True) |
| 937 |
assert not stop |
| 938 |
assert app.called_cmdfinalization == 1 |
| 939 |
|
| 940 |
|
| 941 |
app.reset_counters() |
| 942 |
stop = app.onecmd_plus_hooks("quit", raise_keyboard_interrupt=True) |
| 943 |
assert stop |
| 944 |
assert app.called_cmdfinalization == 1 |
| 945 |
|
| 946 |
|
| 947 |
def test_cmdfinalization_hook_passthrough_exception() -> None: |
| 948 |
app = PluggedApp() |
| 949 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook_passthrough_exception) |
| 950 |
|
| 951 |
expected_err = "Pass me up" |
| 952 |
with pytest.raises(OSError, match=expected_err): |
| 953 |
app.onecmd_plus_hooks("say hello") |
| 954 |
assert app.called_cmdfinalization == 1 |
| 955 |
|
| 956 |
|
| 957 |
def test_skip_postcmd_hooks(capsys) -> None: |
| 958 |
app = PluggedApp() |
| 959 |
app.register_postcmd_hook(app.postcmd_hook) |
| 960 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook) |
| 961 |
|
| 962 |
|
| 963 |
app.onecmd_plus_hooks("skip_postcmd_hooks") |
| 964 |
out, _err = capsys.readouterr() |
| 965 |
assert "In do_skip_postcmd_hooks" in out |
| 966 |
assert app.called_postcmd == 0 |
| 967 |
assert app.called_cmdfinalization == 1 |
| 968 |
|
| 969 |
|
| 970 |
def test_cmd2_argparse_exception(capsys) -> None: |
| 971 |
"""Verify Cmd2ArgparseErrors raised after calling a command prevent postcmd events from |
| 972 |
running but do not affect cmdfinalization events |
| 973 |
""" |
| 974 |
app = PluggedApp() |
| 975 |
app.register_postcmd_hook(app.postcmd_hook) |
| 976 |
app.register_cmdfinalization_hook(app.cmdfinalization_hook) |
| 977 |
|
| 978 |
|
| 979 |
app.onecmd_plus_hooks("argparse_cmd arg_val") |
| 980 |
out, err = capsys.readouterr() |
| 981 |
assert out == "arg_val\n" |
| 982 |
assert not err |
| 983 |
assert app.called_postcmd == 2 |
| 984 |
assert app.called_cmdfinalization == 1 |
| 985 |
|
| 986 |
app.reset_counters() |
| 987 |
|
| 988 |
|
| 989 |
app.onecmd_plus_hooks("argparse_cmd") |
| 990 |
out, err = capsys.readouterr() |
| 991 |
assert not out |
| 992 |
assert "Error: the following arguments are required: my_arg" in err |
| 993 |
assert app.called_postcmd == 0 |
| 994 |
assert app.called_cmdfinalization == 1 |