Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
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.
  • Loading branch information
AtharvaTilewale committed Nov 21, 2026
commit df3ea24247b349d972b334ebce45d2c0d3ced63b
22 changes: 12 additions & 10 deletions packages/pymol-plugin/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@

import pytest

# A present ``pymol`` spec does not guarantee ``import pymol`` succeeds: the import can
# still fail on a missing shared library or an unavailable Qt backend. Guard the real
# import (and the launch + plugin imports it enables) so any such failure ignores this
# package instead of aborting the whole collection session. ``find_spec`` alone cannot
# catch that — it only proves a spec exists.
# A present ``pymol`` spec does not guarantee ``import pymol`` succeeds: the import can still
# fail on a missing shared library or an unavailable Qt backend. Only that dependency check is
# guarded, and only for the errors it raises (ImportError, OSError), so this package is ignored
# when PyMOL is unusable. Launch failures and errors in the plugin's own modules are real
# regressions and must still fail collection rather than turn into a silently skipped package.
try:
import pymol
from pymol import cmd
except (ImportError, OSError):
pymol = None
collect_ignore_glob = ["*.py"]

if pymol is not None:
# Launch PyMOL headless and skip ``~/.pymolrc`` before any ``cmd`` access.
# Without ``-k`` PyMOL sources the user's pymolrc, which for developers who
# ran the plugin's register step re-runs ``pymol_plugin/__init__.py`` as a
# script and calls ``addmenuitemqt`` — unavailable in library mode and raises
# ``QtNotAvailableError``.
import pymol

pymol.finish_launching(["pymol", "-qck"])

from atomworks.io.config import ParseConfig
from atomworks_studio_pymol.bridge import load_structure
from atomworks_studio_pymol.state import STATE
from pymol import cmd
except Exception:
collect_ignore_glob = ["*.py"]

# Anchor: <repo>/packages/pymol-plugin/tests/conftest.py → <repo>
REPO_ROOT = Path(__file__).resolve().parents[3]
Expand Down
39 changes: 39 additions & 0 deletions packages/pymol-plugin/tests/test_review_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,42 @@ def fake_import(name, *args, **kwargs):
exec(compile(source, str(conftest_path), "exec"), ns)

assert ns.get("collect_ignore_glob") == ["*.py"]


def _exec_conftest(monkeypatch, fail_on=None, error=ImportError):
import builtins

conftest_path = Path(__file__).resolve().parent / "conftest.py"
real_import = builtins.__import__

def fake_import(name, *args, **kwargs):
if fail_on is not None and (name == fail_on or name.startswith(fail_on + ".")):
raise error(f"simulated failure importing {name}")
return real_import(name, *args, **kwargs)

monkeypatch.setattr(builtins, "__import__", fake_import)
ns: dict = {"__file__": str(conftest_path), "__name__": "conftest_guard_probe"}
exec(compile(conftest_path.read_text(), str(conftest_path), "exec"), ns)
return ns


def test_conftest_guard_ignores_package_when_pymol_library_fails_to_load(monkeypatch):
ns = _exec_conftest(monkeypatch, fail_on="pymol", error=OSError)
assert ns.get("collect_ignore_glob") == ["*.py"]


def test_conftest_guard_does_not_hide_plugin_import_regressions(monkeypatch):
"""An ImportError in the plugin's own code is a regression, not a missing dependency."""
with pytest.raises(ImportError, match=r"atomworks_studio_pymol\.bridge"):
_exec_conftest(monkeypatch, fail_on="atomworks_studio_pymol.bridge")


def test_conftest_guard_does_not_hide_launch_failures(monkeypatch):
import pymol

def broken_launch(_args):
raise RuntimeError("simulated launch regression")

monkeypatch.setattr(pymol, "finish_launching", broken_launch)
with pytest.raises(RuntimeError, match="launch regression"):
_exec_conftest(monkeypatch)
Loading