Skip to content

Commit 0d67d5d

Browse files
committed
mv ws_fetch kernelspecs and contents_manager fixtures to pytest_plugin for 3rd party usage
1 parent e41336d commit 0d67d5d

File tree

4 files changed

+54
-53
lines changed

4 files changed

+54
-53
lines changed

jupyter_server/pytest_plugin.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from jupyter_server.extension import serverextension
1616
from jupyter_server.serverapp import ServerApp
1717
from jupyter_server.utils import url_path_join
18+
from jupyter_server.services.contents.filemanager import FileContentsManager
1819

1920
import nbformat
2021

@@ -62,6 +63,11 @@ def mkdir(tmp_path, *parts):
6263
env_config_path = pytest.fixture(
6364
lambda tmp_path: mkdir(tmp_path, "env", "etc", "jupyter")
6465
)
66+
some_resource = u"The very model of a modern major general"
67+
sample_kernel_json = {
68+
'argv':['cat', '{connection_file}'],
69+
'display_name':'Test kernel',
70+
}
6571
argv = pytest.fixture(lambda: [])
6672

6773

@@ -199,6 +205,52 @@ def client_fetch(*parts, headers={}, params={}, **kwargs):
199205
return client_fetch
200206

201207

208+
@pytest.fixture
209+
def ws_fetch(auth_header, http_port):
210+
"""websocket fetch fixture that handles auth, base_url, and path"""
211+
def client_fetch(*parts, headers={}, params={}, **kwargs):
212+
# Handle URL strings
213+
path = url_escape(url_path_join(*parts), plus=False)
214+
urlparts = urllib.parse.urlparse('ws://localhost:{}'.format(http_port))
215+
urlparts = urlparts._replace(
216+
path=path,
217+
query=urllib.parse.urlencode(params)
218+
)
219+
url = urlparts.geturl()
220+
# Add auth keys to header
221+
headers.update(auth_header)
222+
# Make request.
223+
req = tornado.httpclient.HTTPRequest(
224+
url,
225+
headers=auth_header,
226+
connect_timeout=120
227+
)
228+
return tornado.websocket.websocket_connect(req)
229+
return client_fetch
230+
231+
232+
@pytest.fixture
233+
def kernelspecs(data_dir):
234+
spec_names = ['sample', 'sample 2']
235+
for name in spec_names:
236+
sample_kernel_dir = data_dir.joinpath('kernels', name)
237+
sample_kernel_dir.mkdir(parents=True)
238+
# Create kernel json file
239+
sample_kernel_file = sample_kernel_dir.joinpath('kernel.json')
240+
sample_kernel_file.write_text(json.dumps(sample_kernel_json))
241+
# Create resources text
242+
sample_kernel_resources = sample_kernel_dir.joinpath('resource.txt')
243+
sample_kernel_resources.write_text(some_resource)
244+
245+
246+
# contents_manager_atomic = pytest.fixture(lambda tmp_path: FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=True))
247+
# contents_manager_nonatomic = pytest.fixture(lambda tmp_path: FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=False))
248+
249+
@pytest.fixture(params=[True, False])
250+
def contents_manager(request, tmp_path):
251+
return FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=request.param)
252+
253+
202254
@pytest.fixture
203255
def create_notebook(root_dir):
204256
"""Create a notebook in the test's home directory."""

tests/services/contents/test_manager.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515

1616
# -------------- Functions ----------------------------
1717

18-
# contents_manager_atomic = pytest.fixture(lambda tmp_path: FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=True))
19-
# contents_manager_nonatomic = pytest.fixture(lambda tmp_path: FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=False))
20-
21-
@pytest.fixture(params=[True, False])
22-
def contents_manager(request, tmp_path):
23-
return FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=request.param)
24-
25-
2618
def _make_dir(contents_manager, api_path):
2719
"""
2820
Make a directory.

tests/services/kernels/test_api.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,6 @@
1313
from ...utils import expected_http_error
1414

1515

16-
@pytest.fixture
17-
def ws_fetch(auth_header, http_port):
18-
"""fetch fixture that handles auth, base_url, and path"""
19-
def client_fetch(*parts, headers={}, params={}, **kwargs):
20-
# Handle URL strings
21-
path = url_escape(url_path_join(*parts), plus=False)
22-
urlparts = urllib.parse.urlparse('ws://localhost:{}'.format(http_port))
23-
urlparts = urlparts._replace(
24-
path=path,
25-
query=urllib.parse.urlencode(params)
26-
)
27-
url = urlparts.geturl()
28-
# Add auth keys to header
29-
headers.update(auth_header)
30-
# Make request.
31-
req = tornado.httpclient.HTTPRequest(
32-
url,
33-
headers=auth_header,
34-
connect_timeout=120
35-
)
36-
return tornado.websocket.websocket_connect(req)
37-
return client_fetch
38-
39-
4016
async def test_no_kernels(fetch):
4117
r = await fetch(
4218
'api', 'kernels',

tests/services/kernelspecs/test_api.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,13 @@
33

44
import tornado
55

6+
from jupyter_server.pytest_plugin import some_resource
7+
68
from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
79

810
from ...utils import expected_http_error
911

1012

11-
sample_kernel_json = {
12-
'argv':['cat', '{connection_file}'],
13-
'display_name':'Test kernel',
14-
}
15-
some_resource = u"The very model of a modern major general"
16-
17-
18-
@pytest.fixture
19-
def kernelspecs(data_dir):
20-
spec_names = ['sample', 'sample 2']
21-
for name in spec_names:
22-
sample_kernel_dir = data_dir.joinpath('kernels', name)
23-
sample_kernel_dir.mkdir(parents=True)
24-
# Create kernel json file
25-
sample_kernel_file = sample_kernel_dir.joinpath('kernel.json')
26-
sample_kernel_file.write_text(json.dumps(sample_kernel_json))
27-
# Create resources text
28-
sample_kernel_resources = sample_kernel_dir.joinpath('resource.txt')
29-
sample_kernel_resources.write_text(some_resource)
30-
31-
3213
async def test_list_kernelspecs_bad(fetch, kernelspecs, data_dir):
3314
bad_kernel_dir = data_dir.joinpath(data_dir, 'kernels', 'bad')
3415
bad_kernel_dir.mkdir(parents=True)

0 commit comments

Comments
 (0)