From b857ba6746589a3b2a812df5b3b10472a8365e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikul=C3=A1=C5=A1=20Poul?= Date: Sun, 25 Mar 2018 16:01:49 +0200 Subject: [PATCH 1/6] Added a way of reusing the cli to add new commands --- elsa/_cli.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/elsa/_cli.py b/elsa/_cli.py index 6ac08cd..1400909 100644 --- a/elsa/_cli.py +++ b/elsa/_cli.py @@ -55,7 +55,7 @@ def cname(): mimetype='application/octet-stream') -def cli(app, *, freezer=None, base_url=None): +def cli(app, *, freezer=None, base_url=None, invoke_cli=True): """Get a cli() function for provided app""" if not freezer: freezer = ShutdownableFreezer(app) @@ -139,4 +139,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 From 60227e675ac056c38d85081b22c32e406e8e557b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikul=C3=A1=C5=A1=20Poul?= Date: Sun, 25 Mar 2018 23:14:54 +0200 Subject: [PATCH 2/6] Updated elsa.cli docstring, added test for invoke_cli=False --- elsa/_cli.py | 6 +++++- tests/fixtures/custom_command.py | 21 +++++++++++++++++++++ tests/test_commands.py | 17 +++++++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/custom_command.py diff --git a/elsa/_cli.py b/elsa/_cli.py index 1400909..5db0066 100644 --- a/elsa/_cli.py +++ b/elsa/_cli.py @@ -56,7 +56,11 @@ def cname(): def cli(app, *, freezer=None, base_url=None, invoke_cli=True): - """Get a cli() function for provided app""" + """ Generates command-line interface for the provided app. + + If ``invoke_cli`` is set to ``True``, the cli is invoked right away, + otherwise it's returned so it can be used further. + """ if not freezer: freezer = ShutdownableFreezer(app) 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..f6de30e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -73,12 +73,14 @@ class ElsaRunner: If there is a local website.py in pwd, uses that one, uses the one from fixtures instead. ''' - def run(self, *command, script=None, should_fail=False): + def run(self, *command, script=None, should_fail=False, + subprocess_kwargs=None): print('COMMAND: python website.py', *command) try: cr = subprocess.run( self.create_command(command, script), check=not should_fail, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + **(subprocess_kwargs or {}) ) except subprocess.CalledProcessError as e: raise CommandFailed('return code was {}'.format(e.returncode)) @@ -461,3 +463,14 @@ 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', + subprocess_kwargs={"stdout": subprocess.PIPE}) + + assert result.stdout.decode("utf-8").strip() == 'Custom command' From c61b582cabd725c247329dd4b291c37d1e69f149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Sun, 25 Mar 2018 23:19:07 +0200 Subject: [PATCH 3/6] fixup --- elsa/_cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/elsa/_cli.py b/elsa/_cli.py index 5db0066..51570d9 100644 --- a/elsa/_cli.py +++ b/elsa/_cli.py @@ -58,7 +58,8 @@ def cname(): 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 cli is invoked right away, + If ``invoke_cli`` is set to ``True`` (the dafulat), + the cli is invoked right away, otherwise it's returned so it can be used further. """ if not freezer: From 009a8602da0c93ac9106028ce7eeadac8d0ed1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Sun, 25 Mar 2018 23:19:50 +0200 Subject: [PATCH 4/6] fixup**2 --- elsa/_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elsa/_cli.py b/elsa/_cli.py index 51570d9..78b06eb 100644 --- a/elsa/_cli.py +++ b/elsa/_cli.py @@ -58,7 +58,7 @@ def cname(): 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 dafulat), + 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. """ From f2d4a648f85d49d622ff47a57fc5348e8bca8116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Sun, 25 Mar 2018 23:25:01 +0200 Subject: [PATCH 5/6] tests fixup --- tests/test_commands.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index f6de30e..cb86b10 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -73,20 +73,21 @@ class ElsaRunner: If there is a local website.py in pwd, uses that one, uses the one from fixtures instead. ''' - def run(self, *command, script=None, should_fail=False, - subprocess_kwargs=None): + def run(self, *command, script=None, should_fail=False): print('COMMAND: python website.py', *command) try: cr = subprocess.run( self.create_command(command, script), check=not should_fail, - stderr=subprocess.PIPE, - **(subprocess_kwargs or {}) + 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 @@ -470,7 +471,6 @@ def test_invoke_cli(elsa): with open(INDEX_FIXTURES) as f: assert 'SUCCESS' in f.read() - result = elsa.run('custom_command', script='custom_command.py', - subprocess_kwargs={"stdout": subprocess.PIPE}) + result = elsa.run('custom_command', script='custom_command.py') - assert result.stdout.decode("utf-8").strip() == 'Custom command' + assert result.stdout.strip() == 'Custom command' From 315772dd42c762a331bffb175859fff4bec6fc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Sun, 25 Mar 2018 23:27:14 +0200 Subject: [PATCH 6/6] fixup**2 --- tests/test_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index cb86b10..158ac25 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -79,7 +79,7 @@ def run(self, *command, script=None, should_fail=False): cr = subprocess.run( self.create_command(command, script), check=not should_fail, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stderr=subprocess.PIPE, ) except subprocess.CalledProcessError as e: raise CommandFailed('return code was {}'.format(e.returncode))