Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions elsa/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ def cname():
mimetype='application/octet-stream')


def cli(app, *, freezer=None, base_url=None):
"""Get a cli() function for provided app"""
def cli(app, *, freezer=None, base_url=None, invoke_cli=True):
""" Generates command-line interface for the provided app.

If ``invoke_cli`` is set to ``True`` (the default),
the cli is invoked right away,
otherwise it's returned so it can be used further.
"""
if not freezer:
freezer = ShutdownableFreezer(app)

Expand Down Expand Up @@ -139,4 +144,7 @@ def deploy(path, base_url, remote, push, freeze,

deploy_(path, remote=remote, push=push, show_err=show_git_push_stderr)

return command()
if invoke_cli:
return command()
else:
return command
21 changes: 21 additions & 0 deletions tests/fixtures/custom_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import click
from flask import Flask


app = Flask('custom_command')


@app.route('/')
def index():
return '<html><body>SUCCESS</body></html>'


if __name__ == '__main__':
from elsa import cli
elsa = cli(app, base_url='https://example.org', invoke_cli=False)

@elsa.command()
def custom_command():
click.echo("Custom command")

elsa()
15 changes: 14 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ def run(self, *command, script=None, should_fail=False):
try:
cr = subprocess.run(
self.create_command(command, script), check=not should_fail,
stderr=subprocess.PIPE
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except subprocess.CalledProcessError as e:
raise CommandFailed('return code was {}'.format(e.returncode))
if should_fail and cr.returncode == 0:
raise CommandNotFailed('return code was 0')
cr.stdout = cr.stdout.decode('utf-8')
cr.stderr = cr.stderr.decode('utf-8')
sys.stdout.write(cr.stdout)
sys.stderr.write(cr.stderr)
return cr

Expand Down Expand Up @@ -461,3 +464,13 @@ def test_deploy_different_remote(elsa, push, gitrepo):
elsa.run('deploy', push, '--remote', 'foo')
assert 'SUCCESS' in commit_info()
assert is_true(push) == was_pushed(remote=remote)


def test_invoke_cli(elsa):
elsa.run('freeze', script='custom_command.py')
with open(INDEX_FIXTURES) as f:
assert 'SUCCESS' in f.read()

result = elsa.run('custom_command', script='custom_command.py')

assert result.stdout.strip() == 'Custom command'