1
1
# Copyright (c) Jupyter Development Team.
2
2
# Distributed under the terms of the Modified BSD License.
3
3
4
+ import nbformat
4
5
import os
5
6
import json
6
7
import pytest
18
19
from jupyter_server .utils import url_path_join
19
20
from jupyter_server .services .contents .filemanager import FileContentsManager
20
21
21
- import nbformat
22
22
23
- # This shouldn't be needed anymore, since pytest_tornasync is found in entrypoints
24
- pytest_plugins = "pytest_tornasync"
23
+ from .utils import mkdir
25
24
26
25
# NOTE: This is a temporary fix for Windows 3.8
27
26
# We have to override the io_loop fixture with an
28
27
# asyncio patch. This will probably be removed in
29
28
# the future.
30
-
31
29
@pytest .fixture
32
30
def jp_asyncio_patch ():
31
+ """Appropriately configures the event loop policy if running on Windows w/ Python >= 3.8."""
33
32
ServerApp ()._init_asyncio_patch ()
34
33
34
+
35
35
@pytest .fixture
36
36
def io_loop (jp_asyncio_patch ):
37
+ """Returns an ioloop instance that includes the asyncio patch for Windows 3.8 platforms."""
37
38
loop = tornado .ioloop .IOLoop ()
38
39
loop .make_current ()
39
40
yield loop
40
41
loop .clear_current ()
41
42
loop .close (all_fds = True )
42
43
43
- jp_server_config = pytest .fixture (lambda : {})
44
44
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
+
51
50
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 []
52
67
53
68
54
69
@pytest .fixture
@@ -59,6 +74,7 @@ def jp_extension_environ(jp_env_config_path, monkeypatch):
59
74
60
75
@pytest .fixture
61
76
def jp_http_port (http_server_port ):
77
+ """Returns the port value from the http_server_port fixture. """
62
78
return http_server_port [- 1 ]
63
79
64
80
@@ -72,6 +88,7 @@ def jp_configurable_serverapp(
72
88
jp_root_dir ,
73
89
io_loop ,
74
90
):
91
+ """Starts a Jupyter Server instance based on the provided configuration values."""
75
92
ServerApp .clear_instance ()
76
93
77
94
def _configurable_serverapp (
@@ -117,6 +134,7 @@ def _configurable_serverapp(
117
134
118
135
@pytest .fixture (scope = "function" )
119
136
def jp_serverapp (jp_server_config , jp_argv , jp_configurable_serverapp ):
137
+ """Starts a Jupyter Server instance based on the established configuration values."""
120
138
app = jp_configurable_serverapp (config = jp_server_config , argv = jp_argv )
121
139
yield app
122
140
app .remove_server_info_file ()
@@ -132,17 +150,19 @@ def app(jp_serverapp):
132
150
133
151
@pytest .fixture
134
152
def jp_auth_header (jp_serverapp ):
153
+ """Configures an authorization header using the token from the serverapp fixture."""
135
154
return {"Authorization" : "token {token}" .format (token = jp_serverapp .token )}
136
155
137
156
138
157
@pytest .fixture
139
158
def jp_base_url ():
159
+ """Returns the base url to use for the test."""
140
160
return "/"
141
161
142
162
143
163
@pytest .fixture
144
164
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. """
146
166
def client_fetch (* parts , headers = {}, params = {}, ** kwargs ):
147
167
# Handle URL strings
148
168
path_url = url_escape (url_path_join (jp_base_url , * parts ), plus = False )
@@ -159,7 +179,7 @@ def client_fetch(*parts, headers={}, params={}, **kwargs):
159
179
160
180
@pytest .fixture
161
181
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. """
163
183
def client_fetch (* parts , headers = {}, params = {}, ** kwargs ):
164
184
# Handle URL strings
165
185
path = url_escape (url_path_join (* parts ), plus = False )
@@ -181,8 +201,14 @@ def client_fetch(*parts, headers={}, params={}, **kwargs):
181
201
return client_fetch
182
202
183
203
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
+ }
184
209
@pytest .fixture
185
210
def jp_kernelspecs (jp_data_dir ):
211
+ """Configures some sample kernelspecs in the Jupyter data directory."""
186
212
spec_names = ['sample' , 'sample 2' ]
187
213
for name in spec_names :
188
214
sample_kernel_dir = jp_data_dir .joinpath ('kernels' , name )
@@ -196,13 +222,14 @@ def jp_kernelspecs(jp_data_dir):
196
222
197
223
198
224
@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 )
201
228
202
229
203
230
@pytest .fixture
204
231
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."""
206
233
def inner (nbpath ):
207
234
nbpath = jp_root_dir .joinpath (nbpath )
208
235
# Check that the notebook has the correct file extension.
0 commit comments