|
| 1 | +import json |
| 2 | +import os |
| 3 | +import os.path as osp |
| 4 | +import shutil |
| 5 | +from importlib.resources import path |
| 6 | +from os.path import join as pjoin |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from notebook.app import JupyterNotebookApp |
| 11 | + |
| 12 | +pytest_plugins = ["jupyter_server.pytest_plugin"] |
| 13 | + |
| 14 | + |
| 15 | +def mkdir(tmp_path, *parts): |
| 16 | + path = tmp_path.joinpath(*parts) |
| 17 | + if not path.exists(): |
| 18 | + path.mkdir(parents=True) |
| 19 | + return path |
| 20 | + |
| 21 | + |
| 22 | +app_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "app_settings")) |
| 23 | +user_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "user_settings")) |
| 24 | +schemas_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "schemas")) |
| 25 | +workspaces_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "workspaces")) |
| 26 | +labextensions_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "labextensions_dir")) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def make_notebook_app( |
| 31 | + jp_root_dir, |
| 32 | + jp_template_dir, |
| 33 | + app_settings_dir, |
| 34 | + user_settings_dir, |
| 35 | + schemas_dir, |
| 36 | + workspaces_dir, |
| 37 | + labextensions_dir, |
| 38 | +): |
| 39 | + def _make_notebook_app(**kwargs): |
| 40 | + |
| 41 | + return JupyterNotebookApp( |
| 42 | + static_dir=str(jp_root_dir), |
| 43 | + templates_dir=str(jp_template_dir), |
| 44 | + app_url="/", |
| 45 | + app_settings_dir=str(app_settings_dir), |
| 46 | + user_settings_dir=str(user_settings_dir), |
| 47 | + schemas_dir=str(schemas_dir), |
| 48 | + workspaces_dir=str(workspaces_dir), |
| 49 | + extra_labextensions_path=[str(labextensions_dir)], |
| 50 | + ) |
| 51 | + |
| 52 | + # Create the index files. |
| 53 | + index = jp_template_dir.joinpath("index.html") |
| 54 | + index.write_text( |
| 55 | + """ |
| 56 | +<!DOCTYPE html> |
| 57 | +<html> |
| 58 | +<head> |
| 59 | + <title>{{page_config['appName'] | e}}</title> |
| 60 | +</head> |
| 61 | +<body> |
| 62 | + {# Copy so we do not modify the page_config with updates. #} |
| 63 | + {% set page_config_full = page_config.copy() %} |
| 64 | + {# Set a dummy variable - we just want the side effect of the update. #} |
| 65 | + {% set _ = page_config_full.update(baseUrl=base_url, wsUrl=ws_url) %} |
| 66 | + <script id="jupyter-config-data" type="application/json"> |
| 67 | + {{ page_config_full | tojson }} |
| 68 | + </script> |
| 69 | + <script src="{{page_config['fullStaticUrl'] | e}}/bundle.js" main="index"></script> |
| 70 | + <script type="text/javascript"> |
| 71 | + /* Remove token from URL. */ |
| 72 | + (function () { |
| 73 | + var parsedUrl = new URL(window.location.href); |
| 74 | + if (parsedUrl.searchParams.get('token')) { |
| 75 | + parsedUrl.searchParams.delete('token'); |
| 76 | + window.history.replaceState({ }, '', parsedUrl.href); |
| 77 | + } |
| 78 | + })(); |
| 79 | + </script> |
| 80 | +</body> |
| 81 | +</html> |
| 82 | +""" |
| 83 | + ) |
| 84 | + |
| 85 | + # Copy the schema files. |
| 86 | + test_data = str(path("jupyterlab_server", "test_data")) |
| 87 | + src = pjoin(test_data, "schemas", "@jupyterlab") |
| 88 | + dst = pjoin(str(schemas_dir), "@jupyterlab") |
| 89 | + if os.path.exists(dst): |
| 90 | + shutil.rmtree(dst) |
| 91 | + shutil.copytree(src, dst) |
| 92 | + |
| 93 | + # Create the federated extensions |
| 94 | + for name in ["apputils-extension", "codemirror-extension"]: |
| 95 | + target_name = name + "-federated" |
| 96 | + target = pjoin(str(labextensions_dir), "@jupyterlab", target_name) |
| 97 | + src = pjoin(test_data, "schemas", "@jupyterlab", name) |
| 98 | + dst = pjoin(target, "schemas", "@jupyterlab", target_name) |
| 99 | + if osp.exists(dst): |
| 100 | + shutil.rmtree(dst) |
| 101 | + shutil.copytree(src, dst) |
| 102 | + with open(pjoin(target, "package.orig.json"), "w") as fid: |
| 103 | + data = dict(name=target_name, jupyterlab=dict(extension=True)) |
| 104 | + json.dump(data, fid) |
| 105 | + |
| 106 | + # Copy the overrides file. |
| 107 | + src = pjoin(test_data, "app-settings", "overrides.json") |
| 108 | + dst = pjoin(str(app_settings_dir), "overrides.json") |
| 109 | + if os.path.exists(dst): |
| 110 | + os.remove(dst) |
| 111 | + shutil.copyfile(src, dst) |
| 112 | + |
| 113 | + # Copy workspaces. |
| 114 | + data = pjoin(test_data, "workspaces") |
| 115 | + for item in os.listdir(data): |
| 116 | + src = pjoin(data, item) |
| 117 | + dst = pjoin(str(workspaces_dir), item) |
| 118 | + if os.path.exists(dst): |
| 119 | + os.remove(dst) |
| 120 | + shutil.copy(src, str(workspaces_dir)) |
| 121 | + |
| 122 | + return _make_notebook_app |
| 123 | + |
| 124 | + |
| 125 | +@pytest.fixture |
| 126 | +def notebookapp(jp_serverapp, make_notebook_app): |
| 127 | + app = make_notebook_app() |
| 128 | + app._link_jupyter_server_extension(jp_serverapp) |
| 129 | + app.initialize() |
| 130 | + return app |
0 commit comments