diff --git a/elsa/_cli.py b/elsa/_cli.py index 6ac08cd..78b06eb 100644 --- a/elsa/_cli.py +++ b/elsa/_cli.py @@ -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) @@ -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 diff --git a/tests/fixtures/custom_command.py b/tests/fixtures/custom_command.py new file mode 100644 index 0000000..d682385 --- /dev/null +++ b/tests/fixtures/custom_command.py @@ -0,0 +1,21 @@ +import click +from flask import Flask + + +app = Flask('custom_command') + + +@app.route('/') +def index(): + return 'SUCCESS' + + +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() diff --git a/tests/test_commands.py b/tests/test_commands.py index 856a985..158ac25 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -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 @@ -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'