Skip to content

Commit

Permalink
Merge pull request #3925 from crazymerlyn/fix-exit-code
Browse files Browse the repository at this point in the history
Fix exit code for command line errors
  • Loading branch information
nicoddemus authored Sep 4, 2018
2 parents 01df368 + b01704c commit 29bfa5e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
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 _
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

0 comments on commit 29bfa5e

Please sign in to comment.