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

Pytest exit codes for command line errors look incorrect #3913

Closed
rdadolf opened this Issue Aug 30, 2018 · 3 comments

Comments

Projects
None yet
4 participants
@rdadolf

rdadolf commented Aug 30, 2018

According to the documentation, the CLI return value for a bug on the command line should be 4. From what I can tell, all bugs return 2.

$ pytest -invalid-
pytest: error: unrecognized arguments: -invalid-
  inifile: None
  rootdir: /tmp
$ echo $?
2
$ pytest -v-v
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: argument -v/--verbose: ignored explicit argument '-v'
$ echo $?
2

It looks like interrupting PyTest does return 2, however:

$ pytest xgt_test.py 
================================ test session starts ================================
platform linux2 -- Python 2.7.15rc1, pytest-3.7.2, py-1.5.4, pluggy-0.7.1
rootdir: /gems/build/apps/mygems, inifile:
collected 26 items                                                                  

xgt_test.py EEEEEEEEEEE^C
<snip>
$ echo $?
2

System configuration:

$ pytest --version
This is pytest version 3.7.2, imported from /usr/local/lib/python2.7/dist-packages/pytest.pyc
$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS"

Happy to answer questions, and apologies if I've misunderstood the intended behavior.

@crazymerlyn

This comment has been minimized.

Show comment
Hide comment
@crazymerlyn

crazymerlyn Sep 2, 2018

Contributor

This line seems to be the culprit.
argparse.ArgumentParser.error exits with status code 2.

One possible solution is to override the error function and change the status code to EXIT_USAGEERROR.
Another is to raise a UsageError with an appropriate message.
Not sure which one would be better.

Contributor

crazymerlyn commented Sep 2, 2018

This line seems to be the culprit.
argparse.ArgumentParser.error exits with status code 2.

One possible solution is to override the error function and change the status code to EXIT_USAGEERROR.
Another is to raise a UsageError with an appropriate message.
Not sure which one would be better.

@nicoddemus

This comment has been minimized.

Show comment
Hide comment
@nicoddemus

nicoddemus Sep 2, 2018

Member

I think overriding error is simple enough:

    def error(self, message):
        """error(message: string)

        Prints a usage message incorporating the message to stderr and
        exits.

        If you override this in a subclass, it should not return -- it
        should either exit or raise an exception.
        """
        self.print_usage(_sys.stderr)
        args = {'prog': self.prog, 'message': message}
        self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
Member

nicoddemus commented Sep 2, 2018

I think overriding error is simple enough:

    def error(self, message):
        """error(message: string)

        Prints a usage message incorporating the message to stderr and
        exits.

        If you override this in a subclass, it should not return -- it
        should either exit or raise an exception.
        """
        self.print_usage(_sys.stderr)
        args = {'prog': self.prog, 'message': message}
        self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

crazymerlyn added a commit to crazymerlyn/pytest that referenced this issue Sep 2, 2018

crazymerlyn added a commit to crazymerlyn/pytest that referenced this issue Sep 3, 2018

@nicoddemus nicoddemus closed this Sep 4, 2018

@rdadolf

This comment has been minimized.

Show comment
Hide comment
@rdadolf

rdadolf Sep 4, 2018

Thanks for the diagnosis and fix, folks. Much appreciated!

rdadolf commented Sep 4, 2018

Thanks for the diagnosis and fix, folks. Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment