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