Click command always return exit code zero
My click command is using subprocess module
import os
import subprocess
import click
@click.command()
@click.argument('path', default=os.path.join('morakot_support', 'tests'))
def cli(path):
"""
Run tests with Pytest.
:param path: Test path
:return: Subprocess call result
"""
cmd = f'py.test {path}'
return subprocess.call(cmd, shell=True)
even though my test fail click always return exit code zero.
but when I run the the same command with separate file
import subprocess
p = subprocess.call('pytest morakot_support/tests', shell=True)
print(p)
the exit code is 1. I'm using the click commad in CI even my test fail the CI build always passed because my click command always return exit code zero
Use sys.exit instead of a return value: sys.exit(...) instead of return ...
@untitaker Okay let me try so I would replace this line
return subprocess.call(cmd, shell=True)
to this line right
sys.exit(subprocess.call(cmd, shell=True))
Yes, exactly. click does nothing with the return value of a function.
Thank you @untitaker it works!
Is there a reason click was designed to return NOT return the value of the function? I assumed that the command-line version of my click-decorated functions would send their return value back to the shell, and I was surprised that the return value is always "0".
It seems like a simple modification:
diff --git a/click/core.py b/click/core.py
index b307407..4c5b93e 100644
--- a/click/core.py
+++ b/click/core.py
@@ -709 +709 @@ class BaseCommand(object):
- ctx.exit()
+ ctx.exit(rv)
This change appears to cause errors in the pytests in test_chain.py (on the assert not result.exception line. result.exception would contain an array of [None, None, None] for each of the chained commands instead of just a single None.)
The rv already means something (i.e. it can be propagated), there was a PR referencing this issue that explored this.
On 28 June 2017 18:39:35 GMT+02:00, Peter Hutkins [email protected] wrote:
Is there a reason click was designed to return NOT return the value of the function? I assumed that the command-line version of my click-decorated functions would send their return value back to the shell, and I was surprised that the return value is always "0".
It seems like a simple modification:
diff --git a/click/core.py b/click/core.py index b307407..4c5b93e 100644 --- a/click/core.py +++ b/click/core.py @@ -709 +709 @@ class BaseCommand(object): - ctx.exit() + ctx.exit(rv)This change appears to cause errors in the pytests that test chaining (on the
assert not result.exceptionline), but I don't know the click project well enough to know if that is a real problem or just the assumptions of the tests.-- You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub: https://github.com/pallets/click/issues/747#issuecomment-311717363
-- Sent from my Android device with K-9 Mail. Please excuse my brevity.
Is this the PR you're referring to? https://github.com/pallets/click/pull/765