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

Fix exit code for command line errors #3925

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Christian Boelsen
Christian Theunert
Christian Tismer
Christopher Gilling
CrazyMerlyn
Cyrus Maden
Dhiren Serai
Daniel Grana
Expand Down
1 change: 1 addition & 0 deletions changelog/3913.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pytest now returns with correct exit code (EXIT_USAGEERROR, 4) when called with unknown arguments.
15 changes: 15 additions & 0 deletions src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
import warnings
import argparse

from gettext import gettext as _
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest does not use gettext at all so why add it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, totally missed that. Weird though, shouldn't flake8 catch that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RonnyPfannschmidt The default implementation of error function used it and I didn't want to change anything other than the error code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, it is used:

self.exit(EXIT_USAGEERROR, _("%(prog)s: error: %(message)s\n") % args)

Even though we don't translate pytest messages, I don't mind leaving it in as gettext is already being imported by argparser anyway. But want me to remove it @RonnyPfannschmidt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crazymerlyn thanks for the additional context
@nicoddemus thanks for the investigation

im a bit thorn between keeping similarity to imported code and limiting code scope, lets keep it as is for now

import sys as _sys

import py

from ..main import EXIT_USAGEERROR

FILE_OR_DIR = "file_or_dir"


Expand Down Expand Up @@ -329,6 +334,16 @@ def __init__(self, parser, extra_info=None):
# an usage error to provide more contextual information to the user
self.extra_info = extra_info

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

Prints a usage message incorporating the message to stderr and
exits.
Overrides the method in parent class to change exit code"""
self.print_usage(_sys.stderr)
args = {"prog": self.prog, "message": message}
self.exit(EXIT_USAGEERROR, _("%(prog)s: error: %(message)s\n") % args)

def parse_args(self, args=None, namespace=None):
"""allow splitting of positional arguments"""
args, argv = self.parse_known_args(args, namespace)
Expand Down
5 changes: 5 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,8 @@ def test_fixture_mock_integration(testdir):
p = testdir.copy_example("acceptance/fixture_mock_integration.py")
result = testdir.runpytest(p)
result.stdout.fnmatch_lines("*1 passed*")


def test_usage_error_code(testdir):
result = testdir.runpytest("-unknown-option-")
assert result.ret == EXIT_USAGEERROR