|
| 1 | +"""`OO_REQUIRE_FIXTURES=1` fails a run in which a module skipped at COLLECTION. |
| 2 | +
|
| 3 | +A module-level skip happens before any test item exists, so only |
| 4 | +`pytest_collectreport` sees it. `conftest.py` used to rewrite the report there, |
| 5 | +and that did not reach the exit status: one passing test plus one such module |
| 6 | +gave `1 passed, 1 skipped` and exit 0 under the variable. Four files in the |
| 7 | +strict leg of the CI `python` job skip this way (`test_shacl.py`, |
| 8 | +`test_shacl_vacuous.py`, `test_align.py`, `test_vocab_check.py`), so that leg |
| 9 | +stayed green if their extras went missing. |
| 10 | +
|
| 11 | +Each test runs pytest in a subprocess, on a throwaway directory that holds a |
| 12 | +COPY of this suite's own `conftest.py`. The hook under test is the shipped one, |
| 13 | +not a restatement of it. |
| 14 | +""" |
| 15 | + |
| 16 | +import os |
| 17 | +import shutil |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +CONFTEST = Path(__file__).with_name("conftest.py") |
| 23 | + |
| 24 | +PASSES = "def test_passes():\n assert True\n" |
| 25 | + |
| 26 | +SKIPS_AT_COLLECTION = ( |
| 27 | + "import pytest\n" |
| 28 | + "pytest.importorskip('a_module_that_is_installed_nowhere')\n" |
| 29 | + "\n" |
| 30 | + "def test_never_runs():\n" |
| 31 | + " assert True\n" |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +def _run(tmp_path, strict): |
| 36 | + shutil.copy(CONFTEST, tmp_path / "conftest.py") |
| 37 | + (tmp_path / "test_passes.py").write_text(PASSES) |
| 38 | + (tmp_path / "test_skips_at_collection.py").write_text(SKIPS_AT_COLLECTION) |
| 39 | + env = dict(os.environ) |
| 40 | + env.pop("OO_REQUIRE_FIXTURES", None) |
| 41 | + env.pop("PYTEST_ADDOPTS", None) |
| 42 | + if strict: |
| 43 | + env["OO_REQUIRE_FIXTURES"] = "1" |
| 44 | + return subprocess.run( |
| 45 | + [sys.executable, "-m", "pytest", "-q", "-p", "no:cacheprovider", |
| 46 | + "--rootdir", str(tmp_path), str(tmp_path)], |
| 47 | + cwd=tmp_path, |
| 48 | + env=env, |
| 49 | + capture_output=True, |
| 50 | + text=True, |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def test_a_skip_at_collection_fails_the_strict_run(tmp_path): |
| 55 | + r = _run(tmp_path, strict=True) |
| 56 | + assert r.returncode == 1, r.stdout + r.stderr |
| 57 | + assert "test_skips_at_collection.py" in r.stdout, r.stdout |
| 58 | + |
| 59 | + |
| 60 | +def test_the_same_skip_passes_without_the_variable(tmp_path): |
| 61 | + """The control. A conftest that failed EVERY run would pass the test above.""" |
| 62 | + r = _run(tmp_path, strict=False) |
| 63 | + assert r.returncode == 0, r.stdout + r.stderr |
| 64 | + assert "1 passed, 1 skipped" in r.stdout, r.stdout |
0 commit comments