Skip to content

Commit ff237e4

Browse files
committed
Add doc strings, perform minor reorganization
1 parent 1a91510 commit ff237e4

File tree

3 files changed

+99
-39
lines changed

3 files changed

+99
-39
lines changed

pytest_jupyter/jupyter_core.py

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,61 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
import jupyter_core.paths
45
import os
56
import pytest
67
import shutil
78
import sys
89

9-
import jupyter_core.paths
10+
from .utils import mkdir
11+
12+
13+
@pytest.fixture
14+
def jp_home_dir(tmp_path):
15+
"""Provides a temporary HOME directory value."""
16+
return mkdir(tmp_path, "home")
17+
18+
19+
@pytest.fixture
20+
def jp_data_dir(tmp_path):
21+
"""Provides a temporary Jupyter data dir directory value."""
22+
return mkdir(tmp_path, "data")
23+
24+
25+
@pytest.fixture
26+
def jp_config_dir(tmp_path):
27+
"""Provides a temporary Jupyter config dir directory value."""
28+
return mkdir(tmp_path, "config")
1029

1130

12-
def mkdir(tmp_path, *parts):
13-
path = tmp_path.joinpath(*parts)
14-
if not path.exists():
15-
path.mkdir(parents=True)
16-
return path
31+
@pytest.fixture
32+
def jp_runtime_dir(tmp_path):
33+
"""Provides a temporary Jupyter runtime dir directory value."""
34+
return mkdir(tmp_path, "runtime")
35+
36+
37+
@pytest.fixture
38+
def jp_system_jupyter_path(tmp_path):
39+
"""Provides a temporary Jupyter system path value."""
40+
return mkdir(tmp_path, "share", "jupyter")
41+
1742

43+
@pytest.fixture
44+
def jp_env_jupyter_path(tmp_path):
45+
"""Provides a temporary Jupyter env system path value."""
46+
return mkdir(tmp_path, "env", "share", "jupyter")
1847

19-
jp_home_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "home"))
20-
jp_data_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "data"))
21-
jp_config_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "config"))
22-
jp_runtime_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "runtime"))
23-
jp_root_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "root_dir"))
24-
jp_template_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "templates"))
25-
jp_system_jupyter_path = pytest.fixture(
26-
lambda tmp_path: mkdir(tmp_path, "share", "jupyter")
27-
)
28-
jp_env_jupyter_path = pytest.fixture(
29-
lambda tmp_path: mkdir(tmp_path, "env", "share", "jupyter")
30-
)
31-
jp_system_config_path = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "etc", "jupyter"))
32-
jp_env_config_path = pytest.fixture(
33-
lambda tmp_path: mkdir(tmp_path, "env", "etc", "jupyter")
34-
)
48+
49+
@pytest.fixture
50+
def jp_system_config_path(tmp_path):
51+
"""Provides a temporary Jupyter config path value."""
52+
return mkdir(tmp_path, "etc", "jupyter")
53+
54+
55+
@pytest.fixture
56+
def jp_env_config_path(tmp_path):
57+
"""Provides a temporary Jupyter env config path value."""
58+
return mkdir(tmp_path, "env", "etc", "jupyter")
3559

3660

3761
@pytest.fixture
@@ -42,12 +66,12 @@ def jp_environ(
4266
jp_data_dir,
4367
jp_config_dir,
4468
jp_runtime_dir,
45-
jp_root_dir,
4669
jp_system_jupyter_path,
4770
jp_system_config_path,
4871
jp_env_jupyter_path,
4972
jp_env_config_path,
5073
):
74+
"""Configures a temporary environment based on Jupyter-specific environment variables. """
5175
monkeypatch.setenv("HOME", str(jp_home_dir))
5276
monkeypatch.setenv("PYTHONPATH", os.pathsep.join(sys.path))
5377

pytest_jupyter/jupyter_server.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
import nbformat
45
import os
56
import json
67
import pytest
@@ -18,37 +19,51 @@
1819
from jupyter_server.utils import url_path_join
1920
from jupyter_server.services.contents.filemanager import FileContentsManager
2021

21-
import nbformat
2222

23-
# This shouldn't be needed anymore, since pytest_tornasync is found in entrypoints
24-
pytest_plugins = "pytest_tornasync"
23+
from .utils import mkdir
2524

2625
# NOTE: This is a temporary fix for Windows 3.8
2726
# We have to override the io_loop fixture with an
2827
# asyncio patch. This will probably be removed in
2928
# the future.
30-
3129
@pytest.fixture
3230
def jp_asyncio_patch():
31+
"""Appropriately configures the event loop policy if running on Windows w/ Python >= 3.8."""
3332
ServerApp()._init_asyncio_patch()
3433

34+
3535
@pytest.fixture
3636
def io_loop(jp_asyncio_patch):
37+
"""Returns an ioloop instance that includes the asyncio patch for Windows 3.8 platforms."""
3738
loop = tornado.ioloop.IOLoop()
3839
loop.make_current()
3940
yield loop
4041
loop.clear_current()
4142
loop.close(all_fds=True)
4243

43-
jp_server_config = pytest.fixture(lambda: {})
4444

45-
some_resource = u"The very model of a modern major general"
46-
sample_kernel_json = {
47-
'argv':['cat', '{connection_file}'],
48-
'display_name': 'Test kernel',
49-
}
50-
jp_argv = pytest.fixture(lambda: [])
45+
@pytest.fixture
46+
def jp_server_config():
47+
"""Allows tests to setup their specific configuration values. """
48+
return {}
49+
5150

51+
@pytest.fixture
52+
def jp_root_dir(tmp_path):
53+
"""Provides a temporary Jupyter root directory value."""
54+
return mkdir(tmp_path, "root_dir")
55+
56+
57+
@pytest.fixture
58+
def jp_template_dir(tmp_path):
59+
"""Provides a temporary Jupyter templates directory value."""
60+
return mkdir(tmp_path, "templates")
61+
62+
63+
@pytest.fixture
64+
def jp_argv():
65+
"""Allows tests to setup specific argv values. """
66+
return []
5267

5368

5469
@pytest.fixture
@@ -59,6 +74,7 @@ def jp_extension_environ(jp_env_config_path, monkeypatch):
5974

6075
@pytest.fixture
6176
def jp_http_port(http_server_port):
77+
"""Returns the port value from the http_server_port fixture. """
6278
return http_server_port[-1]
6379

6480

@@ -72,6 +88,7 @@ def jp_configurable_serverapp(
7288
jp_root_dir,
7389
io_loop,
7490
):
91+
"""Starts a Jupyter Server instance based on the provided configuration values."""
7592
ServerApp.clear_instance()
7693

7794
def _configurable_serverapp(
@@ -117,6 +134,7 @@ def _configurable_serverapp(
117134

118135
@pytest.fixture(scope="function")
119136
def jp_serverapp(jp_server_config, jp_argv, jp_configurable_serverapp):
137+
"""Starts a Jupyter Server instance based on the established configuration values."""
120138
app = jp_configurable_serverapp(config=jp_server_config, argv=jp_argv)
121139
yield app
122140
app.remove_server_info_file()
@@ -132,17 +150,19 @@ def app(jp_serverapp):
132150

133151
@pytest.fixture
134152
def jp_auth_header(jp_serverapp):
153+
"""Configures an authorization header using the token from the serverapp fixture."""
135154
return {"Authorization": "token {token}".format(token=jp_serverapp.token)}
136155

137156

138157
@pytest.fixture
139158
def jp_base_url():
159+
"""Returns the base url to use for the test."""
140160
return "/"
141161

142162

143163
@pytest.fixture
144164
def jp_fetch(http_server_client, jp_auth_header, jp_base_url):
145-
"""fetch fixture that handles auth, base_url, and path"""
165+
"""Performs an HTTP request against the test server."""
146166
def client_fetch(*parts, headers={}, params={}, **kwargs):
147167
# Handle URL strings
148168
path_url = url_escape(url_path_join(jp_base_url, *parts), plus=False)
@@ -159,7 +179,7 @@ def client_fetch(*parts, headers={}, params={}, **kwargs):
159179

160180
@pytest.fixture
161181
def jp_ws_fetch(jp_auth_header, jp_http_port):
162-
"""websocket fetch fixture that handles auth, base_url, and path"""
182+
"""Performs a websocket request against the test server."""
163183
def client_fetch(*parts, headers={}, params={}, **kwargs):
164184
# Handle URL strings
165185
path = url_escape(url_path_join(*parts), plus=False)
@@ -181,8 +201,14 @@ def client_fetch(*parts, headers={}, params={}, **kwargs):
181201
return client_fetch
182202

183203

204+
some_resource = u"The very model of a modern major general"
205+
sample_kernel_json = {
206+
'argv':['cat', '{connection_file}'],
207+
'display_name': 'Test kernel',
208+
}
184209
@pytest.fixture
185210
def jp_kernelspecs(jp_data_dir):
211+
"""Configures some sample kernelspecs in the Jupyter data directory."""
186212
spec_names = ['sample', 'sample 2']
187213
for name in spec_names:
188214
sample_kernel_dir = jp_data_dir.joinpath('kernels', name)
@@ -196,13 +222,14 @@ def jp_kernelspecs(jp_data_dir):
196222

197223

198224
@pytest.fixture(params=[True, False])
199-
def jp_contents_manager(request, tmp_path):
200-
return FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=request.param)
225+
def jp_contents_manager(request, jp_root_dir):
226+
"""Returns a FileContentsManager instance based on the use_atomic_writing parameter value."""
227+
return FileContentsManager(root_dir=jp_root_dir, use_atomic_writing=request.param)
201228

202229

203230
@pytest.fixture
204231
def jp_create_notebook(jp_root_dir):
205-
"""Create a notebook in the test's home directory."""
232+
"""Creates a notebook in the test's home directory."""
206233
def inner(nbpath):
207234
nbpath = jp_root_dir.joinpath(nbpath)
208235
# Check that the notebook has the correct file extension.

pytest_jupyter/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
4+
5+
def mkdir(tmp_path, *parts):
6+
path = tmp_path.joinpath(*parts)
7+
if not path.exists():
8+
path.mkdir(parents=True)
9+
return path

0 commit comments

Comments
 (0)