From efdb2157704543be5c3f4a42c3b1af89cf2e1cb1 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Mon, 30 Oct 2023 12:08:12 +0000 Subject: [PATCH 1/5] Test that everything can be imported during ci --- .github/workflows/build.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5242dfc06f9..10e0f5e31b3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -162,6 +162,15 @@ jobs: MAKE: make -j2 --output-sync=recurse SAGE_NUM_THREADS: 2 + - name: Imports + run: | + ../sage -python -m pip install pytest-xdist + ../sage -python -m pytest -c tox.ini --doctest-modules --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: | From 7d9b2a13370b2eb1a5981978eab646f05368c128 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Tue, 31 Oct 2023 09:28:59 +0000 Subject: [PATCH 2/5] Silence output as much as possible --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 10e0f5e31b3..293fe3eea6f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -165,7 +165,7 @@ jobs: - name: Imports run: | ../sage -python -m pip install pytest-xdist - ../sage -python -m pytest -c tox.ini --doctest-modules --collect-only || true + ../sage -python -m pytest -c tox.ini -qq --doctest-modules --collect-only || true working-directory: ./worktree-image/src env: # Increase the length of the lines in the "short summary" From e84cb91724ea912efeaf0b8ea8851aa4a1492137 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Tue, 21 Nov 2023 06:34:02 +0000 Subject: [PATCH 3/5] Skip a few files from doctestsing with pytest --- .github/workflows/build.yml | 4 ++-- .vscode/launch.json | 16 +++++++++++++++- src/bin/sage | 4 ++-- src/conftest.py | 23 ++++++++++++++++++++--- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6ad1b62efe7..47b3e1b765a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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" @@ -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" diff --git a/.vscode/launch.json b/.vscode/launch.json index 5f3e9a4b909..ee84532d4af 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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", @@ -33,4 +47,4 @@ "justMyCode": false } ], -} \ No newline at end of file +} diff --git a/src/bin/sage b/src/bin/sage index 64d7986482b..045b5e45317 100755 --- a/src/bin/sage +++ b/src/bin/sage @@ -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 diff --git a/src/conftest.py b/src/conftest.py index c7423f89521..17af5da768d 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -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 @@ -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]): """ From f051ae1e2439da327bcc9fcafb10561e6311d127 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Thu, 28 Dec 2023 20:47:45 +0800 Subject: [PATCH 4/5] Update .github/workflows/build.yml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Matthias Köppe --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f8605ffb293..255d57b0c87 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -172,7 +172,7 @@ jobs: MAKE: make -j2 --output-sync=recurse SAGE_NUM_THREADS: 2 - - name: Imports + - name: Check that all modules can be imported run: | ../sage -python -m pip install pytest-xdist ../sage -python -m pytest -c tox.ini -qq --doctest --collect-only || true From 97a25485a5587f853e92ac66a8311a14439484d7 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Thu, 28 Dec 2023 12:56:04 +0000 Subject: [PATCH 5/5] Add message to explain long list of modules during check --- .github/workflows/build.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 255d57b0c87..56f231d9578 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -174,6 +174,9 @@ jobs: - 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. ../sage -python -m pip install pytest-xdist ../sage -python -m pytest -c tox.ini -qq --doctest --collect-only || true working-directory: ./worktree-image/src