-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pytest and handle warnings (#6338)
- Loading branch information
Showing
9 changed files
with
218 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import json | ||
import os | ||
import os.path as osp | ||
import shutil | ||
from importlib.resources import path | ||
from os.path import join as pjoin | ||
|
||
import pytest | ||
|
||
from notebook.app import JupyterNotebookApp | ||
|
||
pytest_plugins = ["jupyter_server.pytest_plugin"] | ||
|
||
|
||
def mkdir(tmp_path, *parts): | ||
path = tmp_path.joinpath(*parts) | ||
if not path.exists(): | ||
path.mkdir(parents=True) | ||
return path | ||
|
||
|
||
app_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "app_settings")) | ||
user_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "user_settings")) | ||
schemas_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "schemas")) | ||
workspaces_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "workspaces")) | ||
labextensions_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "labextensions_dir")) | ||
|
||
|
||
@pytest.fixture | ||
def make_notebook_app( | ||
jp_root_dir, | ||
jp_template_dir, | ||
app_settings_dir, | ||
user_settings_dir, | ||
schemas_dir, | ||
workspaces_dir, | ||
labextensions_dir, | ||
): | ||
def _make_notebook_app(**kwargs): | ||
|
||
return JupyterNotebookApp( | ||
static_dir=str(jp_root_dir), | ||
templates_dir=str(jp_template_dir), | ||
app_url="/", | ||
app_settings_dir=str(app_settings_dir), | ||
user_settings_dir=str(user_settings_dir), | ||
schemas_dir=str(schemas_dir), | ||
workspaces_dir=str(workspaces_dir), | ||
extra_labextensions_path=[str(labextensions_dir)], | ||
) | ||
|
||
# Create the index files. | ||
index = jp_template_dir.joinpath("index.html") | ||
index.write_text( | ||
""" | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>{{page_config['appName'] | e}}</title> | ||
</head> | ||
<body> | ||
{# Copy so we do not modify the page_config with updates. #} | ||
{% set page_config_full = page_config.copy() %} | ||
{# Set a dummy variable - we just want the side effect of the update. #} | ||
{% set _ = page_config_full.update(baseUrl=base_url, wsUrl=ws_url) %} | ||
<script id="jupyter-config-data" type="application/json"> | ||
{{ page_config_full | tojson }} | ||
</script> | ||
<script src="{{page_config['fullStaticUrl'] | e}}/bundle.js" main="index"></script> | ||
<script type="text/javascript"> | ||
/* Remove token from URL. */ | ||
(function () { | ||
var parsedUrl = new URL(window.location.href); | ||
if (parsedUrl.searchParams.get('token')) { | ||
parsedUrl.searchParams.delete('token'); | ||
window.history.replaceState({ }, '', parsedUrl.href); | ||
} | ||
})(); | ||
</script> | ||
</body> | ||
</html> | ||
""" | ||
) | ||
|
||
# Copy the schema files. | ||
test_data = str(path("jupyterlab_server", "test_data")) | ||
src = pjoin(test_data, "schemas", "@jupyterlab") | ||
dst = pjoin(str(schemas_dir), "@jupyterlab") | ||
if os.path.exists(dst): | ||
shutil.rmtree(dst) | ||
shutil.copytree(src, dst) | ||
|
||
# Create the federated extensions | ||
for name in ["apputils-extension", "codemirror-extension"]: | ||
target_name = name + "-federated" | ||
target = pjoin(str(labextensions_dir), "@jupyterlab", target_name) | ||
src = pjoin(test_data, "schemas", "@jupyterlab", name) | ||
dst = pjoin(target, "schemas", "@jupyterlab", target_name) | ||
if osp.exists(dst): | ||
shutil.rmtree(dst) | ||
shutil.copytree(src, dst) | ||
with open(pjoin(target, "package.orig.json"), "w") as fid: | ||
data = dict(name=target_name, jupyterlab=dict(extension=True)) | ||
json.dump(data, fid) | ||
|
||
# Copy the overrides file. | ||
src = pjoin(test_data, "app-settings", "overrides.json") | ||
dst = pjoin(str(app_settings_dir), "overrides.json") | ||
if os.path.exists(dst): | ||
os.remove(dst) | ||
shutil.copyfile(src, dst) | ||
|
||
# Copy workspaces. | ||
data = pjoin(test_data, "workspaces") | ||
for item in os.listdir(data): | ||
src = pjoin(data, item) | ||
dst = pjoin(str(workspaces_dir), item) | ||
if os.path.exists(dst): | ||
os.remove(dst) | ||
shutil.copy(src, str(workspaces_dir)) | ||
|
||
return _make_notebook_app | ||
|
||
|
||
@pytest.fixture | ||
def notebookapp(jp_serverapp, make_notebook_app): | ||
app = make_notebook_app() | ||
app._link_jupyter_server_extension(jp_serverapp) | ||
app.initialize() | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def notebooks(jp_create_notebook, notebookapp): | ||
nbpaths = ( | ||
"notebook1.ipynb", | ||
"jlab_test_notebooks/notebook2.ipynb", | ||
"jlab_test_notebooks/level2/notebook3.ipynb", | ||
) | ||
for nb in nbpaths: | ||
jp_create_notebook(nb) | ||
return nbpaths | ||
|
||
|
||
async def test_notebook_handler(notebooks, jp_fetch): | ||
for nbpath in notebooks: | ||
r = await jp_fetch("/", nbpath) | ||
assert r.code == 200 | ||
# Check that the lab template is loaded | ||
html = r.body.decode() | ||
assert "Jupyter Notebook" in html |