Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,23 @@ jobs:
MAKE: make -j2 --output-sync=recurse
SAGE_NUM_THREADS: 2

- name: Check that all modules can be imported
run: |
# The following command checks that all modules can be imported.
# The output also includes a long list of modules together with the number of tests in each module.
# This can be ignored.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the output intended? Do you use this information? Or should everyone ignore it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe to ignore (but pytest doesn't support a way to hide it)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine. I assume that you mean that you also do not use this information. We'll iterate the cosmetics in follow-up PRs.

../sage -python -m pip install pytest-xdist
../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"
COLUMNS: 120

- name: Pytest
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 @@ -1019,10 +1019,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"
exit 1
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