4
4
import asyncio
5
5
import importlib
6
6
import io
7
- import json
8
7
import logging
9
8
import os
10
9
import re
22
21
import nbformat
23
22
import tornado
24
23
import tornado .testing
24
+ from jupyter_server ._version import version_info
25
25
from jupyter_server .auth import Authorizer
26
26
from jupyter_server .extension import serverextension
27
27
from jupyter_server .serverapp import JUPYTER_SERVICE_HANDLERS , ServerApp
28
- from jupyter_server .services .contents .filemanager import AsyncFileContentsManager
29
- from jupyter_server .services .contents .largefilemanager import AsyncLargeFileManager
30
28
from jupyter_server .utils import url_path_join
31
29
from pytest_tornasync .plugin import AsyncHTTPServerClient
32
30
from tornado .escape import url_escape
33
31
from tornado .httpclient import HTTPClientError
34
32
from tornado .websocket import WebSocketHandler
35
33
from traitlets .config import Config
36
34
35
+ is_v2 = version_info [0 ] == 2
36
+
37
37
except ImportError :
38
38
import warnings
39
39
51
51
pytest_plugins = ["pytest_tornasync" , "pytest_jupyter" ]
52
52
53
53
54
- # NOTE: This is a temporary fix for Windows 3.8
55
- # We have to override the io_loop fixture with an
56
- # asyncio patch. This will probably be removed in
57
- # the future.
58
54
@pytest .fixture
59
- def jp_asyncio_patch ():
60
- """Appropriately configures the event loop policy if running on Windows w/ Python >= 3.8."""
61
- ServerApp ()._init_asyncio_patch ()
62
-
63
-
64
- @pytest .fixture
65
- def asyncio_loop ():
66
- loop = asyncio .new_event_loop ()
67
- asyncio .set_event_loop (loop )
68
- yield loop
69
- loop .close ()
55
+ async def asyncio_loop ():
56
+ return asyncio .get_running_loop ()
70
57
71
58
72
59
@pytest .fixture (autouse = True )
@@ -113,11 +100,14 @@ async def get_server():
113
100
@pytest .fixture
114
101
def jp_server_config ():
115
102
"""Allows tests to setup their specific configuration values."""
116
- return Config (
117
- {
118
- "jpserver_extensions" : {"jupyter_server_terminals" : True },
119
- }
120
- )
103
+ if is_v2 :
104
+ return Config (
105
+ {
106
+ "jpserver_extensions" : {"jupyter_server_terminals" : True },
107
+ }
108
+ )
109
+ else :
110
+ return Config ({})
121
111
122
112
123
113
@pytest .fixture
@@ -199,6 +189,7 @@ def jp_configurable_serverapp(
199
189
jp_root_dir ,
200
190
jp_logging_stream ,
201
191
asyncio_loop ,
192
+ io_loop ,
202
193
):
203
194
"""Starts a Jupyter Server instance based on
204
195
the provided configuration values.
@@ -218,7 +209,7 @@ def my_test(jp_configurable_serverapp):
218
209
# explicitly put in config.
219
210
serverapp_config = jp_server_config .setdefault ("ServerApp" , {})
220
211
exts = serverapp_config .setdefault ("jpserver_extensions" , {})
221
- if "jupyter_server_terminals" not in exts :
212
+ if "jupyter_server_terminals" not in exts and is_v2 :
222
213
exts ["jupyter_server_terminals" ] = True
223
214
224
215
def _configurable_serverapp (
@@ -228,14 +219,19 @@ def _configurable_serverapp(
228
219
environ = jp_environ ,
229
220
http_port = jp_http_port ,
230
221
tmp_path = tmp_path ,
222
+ io_loop = io_loop ,
231
223
root_dir = jp_root_dir ,
232
224
** kwargs ,
233
225
):
234
226
c = Config (config )
235
227
c .NotebookNotary .db_file = ":memory:"
236
- if "token" not in c .ServerApp and not c .IdentityProvider .token :
237
- token = hexlify (os .urandom (4 )).decode ("ascii" )
238
- c .IdentityProvider .token = token
228
+
229
+ default_token = hexlify (os .urandom (4 )).decode ("ascii" )
230
+ if not is_v2 :
231
+ kwargs ["token" ] = default_token
232
+
233
+ elif "token" not in c .ServerApp and not c .IdentityProvider .token :
234
+ c .IdentityProvider .token = default_token
239
235
240
236
# Allow tests to configure root_dir via a file, argv, or its
241
237
# default (cwd) by specifying a value of None.
@@ -294,6 +290,8 @@ def jp_web_app(jp_serverapp):
294
290
@pytest .fixture
295
291
def jp_auth_header (jp_serverapp ):
296
292
"""Configures an authorization header using the token from the serverapp fixture."""
293
+ if not is_v2 :
294
+ return {"Authorization" : f"token { jp_serverapp .token } " }
297
295
return {"Authorization" : f"token { jp_serverapp .identity_provider .token } " }
298
296
299
297
@@ -331,6 +329,7 @@ def client_fetch(*parts, headers=None, params=None, **kwargs):
331
329
for key , value in jp_auth_header .items ():
332
330
headers .setdefault (key , value )
333
331
# Make request.
332
+ print (id (http_server_client .io_loop .asyncio_loop ))
334
333
return http_server_client .fetch (url , headers = headers , request_timeout = 20 , ** kwargs )
335
334
336
335
return client_fetch
@@ -382,43 +381,6 @@ def client_fetch(*parts, headers=None, params=None, **kwargs):
382
381
return client_fetch
383
382
384
383
385
- some_resource = "The very model of a modern major general"
386
- sample_kernel_json = {
387
- "argv" : ["cat" , "{connection_file}" ],
388
- "display_name" : "Test kernel" ,
389
- }
390
-
391
-
392
- @pytest .fixture
393
- def jp_kernelspecs (jp_data_dir ):
394
- """Configures some sample kernelspecs in the Jupyter data directory."""
395
- spec_names = ["sample" , "sample2" , "bad" ]
396
- for name in spec_names :
397
- sample_kernel_dir = jp_data_dir .joinpath ("kernels" , name )
398
- sample_kernel_dir .mkdir (parents = True )
399
- # Create kernel json file
400
- sample_kernel_file = sample_kernel_dir .joinpath ("kernel.json" )
401
- kernel_json = sample_kernel_json .copy ()
402
- if name == "bad" :
403
- kernel_json ["argv" ] = ["non_existent_path" ]
404
- sample_kernel_file .write_text (json .dumps (kernel_json ))
405
- # Create resources text
406
- sample_kernel_resources = sample_kernel_dir .joinpath ("resource.txt" )
407
- sample_kernel_resources .write_text (some_resource )
408
-
409
-
410
- @pytest .fixture (params = [True , False ])
411
- def jp_contents_manager (request , tmp_path ):
412
- """Returns an AsyncFileContentsManager instance based on the use_atomic_writing parameter value."""
413
- return AsyncFileContentsManager (root_dir = str (tmp_path ), use_atomic_writing = request .param )
414
-
415
-
416
- @pytest .fixture
417
- def jp_large_contents_manager (tmp_path ):
418
- """Returns an AsyncLargeFileManager instance."""
419
- return AsyncLargeFileManager (root_dir = str (tmp_path ))
420
-
421
-
422
384
@pytest .fixture
423
385
def jp_create_notebook (jp_root_dir ):
424
386
"""Creates a notebook in the test's home directory."""
@@ -435,6 +397,7 @@ def inner(nbpath):
435
397
nb = nbformat .v4 .new_notebook ()
436
398
nbtext = nbformat .writes (nb , version = 4 )
437
399
nbpath .write_text (nbtext )
400
+ return nb
438
401
439
402
return inner
440
403
0 commit comments