Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Fix checker and namespace injection
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Apr 5, 2022
1 parent d3fdd95 commit 58e11d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from _pytest.doctest import (
DoctestItem,
DoctestModule,
_get_checker,
_get_continue_on_failure,
_get_runner,
_is_mocked,
Expand All @@ -24,9 +23,11 @@
from _pytest.pathlib import import_path
import inspect

import sage.all # type: ignore # to avoid cyclic import errors, see Trac #33580
from sage.doctest.parsing import SageDocTestParser

# Import sage.all is necessary to:
# - avoid cyclic import errors, see Trac #33580
# - inject it into globals namespace for doctests
import sage.all
from sage.doctest.parsing import SageDocTestParser, SageOutputChecker

def pytest_collect_file(file_path, parent):
if file_path.suffix == ".py":
Expand All @@ -37,7 +38,8 @@ class SageDoctestModule(DoctestModule):
"""
This is essentially a copy of `DoctestModule` from
https://github.com/pytest-dev/pytest/blob/main/src/_pytest/doctest.py.
The only change is that we use `SageDocTestParser` to extract the doctests.
The only change is that we use `SageDocTestParser` to extract the doctests
and `SageOutputChecker` to verify the output.
"""

def collect(self) -> Iterable[DoctestItem]:
Expand Down Expand Up @@ -103,7 +105,7 @@ def _find(
runner = _get_runner(
verbose=False,
optionflags=optionflags,
checker=_get_checker(),
checker=SageOutputChecker(),
continue_on_failure=_get_continue_on_failure(self.config),
)

Expand All @@ -114,9 +116,6 @@ def _find(
)


from sage.all import * # type: ignore # to avoid cyclic import errors, see Trac #33580, and is implicitly used below by calling globals()


@pytest.fixture(autouse=True)
def add_imports(doctest_namespace: dict[str, Any]):
"""
Expand All @@ -125,4 +124,13 @@ def add_imports(doctest_namespace: dict[str, Any]):
See `pytest documentation <https://docs.pytest.org/en/stable/doctest.html#doctest-namespace-fixture>`.
"""
# Inject sage.all into each doctest
doctest_namespace.update(**globals())
dict_all = sage.all.__dict__

# Remove '__package__' item from the globals since it is not
# always in the globals in an actual Sage session.
dict_all.pop('__package__', None)

sage_namespace = dict(dict_all)
sage_namespace['__name__'] = '__main__'

doctest_namespace.update(**sage_namespace)
1 change: 1 addition & 0 deletions src/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ commands = codespell \
python_files = *_test.py
norecursedirs = local prefix venv build pkgs .git src/sage/pkgs src/doc src/bin src/sage/src/sage_setup
addopts = --import-mode importlib
doctest_optionflags = NORMALIZE_WHITESPACE ELLIPSIS
[coverage:run]
source = sage
Expand Down

0 comments on commit 58e11d7

Please sign in to comment.