Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for serverapp for reraise flag #887

Merged
merged 10 commits into from
Aug 29, 2022
6 changes: 3 additions & 3 deletions jupyter_server/extension/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)

Expand All @@ -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)
Expand Down
24 changes: 14 additions & 10 deletions tests/extension/test_manager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)