Skip to content

Commit

Permalink
pytest: Updates for pytest 8
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Jan 28, 2024
1 parent 5010cfe commit ad609ca
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/pytest_doctest_docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,52 @@ def report_unexpected_exception(
return PytestDoctestRunner


def _get_allow_unicode_flag() -> int:
"""Register and return the ALLOW_UNICODE flag."""
import doctest

return doctest.register_optionflag("ALLOW_UNICODE")


def _get_allow_bytes_flag() -> int:
"""Register and return the ALLOW_BYTES flag."""
import doctest

return doctest.register_optionflag("ALLOW_BYTES")


def _get_number_flag() -> int:
"""Register and return the NUMBER flag."""
import doctest

return doctest.register_optionflag("NUMBER")


def _get_flag_lookup() -> t.Dict[str, int]:
import doctest

return {
"DONT_ACCEPT_TRUE_FOR_1": doctest.DONT_ACCEPT_TRUE_FOR_1,
"DONT_ACCEPT_BLANKLINE": doctest.DONT_ACCEPT_BLANKLINE,
"NORMALIZE_WHITESPACE": doctest.NORMALIZE_WHITESPACE,
"ELLIPSIS": doctest.ELLIPSIS,
"IGNORE_EXCEPTION_DETAIL": doctest.IGNORE_EXCEPTION_DETAIL,
"COMPARISON_FLAGS": doctest.COMPARISON_FLAGS,
"ALLOW_UNICODE": _get_allow_unicode_flag(),
"ALLOW_BYTES": _get_allow_bytes_flag(),
"NUMBER": _get_number_flag(),
}


def get_optionflags(config: pytest.Config) -> int:
optionflags_str = str(config.getini("doctest_optionflags"))
flag_lookup_table = _get_flag_lookup()
flag_acc = 0
for flag in optionflags_str:
flag_acc |= flag_lookup_table[flag]
return flag_acc


def _get_runner(
checker: t.Optional["doctest.OutputChecker"] = None,
verbose: t.Optional[bool] = None,
Expand Down Expand Up @@ -227,15 +273,13 @@ def collect(self) -> t.Iterable["DoctestItem"]:
# Uses internal doctest module parsing mechanism.
finder = DocutilsDocTestFinder()

optionflags = _pytest.doctest.get_optionflags(self) # type: ignore
optionflags = get_optionflags(self.config)

runner = _get_runner(
verbose=False,
optionflags=optionflags,
checker=_pytest.doctest._get_checker(),
continue_on_failure=_pytest.doctest._get_continue_on_failure( # type:ignore
self.config
),
continue_on_failure=_pytest.doctest._get_continue_on_failure(self.config),
)
from _pytest.doctest import DoctestItem

Expand Down

0 comments on commit ad609ca

Please sign in to comment.