diff --git a/napari/utils/_tests/test_theme.py b/napari/utils/_tests/test_theme.py index b18bbb26512..eeca022070b 100644 --- a/napari/utils/_tests/test_theme.py +++ b/napari/utils/_tests/test_theme.py @@ -2,12 +2,17 @@ import sys import pytest +from npe2 import PluginManager, PluginManifest +from npe2 import __version__ as npe2_version +from npe2.manifest.schema import ContributionPoints +from packaging.version import parse as parse_version from pydantic import ValidationError from napari.resources._icons import PLUGIN_FILE_NAME from napari.settings import get_settings from napari.utils.theme import ( Theme, + _install_npe2_themes, available_themes, get_theme, is_theme_available, @@ -160,3 +165,47 @@ def mock_install_theme(_themes): assert is_theme_available("test_blue") assert len(available_themes()) == n_themes + 1 assert "test_blue" in available_themes() + + +@pytest.mark.skipif( + parse_version(npe2_version) < parse_version("0.6.2"), + reason="requires npe2 0.6.2 for syntax style contributions", +) +def test_theme_registration(monkeypatch, caplog): + data = {"dark": get_theme("dark", as_dict=False)} + + manifest = PluginManifest( + name="theme_test", + display_name="Theme Test", + contributions=ContributionPoints( + themes=[ + { + "id": "theme1", + "label": "Theme 1", + "type": "dark", + "syntax_style": "native", + "colors": {}, + }, + { + "id": "theme2", + "label": "Theme 2", + "type": "dark", + "syntax_style": "does_not_exist", + "colors": {}, + }, + ] + ), + ) + + def mock_iter_manifests(disabled): + return [manifest] + + monkeypatch.setattr( + PluginManager.instance(), "iter_manifests", mock_iter_manifests + ) + monkeypatch.setattr("napari.utils.theme._themes", data) + _install_npe2_themes(data) + + assert "theme1" in data + assert "theme2" not in data + assert "Registration theme failed" in caplog.text diff --git a/napari/utils/theme.py b/napari/utils/theme.py index 47a7fded955..8866a24e312 100644 --- a/napari/utils/theme.py +++ b/napari/utils/theme.py @@ -1,5 +1,6 @@ # syntax_style for the console must be one of the supported styles from # pygments - see here for examples https://help.farbox.com/pygments.html +import logging import re import warnings from ast import literal_eval @@ -386,7 +387,10 @@ def _install_npe2_themes(themes=None): theme_colors = theme.colors.dict(exclude_unset=True) theme_dict.update(theme_info) theme_dict.update(theme_colors) - register_theme(theme.id, theme_dict, manifest.name) + try: + register_theme(theme.id, theme_dict, manifest.name) + except ValueError as e: + logging.exception("Registration theme failed.\n%s", e) _install_npe2_themes(_themes)