Skip to content

Validate extension name before toggling through CLI #1509

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 16 additions & 2 deletions jupyter_server/extension/serverextension.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,23 @@ def toggle_server_extension(self, import_name: str) -> None:
self.log.info(f"- Writing config: {config_dir}")
# Validate the server extension.
self.log.info(f" - Validating {import_name}...")
config = extension_manager.config_manager
enabled = False
if config:
jpserver_extensions = config.get_jpserver_extensions()
if import_name not in jpserver_extensions:
msg = (
f"The module '{import_name}' could not be found. Are you "
"sure the extension is installed?"
)
raise ValueError(msg)
enabled = jpserver_extensions[import_name]

# Interface with the Extension Package and validate.
extpkg = ExtensionPackage(name=import_name)
extpkg.validate()
extpkg = ExtensionPackage(name=import_name, enabled=enabled)
if not extpkg.validate():
msg = "validation failed"
raise ValueError(msg)
version = extpkg.version
self.log.info(f" {import_name} {version} {GREEN_OK}")

Expand Down
8 changes: 8 additions & 0 deletions tests/extension/test_serverextension.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,11 @@ def test_server_extension_apps(jp_env_config_path, jp_extension_environ):
def test_server_extension_app():
app = ServerExtensionApp()
app.launch_instance(["list"])


def test_toggle_missing_server_extension(jp_env_config_path, capsys):
app = ToggleServerExtensionApp()
app.extra_args = ["missingextension"]
app.start()
captured = capsys.readouterr()
assert "Validation failed: The module 'missingextension' could not be found." in captured.err
Loading