Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Click command always return exit code zero #747

Closed
thormengkheang opened this issue Mar 11, 2017 · 7 comments
Closed

Click command always return exit code zero #747

thormengkheang opened this issue Mar 11, 2017 · 7 comments

Comments

@thormengkheang
Copy link

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

@untitaker
Copy link
Contributor

untitaker commented Mar 11, 2017

Use sys.exit instead of a return value: sys.exit(...) instead of return ...

@thormengkheang
Copy link
Author

@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))

@untitaker
Copy link
Contributor

Yes, exactly. click does nothing with the return value of a function.

@thormengkheang
Copy link
Author

Thank you @untitaker it works!

@phutkins
Copy link

phutkins commented Jun 28, 2017

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.)

@untitaker
Copy link
Contributor

untitaker commented Jun 28, 2017 via email

@westurner
Copy link

Is this the PR you're referring to?
#765

praiskup added a commit to praiskup/copr that referenced this issue Jan 30, 2020
It turned out that some tests failed in 'manage.py test', but the
command returned exit status 0.  So we were not informed about our CI
failures.

This seems to be known and desired design of click, unfortunately:
pallets/click#747
hussainweb added a commit to axelerant/axl-template that referenced this issue Jun 15, 2020
Click does not let the return code pass through. So, we explicitly use sys.exit(). pallets/click#747
domdfcoding added a commit to python-coincidence/dep_checker that referenced this issue Nov 11, 2020
domdfcoding added a commit to repo-helper/repo_helper that referenced this issue Nov 11, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants