pytest_plugin.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Pytest Fixtures exported by Jupyter Server."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import json
  5. from pathlib import Path
  6. import pytest
  7. from jupyter_server.services.contents.filemanager import AsyncFileContentsManager
  8. from jupyter_server.services.contents.largefilemanager import AsyncLargeFileManager
  9. pytest_plugins = ["pytest_jupyter.jupyter_server"]
  10. some_resource = "The very model of a modern major general"
  11. sample_kernel_json = {
  12. "argv": ["cat", "{connection_file}"],
  13. "display_name": "Test kernel",
  14. }
  15. @pytest.fixture # type:ignore[misc]
  16. def jp_kernelspecs(jp_data_dir: Path) -> None:
  17. """Configures some sample kernelspecs in the Jupyter data directory."""
  18. spec_names = ["sample", "sample2", "bad"]
  19. for name in spec_names:
  20. sample_kernel_dir = jp_data_dir.joinpath("kernels", name)
  21. sample_kernel_dir.mkdir(parents=True)
  22. # Create kernel json file
  23. sample_kernel_file = sample_kernel_dir.joinpath("kernel.json")
  24. kernel_json = sample_kernel_json.copy()
  25. if name == "bad":
  26. kernel_json["argv"] = ["non_existent_path"]
  27. sample_kernel_file.write_text(json.dumps(kernel_json))
  28. # Create resources text
  29. sample_kernel_resources = sample_kernel_dir.joinpath("resource.txt")
  30. sample_kernel_resources.write_text(some_resource)
  31. @pytest.fixture(params=[True, False])
  32. def jp_contents_manager(request, tmp_path):
  33. """Returns an AsyncFileContentsManager instance based on the use_atomic_writing parameter value."""
  34. return AsyncFileContentsManager(root_dir=str(tmp_path), use_atomic_writing=request.param)
  35. @pytest.fixture
  36. def jp_large_contents_manager(tmp_path):
  37. """Returns an AsyncLargeFileManager instance."""
  38. return AsyncLargeFileManager(root_dir=str(tmp_path))