Skip to content

Commit

Permalink
Merge e8a45ab into 2fb53b8
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Mar 19, 2019
2 parents 2fb53b8 + e8a45ab commit 140474c
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,8 @@ Changelog
=========

### 2.4.4 - TBD
- Added the ablity to extend CLI APIs in addition to HTTP APIs issue #744.
- Added optional built-in API aware testing for CLI commands.
- Add unit test for `extend_api()` with CLI commands
- Fix running tests using `python setup.py test`
- Documented the `multiple_files` example
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions examples/multi_file_cli/api.py
@@ -0,0 +1,17 @@
import hug

import sub_api


@hug.cli()
def echo(text: hug.types.text):
return text


@hug.extend_api(sub_command='sub_api')
def extend_with():
return (sub_api, )


if __name__ == '__main__':
hug.API(__name__).cli()
6 changes: 6 additions & 0 deletions examples/multi_file_cli/sub_api.py
@@ -0,0 +1,6 @@
import hug


@hug.cli()
def hello():
return 'Hello world'
22 changes: 18 additions & 4 deletions hug/api.py
Expand Up @@ -156,7 +156,7 @@ def add_exception_handler(self, exception_type, error_handler, versions=(None, )
placement = self._exception_handlers.setdefault(version, OrderedDict())
placement[exception_type] = (error_handler, ) + placement.get(exception_type, tuple())

def extend(self, http_api, route="", base_url=""):
def extend(self, http_api, route="", base_url="", **kwargs):
"""Adds handlers from a different Hug API to this one - to create a single API"""
self.versions.update(http_api.versions)
base_url = base_url or self.base_url
Expand Down Expand Up @@ -396,6 +396,17 @@ def handlers(self):
"""Returns all registered handlers attached to this API"""
return self.commands.values()

def extend(self, cli_api, command_prefix="", sub_command="", **kwargs):
"""Extends this CLI api with the commands present in the provided cli_api object"""
if sub_command and command_prefix:
raise ValueError('It is not currently supported to provide both a command_prefix and sub_command')

if sub_command:
self.commands[sub_command] = cli_api
else:
for name, command in cli_api.commands.items():
self.commands["{}{}".format(command_prefix, name)] = command

def __str__(self):
return "{0}\n\nAvailable Commands:{1}\n".format(self.api.doc or self.api.name,
"\n\n\t- " + "\n\t- ".join(self.commands.keys()))
Expand Down Expand Up @@ -497,12 +508,15 @@ def context(self):
self._context = {}
return self._context

def extend(self, api, route="", base_url=""):
def extend(self, api, route="", base_url="", http=True, cli=True, **kwargs):
"""Adds handlers from a different Hug API to this one - to create a single API"""
api = API(api)

if hasattr(api, '_http'):
self.http.extend(api.http, route, base_url)
if http and hasattr(api, '_http'):
self.http.extend(api.http, route, base_url, **kwargs)

if cli and hasattr(api, '_cli'):
self.cli.extend(api.cli, **kwargs)

for directive in getattr(api, '_directives', {}).values():
self.add_directive(directive)
Expand Down
4 changes: 2 additions & 2 deletions hug/decorators.py
Expand Up @@ -169,12 +169,12 @@ def decorator(middleware_class):
return decorator


def extend_api(route="", api=None, base_url=""):
def extend_api(route="", api=None, base_url="", **kwargs):
"""Extends the current api, with handlers from an imported api. Optionally provide a route that prefixes access"""
def decorator(extend_with):
apply_to_api = hug.API(api) if api else hug.api.from_object(extend_with)
for extended_api in extend_with():
apply_to_api.extend(extended_api, route, base_url)
apply_to_api.extend(extended_api, route, base_url, **kwargs)
return extend_with
return decorator

Expand Down
7 changes: 5 additions & 2 deletions hug/test.py
Expand Up @@ -76,10 +76,13 @@ def call(method, api_or_module, url, body='', headers=None, params=None, query_s
globals()[method.lower()] = tester


def cli(method, *args, **arguments):
def cli(method, *args, api=None, module=None, **arguments):
"""Simulates testing a hug cli method from the command line"""

collect_output = arguments.pop('collect_output', True)
if api and module:
raise ValueError("Please specify an API OR a Module that contains the API, not both")
elif api or module:
method = API(api or module).cli.commands[method].interface._function

command_args = [method.__name__] + list(args)
for name, values in arguments.items():
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Expand Up @@ -32,6 +32,7 @@
JYTHON = 'java' in sys.platform

ext_modules = []
cmdclass = {}

try:
sys.pypy_version_info
Expand Down Expand Up @@ -96,6 +97,7 @@ def list_modules(dirname):
setup_requires=['pytest-runner'],
tests_require=['pytest', 'mock', 'marshmallow'],
ext_modules=ext_modules,
cmdclass=cmdclass,
python_requires=">=3.4",
keywords='Web, Python, Python3, Refactoring, REST, Framework, RPC',
classifiers=[
Expand Down
35 changes: 34 additions & 1 deletion tests/test_decorators.py
Expand Up @@ -825,7 +825,40 @@ def extend_with():

assert hug.test.get(api, '/api/made_up_go').data == 'Going!'
assert tests.module_fake_http_and_cli.made_up_go() == 'Going!'
assert hug.test.cli(tests.module_fake_http_and_cli.made_up_go) == 'Going!'
assert hug.test.cli('made_up_go', api=api)

# Should be able to apply a prefix when extending CLI APIs
@hug.extend_api(command_prefix='prefix_', http=False)
def extend_with():
return (tests.module_fake_http_and_cli, )

assert hug.test.cli('prefix_made_up_go', api=api)

# OR provide a sub command use to reference the commands
@hug.extend_api(sub_command='sub_api', http=False)
def extend_with():
return (tests.module_fake_http_and_cli, )

assert hug.test.cli('sub_api', 'made_up_go', api=api)

# But not both
with pytest.raises(ValueError):
@hug.extend_api(sub_command='sub_api', command_prefix='api_', http=False)
def extend_with():
return (tests.module_fake_http_and_cli, )


def test_extending_api_with_http_and_cli():
"""Test to ensure it's possible to extend the current API so both HTTP and CLI APIs are extended"""
import tests.module_fake_http_and_cli

@hug.extend_api(base_url='/api')
def extend_with():
return (tests.module_fake_http_and_cli, )

assert hug.test.get(api, '/api/made_up_go').data == 'Going!'
assert tests.module_fake_http_and_cli.made_up_go() == 'Going!'
assert hug.test.cli('made_up_go', api=api)


def test_cli():
Expand Down
40 changes: 40 additions & 0 deletions tests/test_test.py
@@ -0,0 +1,40 @@
"""tests/test_test.py.
Test to ensure basic test functionality works as expected.
Copyright (C) 2019 Timothy Edmund Crosley
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import pytest

import hug

api = hug.API(__name__)


def test_cli():
"""Test to ensure the CLI tester works as intended to allow testing CLI endpoints"""
@hug.cli()
def my_cli_function():
return 'Hello'

assert hug.test.cli(my_cli_function) == 'Hello'
assert hug.test.cli('my_cli_function', api=api) == 'Hello'

# Shouldn't be able to specify both api and module.
with pytest.raises(ValueError):
assert hug.test.cli('my_method', api=api, module=hug)

0 comments on commit 140474c

Please sign in to comment.