diff --git a/jupyter_server/extension/manager.py b/jupyter_server/extension/manager.py index e660b3dc14..e824d99729 100644 --- a/jupyter_server/extension/manager.py +++ b/jupyter_server/extension/manager.py @@ -322,7 +322,7 @@ def add_extension(self, extension_name, enabled=False): return True # Raise a warning if the extension cannot be loaded. except Exception as e: - if self.serverapp.reraise_server_extension_failures: + if self.serverapp and self.serverapp.reraise_server_extension_failures: raise self.log.warning( "%s | error adding extension (enabled: %s): %s", @@ -343,7 +343,7 @@ def link_extension(self, name): self.linked_extensions[name] = True self.log.info("%s | extension was successfully linked.", name) except Exception as e: - if self.serverapp.reraise_server_extension_failures: + if self.serverapp and self.serverapp.reraise_server_extension_failures: raise self.log.warning("%s | error linking extension: %s", name, e, exc_info=True) @@ -354,7 +354,7 @@ def load_extension(self, name): try: extension.load_all_points(self.serverapp) except Exception as e: - if self.serverapp.reraise_server_extension_failures: + if self.serverapp and self.serverapp.reraise_server_extension_failures: raise self.log.warning("%s | extension failed loading with message: %s", name, e) self.log.exception("%s | stack trace", name) diff --git a/tests/extension/test_manager.py b/tests/extension/test_manager.py index 2b52fea543..a6edddae15 100644 --- a/tests/extension/test_manager.py +++ b/tests/extension/test_manager.py @@ -1,5 +1,6 @@ import os import unittest.mock as mock +from contextlib import nullcontext import pytest from jupyter_core.paths import jupyter_config_path @@ -96,39 +97,42 @@ def test_extension_manager_linked_extensions(jp_serverapp): assert name in manager.linked_extensions -def test_extension_manager_fail_add(jp_serverapp): +@pytest.mark.parametrize("has_app", [True, False]) +def test_extension_manager_fail_add(jp_serverapp, has_app): name = "tests.extension.notanextension" - manager = ExtensionManager(serverapp=jp_serverapp) + manager = ExtensionManager(serverapp=jp_serverapp if has_app else None) manager.add_extension(name, enabled=True) # should only warn jp_serverapp.reraise_server_extension_failures = True - with pytest.raises(ExtensionModuleNotFound): - manager.add_extension(name, enabled=True) + with pytest.raises(ExtensionModuleNotFound) if has_app else nullcontext(): + assert manager.add_extension(name, enabled=True) is False -def test_extension_manager_fail_link(jp_serverapp): +@pytest.mark.parametrize("has_app", [True, False]) +def test_extension_manager_fail_link(jp_serverapp, has_app): name = "tests.extension.mockextensions.app" with mock.patch( "tests.extension.mockextensions.app.MockExtensionApp.parse_command_line", side_effect=RuntimeError, ): - manager = ExtensionManager(serverapp=jp_serverapp) + manager = ExtensionManager(serverapp=jp_serverapp if has_app else None) manager.add_extension(name, enabled=True) manager.link_extension(name) # should only warn jp_serverapp.reraise_server_extension_failures = True - with pytest.raises(RuntimeError): + with pytest.raises(RuntimeError) if has_app else nullcontext(): manager.link_extension(name) -def test_extension_manager_fail_load(jp_serverapp): +@pytest.mark.parametrize("has_app", [True, False]) +def test_extension_manager_fail_load(jp_serverapp, has_app): name = "tests.extension.mockextensions.app" with mock.patch( "tests.extension.mockextensions.app.MockExtensionApp.initialize_handlers", side_effect=RuntimeError, ): - manager = ExtensionManager(serverapp=jp_serverapp) + manager = ExtensionManager(serverapp=jp_serverapp if has_app else None) manager.add_extension(name, enabled=True) manager.link_extension(name) manager.load_extension(name) # should only warn jp_serverapp.reraise_server_extension_failures = True - with pytest.raises(RuntimeError): + with pytest.raises(RuntimeError) if has_app else nullcontext(): manager.load_extension(name)