pytest_plugin.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """pytest fixtures."""
  4. from __future__ import annotations
  5. import json
  6. import os
  7. import os.path as osp
  8. import shutil
  9. from os.path import join as pjoin
  10. from pathlib import Path
  11. from typing import Any, Callable
  12. import pytest
  13. from jupyter_server.serverapp import ServerApp
  14. from jupyterlab_server import LabServerApp
  15. pytest_plugins = ["pytest_jupyter.jupyter_server"]
  16. def mkdir(tmp_path: Path, *parts: str) -> Path:
  17. """Util for making a directory."""
  18. path = tmp_path.joinpath(*parts)
  19. if not path.exists():
  20. path.mkdir(parents=True)
  21. return path
  22. HERE = os.path.abspath(os.path.dirname(__file__))
  23. app_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "app_settings"))
  24. user_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "user_settings"))
  25. schemas_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "schemas"))
  26. workspaces_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "workspaces"))
  27. labextensions_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "labextensions_dir"))
  28. @pytest.fixture
  29. def make_labserver_extension_app(
  30. jp_root_dir: Path,
  31. jp_template_dir: Path,
  32. app_settings_dir: Path,
  33. user_settings_dir: Path,
  34. schemas_dir: Path,
  35. workspaces_dir: Path,
  36. labextensions_dir: Path,
  37. ) -> Callable[..., LabServerApp]:
  38. """Return a factory function for a labserver extension app."""
  39. def _make_labserver_extension_app(**kwargs: Any) -> LabServerApp: # noqa: ARG001
  40. """Factory function for lab server extension apps."""
  41. return LabServerApp(
  42. static_dir=str(jp_root_dir),
  43. templates_dir=str(jp_template_dir),
  44. app_url="/lab",
  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. # Create the index files.
  52. index = jp_template_dir.joinpath("index.html")
  53. index.write_text(
  54. """
  55. <!DOCTYPE html>
  56. <html>
  57. <head>
  58. <title>{{page_config['appName'] | e}}</title>
  59. </head>
  60. <body>
  61. {# Copy so we do not modify the page_config with updates. #}
  62. {% set page_config_full = page_config.copy() %}
  63. {# Set a dummy variable - we just want the side effect of the update. #}
  64. {% set _ = page_config_full.update(baseUrl=base_url, wsUrl=ws_url) %}
  65. <script id="jupyter-config-data" type="application/json">
  66. {{ page_config_full | tojson }}
  67. </script>
  68. <script src="{{page_config['fullStaticUrl'] | e}}/bundle.js" main="index"></script>
  69. <script type="text/javascript">
  70. /* Remove token from URL. */
  71. (function () {
  72. var parsedUrl = new URL(window.location.href);
  73. if (parsedUrl.searchParams.get('token')) {
  74. parsedUrl.searchParams.delete('token');
  75. window.history.replaceState({ }, '', parsedUrl.href);
  76. }
  77. })();
  78. </script>
  79. </body>
  80. </html>
  81. """
  82. )
  83. # Copy the schema files.
  84. src = pjoin(HERE, "test_data", "schemas", "@jupyterlab")
  85. dst = pjoin(str(schemas_dir), "@jupyterlab")
  86. if os.path.exists(dst):
  87. shutil.rmtree(dst)
  88. shutil.copytree(src, dst)
  89. # Create the federated extensions
  90. for name in ["apputils-extension", "codemirror-extension"]:
  91. target_name = name + "-federated"
  92. target = pjoin(str(labextensions_dir), "@jupyterlab", target_name)
  93. src = pjoin(HERE, "test_data", "schemas", "@jupyterlab", name)
  94. dst = pjoin(target, "schemas", "@jupyterlab", target_name)
  95. if osp.exists(dst):
  96. shutil.rmtree(dst)
  97. shutil.copytree(src, dst)
  98. with open(pjoin(target, "package.orig.json"), "w") as fid:
  99. data = dict(name=target_name, jupyterlab=dict(extension=True))
  100. json.dump(data, fid)
  101. # Copy the overrides file.
  102. src = pjoin(HERE, "test_data", "app-settings", "overrides.json")
  103. dst = pjoin(str(app_settings_dir), "overrides.json")
  104. if os.path.exists(dst):
  105. os.remove(dst)
  106. shutil.copyfile(src, dst)
  107. # Copy workspaces.
  108. ws_path = pjoin(HERE, "test_data", "workspaces")
  109. for item in os.listdir(ws_path):
  110. src = pjoin(ws_path, item)
  111. dst = pjoin(str(workspaces_dir), item)
  112. if os.path.exists(dst):
  113. os.remove(dst)
  114. shutil.copy(src, str(workspaces_dir))
  115. return _make_labserver_extension_app
  116. @pytest.fixture
  117. def labserverapp(
  118. jp_serverapp: ServerApp, make_labserver_extension_app: Callable[..., LabServerApp]
  119. ) -> LabServerApp:
  120. """A lab server app."""
  121. app = make_labserver_extension_app()
  122. app._link_jupyter_server_extension(jp_serverapp)
  123. app.initialize() # type:ignore[no-untyped-call]
  124. return app