1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from contextlib import contextmanager
import pytest
from clikit.api.args import Args
from clikit.api.args.format import Argument
from clikit.args.string_args import StringArgs
from clikit.config.default_application_config import DefaultApplicationConfig
from clikit.console_application import ConsoleApplication
from clikit.io.output_stream import BufferedOutputStream
@pytest.fixture()
def app():
config = DefaultApplicationConfig()
config.set_catch_exceptions(True)
config.set_terminate_after_run(False)
config.set_display_name("The Application")
config.set_version("1.2.3")
with config.command("command") as c:
with c.sub_command("run") as sc:
sc.set_description('Description of "run"')
sc.add_argument("args", Argument.MULTI_VALUED, 'Description of "argument"')
application = ConsoleApplication(config)
return application
def func_spy():
def decorator(func):
def wrapper(*args, **kwargs):
decorator.called = True
return func(*args, **kwargs)
return wrapper
decorator.called = False
return decorator
@contextmanager
def help_spy(help_command):
spy = func_spy()
original = help_command.config.handler.handle
try:
help_command.config.handler.handle = spy(original)
yield spy
finally:
help_command.config.handler.handle = original
@pytest.mark.parametrize(
"args",
[
"command run --help",
"command run --help --another",
"command run --help -- whatever",
"command run -h",
"command run -h --another",
"command run -h -- whatever",
],
)
def test_help_option(app, args):
help_command = app.get_command("help")
with help_spy(help_command) as spy:
app.run(StringArgs(args))
assert spy.called, "help command not called"
@pytest.mark.parametrize(
"args",
[
"command run -- whatever --help",
"command run -- whatever -h",
"command run -- --help",
],
)
def test_help_option_ignored(app, args):
help_command = app.get_command("help")
with help_spy(help_command) as spy:
app.run(StringArgs(args))
assert not spy.called, "help command called"
|