Skip to content

Commit

Permalink
Skip a few files from doctestsing with pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez authored Nov 21, 2023
1 parent 259e1a6 commit e84cb91
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ jobs:
- name: Imports
run: |
../sage -python -m pip install pytest-xdist
../sage -python -m pytest -c tox.ini -qq --doctest-modules --collect-only || true
../sage -python -m pytest -c tox.ini -qq --doctest --collect-only || true
working-directory: ./worktree-image/src
env:
# Increase the length of the lines in the "short summary"
Expand All @@ -184,7 +184,7 @@ jobs:
if: contains(github.ref, 'pytest')
run: |
../sage -python -m pip install coverage pytest-xdist
../sage -python -m coverage run -m pytest -c tox.ini --doctest-modules || true
../sage -python -m coverage run -m pytest -c tox.ini --doctest || true
working-directory: ./worktree-image/src
env:
# Increase the length of the lines in the "short summary"
Expand Down
16 changes: 15 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Sage: Pytest Doctests",
"type": "python",
"request": "launch",
"module": "pytest",
"args": [
"-c",
"src/tox.ini",
"--doctest",
"${file}"
],
"console": "integratedTerminal",
"justMyCode": false
},
{
"name": "Sage: Pytest",
"type": "python",
Expand Down Expand Up @@ -33,4 +47,4 @@
"justMyCode": false
}
],
}
}
4 changes: 2 additions & 2 deletions src/bin/sage
Original file line number Diff line number Diff line change
Expand Up @@ -1008,10 +1008,10 @@ if [ "$1" = '-pytest' -o "$1" = '--pytest' ]; then
for a in $*; do
case $a in
-*) ;;
*) exec pytest --rootdir="$SAGE_SRC" --doctest-modules "$@"
*) exec pytest --rootdir="$SAGE_SRC" --doctest "$@"
esac
done
exec pytest --rootdir="$SAGE_SRC" --doctest-modules "$@" "$SAGE_SRC"
exec pytest --rootdir="$SAGE_SRC" --doctest "$@" "$SAGE_SRC"
else
echo "Run 'sage -i pytest' to install"
fi
Expand Down
23 changes: 20 additions & 3 deletions src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
_patch_unwrap_mock_aware,
get_optionflags,
)
from _pytest.pathlib import import_path, ImportMode

from _pytest.pathlib import ImportMode, import_path
from sage.doctest.parsing import SageDocTestParser, SageOutputChecker


Expand Down Expand Up @@ -141,10 +140,28 @@ def pytest_collect_file(
# hit this here if someone explicitly runs `pytest some_file.pyx`.
return IgnoreCollector.from_parent(parent)
elif file_path.suffix == ".py":
if parent.config.option.doctestmodules:
if parent.config.option.doctest:
if file_path.name == "__main__.py":
# We don't allow tests to be defined in __main__.py files (because their import will fail).
return IgnoreCollector.from_parent(parent)
if file_path.name == "postprocess.py" and file_path.parent.name == "nbconvert":
# This is an executable file.
return IgnoreCollector.from_parent(parent)
return SageDoctestModule.from_parent(parent, path=file_path)


def pytest_addoption(parser):
# Add a command line option to run doctests
# (we don't use the built-in --doctest-modules option because then doctests are collected twice)
group = parser.getgroup("collect")
group.addoption(
"--doctest",
action="store_true",
default=False,
help="Run doctests in all .py modules",
dest="doctest",
)

@pytest.fixture(autouse=True, scope="session")
def add_imports(doctest_namespace: dict[str, Any]):
"""
Expand Down

0 comments on commit e84cb91

Please sign in to comment.