conversion of exit codes to enum + exposure (#5420)
conversion of exit codes to enum + exposure
This commit is contained in:
@@ -48,7 +48,7 @@ def main(args=None, plugins=None):
|
||||
:arg plugins: list of plugin objects to be auto-registered during
|
||||
initialization.
|
||||
"""
|
||||
from _pytest.main import EXIT_USAGEERROR
|
||||
from _pytest.main import ExitCode
|
||||
|
||||
try:
|
||||
try:
|
||||
@@ -78,7 +78,7 @@ def main(args=None, plugins=None):
|
||||
tw = py.io.TerminalWriter(sys.stderr)
|
||||
for msg in e.args:
|
||||
tw.line("ERROR: {}\n".format(msg), red=True)
|
||||
return EXIT_USAGEERROR
|
||||
return ExitCode.USAGE_ERROR
|
||||
|
||||
|
||||
class cmdline: # compatibility namespace
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
""" core implementation of testing process: init, session, runtest loop. """
|
||||
import enum
|
||||
import fnmatch
|
||||
import functools
|
||||
import os
|
||||
@@ -18,13 +19,26 @@ from _pytest.deprecated import PYTEST_CONFIG_GLOBAL
|
||||
from _pytest.outcomes import exit
|
||||
from _pytest.runner import collect_one_node
|
||||
|
||||
# exitcodes for the command line
|
||||
EXIT_OK = 0
|
||||
EXIT_TESTSFAILED = 1
|
||||
EXIT_INTERRUPTED = 2
|
||||
EXIT_INTERNALERROR = 3
|
||||
EXIT_USAGEERROR = 4
|
||||
EXIT_NOTESTSCOLLECTED = 5
|
||||
|
||||
class ExitCode(enum.IntEnum):
|
||||
"""
|
||||
Encodes the valid exit codes by pytest.
|
||||
|
||||
Currently users and plugins may supply other exit codes as well.
|
||||
"""
|
||||
|
||||
#: tests passed
|
||||
OK = 0
|
||||
#: tests failed
|
||||
TESTS_FAILED = 1
|
||||
#: pytest was interrupted
|
||||
INTERRUPTED = 2
|
||||
#: an internal error got in the way
|
||||
INTERNAL_ERROR = 3
|
||||
#: pytest was missused
|
||||
USAGE_ERROR = 4
|
||||
#: pytest couldnt find tests
|
||||
NO_TESTS_COLLECTED = 5
|
||||
|
||||
|
||||
def pytest_addoption(parser):
|
||||
@@ -188,7 +202,7 @@ def pytest_configure(config):
|
||||
def wrap_session(config, doit):
|
||||
"""Skeleton command line program"""
|
||||
session = Session(config)
|
||||
session.exitstatus = EXIT_OK
|
||||
session.exitstatus = ExitCode.OK
|
||||
initstate = 0
|
||||
try:
|
||||
try:
|
||||
@@ -198,13 +212,13 @@ def wrap_session(config, doit):
|
||||
initstate = 2
|
||||
session.exitstatus = doit(config, session) or 0
|
||||
except UsageError:
|
||||
session.exitstatus = EXIT_USAGEERROR
|
||||
session.exitstatus = ExitCode.USAGE_ERROR
|
||||
raise
|
||||
except Failed:
|
||||
session.exitstatus = EXIT_TESTSFAILED
|
||||
session.exitstatus = ExitCode.TESTS_FAILED
|
||||
except (KeyboardInterrupt, exit.Exception):
|
||||
excinfo = _pytest._code.ExceptionInfo.from_current()
|
||||
exitstatus = EXIT_INTERRUPTED
|
||||
exitstatus = ExitCode.INTERRUPTED
|
||||
if isinstance(excinfo.value, exit.Exception):
|
||||
if excinfo.value.returncode is not None:
|
||||
exitstatus = excinfo.value.returncode
|
||||
@@ -217,7 +231,7 @@ def wrap_session(config, doit):
|
||||
except: # noqa
|
||||
excinfo = _pytest._code.ExceptionInfo.from_current()
|
||||
config.notify_exception(excinfo, config.option)
|
||||
session.exitstatus = EXIT_INTERNALERROR
|
||||
session.exitstatus = ExitCode.INTERNAL_ERROR
|
||||
if excinfo.errisinstance(SystemExit):
|
||||
sys.stderr.write("mainloop: caught unexpected SystemExit!\n")
|
||||
|
||||
@@ -243,9 +257,9 @@ def _main(config, session):
|
||||
config.hook.pytest_runtestloop(session=session)
|
||||
|
||||
if session.testsfailed:
|
||||
return EXIT_TESTSFAILED
|
||||
return ExitCode.TESTS_FAILED
|
||||
elif session.testscollected == 0:
|
||||
return EXIT_NOTESTSCOLLECTED
|
||||
return ExitCode.NO_TESTS_COLLECTED
|
||||
|
||||
|
||||
def pytest_collection(session):
|
||||
|
||||
@@ -19,8 +19,7 @@ from _pytest._io.saferepr import saferepr
|
||||
from _pytest.assertion.rewrite import AssertionRewritingHook
|
||||
from _pytest.capture import MultiCapture
|
||||
from _pytest.capture import SysCapture
|
||||
from _pytest.main import EXIT_INTERRUPTED
|
||||
from _pytest.main import EXIT_OK
|
||||
from _pytest.main import ExitCode
|
||||
from _pytest.main import Session
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
from _pytest.pathlib import Path
|
||||
@@ -691,7 +690,7 @@ class Testdir:
|
||||
p = py.path.local(arg)
|
||||
config.hook.pytest_sessionstart(session=session)
|
||||
res = session.perform_collect([str(p)], genitems=False)[0]
|
||||
config.hook.pytest_sessionfinish(session=session, exitstatus=EXIT_OK)
|
||||
config.hook.pytest_sessionfinish(session=session, exitstatus=ExitCode.OK)
|
||||
return res
|
||||
|
||||
def getpathnode(self, path):
|
||||
@@ -708,11 +707,11 @@ class Testdir:
|
||||
x = session.fspath.bestrelpath(path)
|
||||
config.hook.pytest_sessionstart(session=session)
|
||||
res = session.perform_collect([x], genitems=False)[0]
|
||||
config.hook.pytest_sessionfinish(session=session, exitstatus=EXIT_OK)
|
||||
config.hook.pytest_sessionfinish(session=session, exitstatus=ExitCode.OK)
|
||||
return res
|
||||
|
||||
def genitems(self, colitems):
|
||||
"""Generate all test items from a collection node.
|
||||
"""Generate all test items from a collection node.src/_pytest/main.py
|
||||
|
||||
This recurses into the collection node and returns a list of all the
|
||||
test items contained within.
|
||||
@@ -841,7 +840,7 @@ class Testdir:
|
||||
|
||||
# typically we reraise keyboard interrupts from the child run
|
||||
# because it's our user requesting interruption of the testing
|
||||
if ret == EXIT_INTERRUPTED and not no_reraise_ctrlc:
|
||||
if ret == ExitCode.INTERRUPTED and not no_reraise_ctrlc:
|
||||
calls = reprec.getcalls("pytest_keyboard_interrupt")
|
||||
if calls and calls[-1].excinfo.type == KeyboardInterrupt:
|
||||
raise KeyboardInterrupt()
|
||||
|
||||
@@ -16,11 +16,7 @@ from more_itertools import collapse
|
||||
|
||||
import pytest
|
||||
from _pytest import nodes
|
||||
from _pytest.main import EXIT_INTERRUPTED
|
||||
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
||||
from _pytest.main import EXIT_OK
|
||||
from _pytest.main import EXIT_TESTSFAILED
|
||||
from _pytest.main import EXIT_USAGEERROR
|
||||
from _pytest.main import ExitCode
|
||||
|
||||
REPORT_COLLECTING_RESOLUTION = 0.5
|
||||
|
||||
@@ -654,17 +650,17 @@ class TerminalReporter:
|
||||
outcome.get_result()
|
||||
self._tw.line("")
|
||||
summary_exit_codes = (
|
||||
EXIT_OK,
|
||||
EXIT_TESTSFAILED,
|
||||
EXIT_INTERRUPTED,
|
||||
EXIT_USAGEERROR,
|
||||
EXIT_NOTESTSCOLLECTED,
|
||||
ExitCode.OK,
|
||||
ExitCode.TESTS_FAILED,
|
||||
ExitCode.INTERRUPTED,
|
||||
ExitCode.USAGE_ERROR,
|
||||
ExitCode.NO_TESTS_COLLECTED,
|
||||
)
|
||||
if exitstatus in summary_exit_codes:
|
||||
self.config.hook.pytest_terminal_summary(
|
||||
terminalreporter=self, exitstatus=exitstatus, config=self.config
|
||||
)
|
||||
if exitstatus == EXIT_INTERRUPTED:
|
||||
if exitstatus == ExitCode.INTERRUPTED:
|
||||
self._report_keyboardinterrupt()
|
||||
del self._keyboardinterrupt_memo
|
||||
self.summary_stats()
|
||||
|
||||
Reference in New Issue
Block a user