Skip to content

Commit f2e87c8

Browse files
Only skip the PyMOL tests when PyMOL itself is unusable
Addresses the second review on #22: the conftest caught every Exception, so a regression in finish_launching() or in the plugin's own imports would quietly ignore the whole PyMOL test package and give a false-green run. The guard now covers only the dependency check (import pymol, from pymol import cmd) and only the errors it raises (ImportError, OSError). Launch failures and plugin import errors propagate and fail collection. Tests cover both kinds of unusable PyMOL and both kinds of regression.
1 parent 897c50f commit f2e87c8

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎packages/pymol-plugin/tests/conftest.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,29 @@
99

1010
import pytest
1111

12-
# A present ``pymol`` spec does not guarantee ``import pymol`` succeeds: the import can
13-
# still fail on a missing shared library or an unavailable Qt backend. Guard the real
14-
# import (and the launch + plugin imports it enables) so any such failure ignores this
15-
# package instead of aborting the whole collection session. ``find_spec`` alone cannot
16-
# catch that — it only proves a spec exists.
12+
# A present ``pymol`` spec does not guarantee ``import pymol`` succeeds: the import can still
13+
# fail on a missing shared library or an unavailable Qt backend. Only that dependency check is
14+
# guarded, and only for the errors it raises (ImportError, OSError), so this package is ignored
15+
# when PyMOL is unusable. Launch failures and errors in the plugin's own modules are real
16+
# regressions and must still fail collection rather than turn into a silently skipped package.
1717
try:
18+
import pymol
19+
from pymol import cmd
20+
except (ImportError, OSError):
21+
pymol = None
22+
collect_ignore_glob = ["*.py"]
23+
24+
if pymol is not None:
1825
# Launch PyMOL headless and skip ``~/.pymolrc`` before any ``cmd`` access.
1926
# Without ``-k`` PyMOL sources the user's pymolrc, which for developers who
2027
# ran the plugin's register step re-runs ``pymol_plugin/__init__.py`` as a
2128
# script and calls ``addmenuitemqt`` — unavailable in library mode and raises
2229
# ``QtNotAvailableError``.
23-
import pymol
24-
2530
pymol.finish_launching(["pymol", "-qck"])
2631

2732
from atomworks.io.config import ParseConfig
2833
from atomworks_studio_pymol.bridge import load_structure
2934
from atomworks_studio_pymol.state import STATE
30-
from pymol import cmd
31-
except Exception:
32-
collect_ignore_glob = ["*.py"]
3335

3436
# Anchor: <repo>/packages/pymol-plugin/tests/conftest.py → <repo>
3537
REPO_ROOT = Path(__file__).resolve().parents[3]

‎packages/pymol-plugin/tests/test_review_fixes.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,42 @@ def fake_import(name, *args, **kwargs):
399399
exec(compile(source, str(conftest_path), "exec"), ns)
400400

401401
assert ns.get("collect_ignore_glob") == ["*.py"]
402+
403+
404+
def _exec_conftest(monkeypatch, fail_on=None, error=ImportError):
405+
import builtins
406+
407+
conftest_path = Path(__file__).resolve().parent / "conftest.py"
408+
real_import = builtins.__import__
409+
410+
def fake_import(name, *args, **kwargs):
411+
if fail_on is not None and (name == fail_on or name.startswith(fail_on + ".")):
412+
raise error(f"simulated failure importing {name}")
413+
return real_import(name, *args, **kwargs)
414+
415+
monkeypatch.setattr(builtins, "__import__", fake_import)
416+
ns: dict = {"__file__": str(conftest_path), "__name__": "conftest_guard_probe"}
417+
exec(compile(conftest_path.read_text(), str(conftest_path), "exec"), ns)
418+
return ns
419+
420+
421+
def test_conftest_guard_ignores_package_when_pymol_library_fails_to_load(monkeypatch):
422+
ns = _exec_conftest(monkeypatch, fail_on="pymol", error=OSError)
423+
assert ns.get("collect_ignore_glob") == ["*.py"]
424+
425+
426+
def test_conftest_guard_does_not_hide_plugin_import_regressions(monkeypatch):
427+
"""An ImportError in the plugin's own code is a regression, not a missing dependency."""
428+
with pytest.raises(ImportError, match=r"atomworks_studio_pymol\.bridge"):
429+
_exec_conftest(monkeypatch, fail_on="atomworks_studio_pymol.bridge")
430+
431+
432+
def test_conftest_guard_does_not_hide_launch_failures(monkeypatch):
433+
import pymol
434+
435+
def broken_launch(_args):
436+
raise RuntimeError("simulated launch regression")
437+
438+
monkeypatch.setattr(pymol, "finish_launching", broken_launch)
439+
with pytest.raises(RuntimeError, match="launch regression"):
440+
_exec_conftest(monkeypatch)

0 commit comments

Comments
 (0)