Skip to content

Commit

Permalink
Fix lint + add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vidartf committed Dec 17, 2022
1 parent 0cf2279 commit bb1a363
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jupyterlab_server/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get_page_config(self):
.as_posix()
)
except Exception:
pass
pass # we do not require the preferred_dir trait to be present
# JupyterLab relies on an unset/default path being "/"
page_config["preferredPath"] = preferred_path or "/"

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ filterwarnings = [
"module:make_current is deprecated:DeprecationWarning",
"module:clear_current is deprecated:DeprecationWarning",
"module:There is no current event loop:DeprecationWarning",
"ignore:ServerApp.preferred_dir config is deprecated:FutureWarning",
"ignore:Passing a schema to Validator.iter_errors:DeprecationWarning",
"module:Subclassing validator classes is not intended to be part of their public API:DeprecationWarning"
]
Expand Down
103 changes: 103 additions & 0 deletions tests/test_labapp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Basic tests for the lab handlers.
"""

import json
import re
from pathlib import Path

import pytest
import tornado.httpclient

Expand All @@ -19,6 +23,17 @@ def notebooks(jp_create_notebook, labserverapp):
return nbpaths


def extract_page_config(html):
return json.loads(
re.search(
r'<script id="jupyter-config-data" type="application/json">\s*(?P<data>.*?)\s*</script>',
html,
).group( # type: ignore
'data'
)
)


async def test_lab_handler(notebooks, jp_fetch):
r = await jp_fetch("lab", "jlab_test_notebooks")
assert r.code == 200
Expand All @@ -28,6 +43,94 @@ async def test_lab_handler(notebooks, jp_fetch):
assert "JupyterLab Server Application" in html


async def test_page_config(labserverapp, jp_fetch):
r = await jp_fetch("lab")
assert r.code == 200
# Check that the lab template is loaded
html = r.body.decode()
page_config = extract_page_config(html)
assert page_config['treePath'] == ""
assert page_config['preferredPath'] == "/"

def ispath(p):
return p.endswith("Dir") or p.endswith("Path") or p == "serverRoot"

nondirs = {k: v for k, v in page_config.items() if not ispath(k)}
assert nondirs == {
'appName': 'JupyterLab Server Application',
'appNamespace': 'jupyterlab_server',
'appUrl': '/lab',
'appVersion': '',
'baseUrl': '/a%40b/',
'cacheFiles': True,
'disabledExtensions': [],
'federated_extensions': [],
'fullAppUrl': '/a%40b/lab',
'fullLabextensionsUrl': '/a%40b/lab/extensions',
'fullLicensesUrl': '/a%40b/lab/api/licenses',
'fullListingsUrl': '/a%40b/lab/api/listings',
'fullMathjaxUrl': 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js',
'fullSettingsUrl': '/a%40b/lab/api/settings',
'fullStaticUrl': '/a%40b/static/jupyterlab_server',
'fullThemesUrl': '/a%40b/lab/api/themes',
'fullTranslationsApiUrl': '/a%40b/lab/api/translations',
'fullTreeUrl': '/a%40b/lab/tree',
'fullWorkspacesApiUrl': '/a%40b/lab/api/workspaces',
'ignorePlugins': [],
'labextensionsUrl': '/lab/extensions',
'licensesUrl': '/lab/api/licenses',
'listingsUrl': '/lab/api/listings',
'mathjaxConfig': 'TeX-AMS_HTML-full,Safe',
'mode': 'multiple-document',
'notebookStartsKernel': True,
'settingsUrl': '/lab/api/settings',
'store_id': 0,
'terminalsAvailable': True,
'themesUrl': '/lab/api/themes',
'translationsApiUrl': '/lab/api/translations',
'treeUrl': '/lab/tree',
'workspace': 'default',
'workspacesApiUrl': '/lab/api/workspaces',
'wsUrl': '',
}


@pytest.fixture(scope="function")
def serverapp_preferred_dir(jp_server_config, jp_root_dir):
preferred_dir = Path(jp_root_dir, "my", "preferred_dir")
preferred_dir.mkdir(parents=True, exist_ok=True)
jp_server_config.ServerApp.preferred_dir = str(preferred_dir)
return preferred_dir


async def test_app_preferred_dir(serverapp_preferred_dir, labserverapp, jp_fetch):
r = await jp_fetch("lab")
assert r.code == 200
# Check that the lab template is loaded
html = r.body.decode()
page_config = extract_page_config(html)
api_path = str(serverapp_preferred_dir.relative_to(labserverapp.serverapp.root_dir).as_posix())
assert page_config['preferredPath'] == api_path


async def test_contents_manager_preferred_dir(jp_root_dir, labserverapp, jp_fetch):
preferred_dir = Path(jp_root_dir, "my", "preferred_dir")
preferred_dir.mkdir(parents=True, exist_ok=True)
try:
_ = labserverapp.serverapp.contents_manager.preferred_dir
labserverapp.serverapp.contents_manager.preferred_dir = str(preferred_dir)
except AttributeError:
pytest.skip("Skipping contents manager test, trait not present")

r = await jp_fetch("lab")
assert r.code == 200
# Check that the lab template is loaded
html = r.body.decode()
page_config = extract_page_config(html)
api_path = str(preferred_dir.relative_to(labserverapp.serverapp.root_dir).as_posix())
assert page_config['preferredPath'] == api_path


async def test_notebook_handler(notebooks, jp_fetch):
for nbpath in notebooks:
r = await jp_fetch("lab", nbpath)
Expand Down

0 comments on commit bb1a363

Please sign in to comment.