Fix exit code for command line errors

Fixes #3913
This commit is contained in:
CrazyMerlyn 2018-09-02 20:42:47 +00:00
parent 01df368d93
commit b01704cce1
4 changed files with 22 additions and 0 deletions

View File

@ -46,6 +46,7 @@ Christian Boelsen
Christian Theunert
Christian Tismer
Christopher Gilling
CrazyMerlyn
Cyrus Maden
Dhiren Serai
Daniel Grana

View File

@ -0,0 +1 @@
Pytest now returns with correct exit code (EXIT_USAGEERROR, 4) when called with unknown arguments.

View File

@ -2,8 +2,13 @@ import six
import warnings
import argparse
from gettext import gettext as _
import sys as _sys
import py
from ..main import EXIT_USAGEERROR
FILE_OR_DIR = "file_or_dir"
@ -329,6 +334,16 @@ class MyOptionParser(argparse.ArgumentParser):
# 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)

View File

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