forked from jupyterlab/jupyterlab
/
test_jupyterlab.py
353 lines (295 loc) · 12.9 KB
/
test_jupyterlab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# coding: utf-8
"""Test installation of JupyterLab extensions"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import glob
import json
import os
import sys
from os.path import join as pjoin
from unittest import TestCase
import pytest
try:
from unittest.mock import patch
except ImportError:
from mock import patch # py2
from ipython_genutils import py3compat
from ipython_genutils.tempdir import TemporaryDirectory
from notebook.notebookapp import NotebookApp
from jupyter_core import paths
from jupyterlab import commands
from jupyterlab.extension import (
load_jupyter_server_extension
)
from jupyterlab.commands import (
install_extension, uninstall_extension, list_extensions,
build, link_package, unlink_package, should_build,
disable_extension, enable_extension, _get_extensions,
_get_linked_packages, _ensure_package, _get_disabled,
_test_overlap
)
here = os.path.dirname(os.path.abspath(__file__))
def touch(file, mtime=None):
"""ensure a file exists, and set its modification time
returns the modification time of the file
"""
dirname = os.path.dirname(file)
if not os.path.exists(dirname):
os.makedirs(dirname)
open(file, 'a').close()
# set explicit mtime
if mtime:
atime = os.stat(file).st_atime
os.utime(file, (atime, mtime))
return os.stat(file).st_mtime
class TestExtension(TestCase):
def tempdir(self):
td = TemporaryDirectory()
self.tempdirs.append(td)
return py3compat.cast_unicode(td.name)
def setUp(self):
# Any TemporaryDirectory objects appended to this list will be cleaned
# up at the end of the test run.
self.tempdirs = []
self._mock_extensions = []
self.devnull = open(os.devnull, 'w')
@self.addCleanup
def cleanup_tempdirs():
for d in self.tempdirs:
d.cleanup()
self.test_dir = self.tempdir()
self.data_dir = pjoin(self.test_dir, 'data')
self.config_dir = pjoin(self.test_dir, 'config')
self.source_dir = pjoin(here, 'mockextension')
self.incompat_dir = pjoin(here, 'mockextension-incompat')
self.mock_package = pjoin(here, 'mockpackage')
self.mime_renderer_dir = pjoin(here, 'mock-mimeextension')
self.patches = []
p = patch.dict('os.environ', {
'JUPYTER_CONFIG_DIR': self.config_dir,
'JUPYTER_DATA_DIR': self.data_dir,
})
self.patches.append(p)
for mod in (paths, commands):
if hasattr(mod, 'ENV_JUPYTER_PATH'):
p = patch.object(mod, 'ENV_JUPYTER_PATH', [self.data_dir])
self.patches.append(p)
if hasattr(mod, 'ENV_CONFIG_PATH'):
p = patch.object(mod, 'ENV_CONFIG_PATH', [self.config_dir])
self.patches.append(p)
if hasattr(mod, 'CONFIG_PATH'):
p = patch.object(mod, 'CONFIG_PATH', self.config_dir)
self.patches.append(p)
if hasattr(mod, 'BUILD_PATH'):
p = patch.object(mod, 'BUILD_PATH', self.data_dir)
self.patches.append(p)
for p in self.patches:
p.start()
self.addCleanup(p.stop)
# verify our patches
self.assertEqual(paths.ENV_CONFIG_PATH, [self.config_dir])
self.assertEqual(paths.ENV_JUPYTER_PATH, [self.data_dir])
self.assertEqual(commands.ENV_JUPYTER_PATH, [self.data_dir])
self.assertEqual(commands.get_app_dir(), os.path.realpath(pjoin(self.data_dir, 'lab')))
self.app_dir = commands.get_app_dir()
def tearDown(self):
for modulename in self._mock_extensions:
sys.modules.pop(modulename)
def test_install_extension(self):
install_extension(self.source_dir)
path = pjoin(self.app_dir, 'extensions', '*python-tests*.tgz')
assert glob.glob(path)
assert '@jupyterlab/python-tests' in _get_extensions(self.app_dir)
def test_install_twice(self):
install_extension(self.source_dir)
path = pjoin(commands.get_app_dir(), 'extensions', '*python-tests*.tgz')
install_extension(self.source_dir)
assert glob.glob(path)
assert '@jupyterlab/python-tests' in _get_extensions(self.app_dir)
def test_install_mime_renderer(self):
install_extension(self.mime_renderer_dir)
assert '@jupyterlab/mime-extension-test' in _get_extensions(self.app_dir)
uninstall_extension('@jupyterlab/mime-extension-test')
assert '@jupyterlab/mime-extension-test' not in _get_extensions(self.app_dir)
def test_install_incompatible(self):
with pytest.raises(ValueError) as excinfo:
install_extension(self.incompat_dir)
assert 'Conflicting Dependencies' in str(excinfo.value)
def test_install_failed(self):
path = self.mock_package
with pytest.raises(ValueError):
install_extension(path)
with open(pjoin(path, 'package.json')) as fid:
data = json.load(fid)
assert not data['name'] in _get_extensions(self.app_dir)
def test_uninstall_extension(self):
install_extension(self.source_dir)
uninstall_extension('@jupyterlab/python-tests')
path = pjoin(self.app_dir, 'extensions', '*python_tests*.tgz')
assert not glob.glob(path)
assert '@jupyterlab/python-tests' not in _get_extensions(self.app_dir)
def test_uninstall_core_extension(self):
uninstall_extension('@jupyterlab/console-extension')
app_dir = self.app_dir
_ensure_package(app_dir)
with open(pjoin(app_dir, 'staging', 'package.json')) as fid:
data = json.load(fid)
extensions = data['jupyterlab']['extensions']
assert '@jupyterlab/console-extension' not in extensions
install_extension('@jupyterlab/console-extension')
_ensure_package(app_dir)
with open(pjoin(app_dir, 'staging', 'package.json')) as fid:
data = json.load(fid)
extensions = data['jupyterlab']['extensions']
assert '@jupyterlab/console-extension' in extensions
def test_link_extension(self):
link_package(self.source_dir)
linked = _get_linked_packages().keys()
assert '@jupyterlab/python-tests' in linked
assert '@jupyterlab/python-tests' in _get_extensions(self.app_dir)
def test_link_mime_renderer(self):
link_package(self.mime_renderer_dir)
linked = _get_linked_packages().keys()
assert '@jupyterlab/mime-extension-test' in linked
assert '@jupyterlab/mime-extension-test' in _get_extensions(self.app_dir)
unlink_package('@jupyterlab/mime-extension-test')
linked = _get_linked_packages().keys()
assert '@jupyterlab/mime-extension-test' not in linked
assert '@jupyterlab/mime-extension-test' not in _get_extensions(self.app_dir)
def test_link_package(self):
path = self.mock_package
link_package(path)
linked = _get_linked_packages().keys()
with open(pjoin(path, 'package.json')) as fid:
data = json.load(fid)
assert data['name'] in linked
assert not data['name'] in _get_extensions(self.app_dir)
unlink_package(path)
linked = _get_linked_packages().keys()
assert not data['name'] in linked
def test_link_incompatible(self):
with pytest.raises(ValueError) as excinfo:
install_extension(self.incompat_dir)
assert 'Conflicting Dependencies' in str(excinfo.value)
def test_unlink_package(self):
target = self.source_dir
link_package(target)
unlink_package(target)
linked = _get_linked_packages().keys()
assert '@jupyterlab/python-tests' not in linked
assert '@jupyterlab/python-tests' not in _get_extensions(self.app_dir)
def test_list_extensions(self):
install_extension(self.source_dir)
list_extensions()
def test_app_dir(self):
app_dir = self.tempdir()
install_extension(self.source_dir, app_dir)
path = pjoin(app_dir, 'extensions', '*python-tests*.tgz')
assert glob.glob(path)
assert '@jupyterlab/python-tests' in _get_extensions(app_dir)
uninstall_extension('@jupyterlab/python-tests', app_dir)
path = pjoin(app_dir, 'extensions', '*python-tests*.tgz')
assert not glob.glob(path)
assert '@jupyterlab/python-tests' not in _get_extensions(app_dir)
link_package(self.source_dir, app_dir)
linked = _get_linked_packages(app_dir).keys()
assert '@jupyterlab/python-tests' in linked
unlink_package(self.source_dir, app_dir)
linked = _get_linked_packages(app_dir).keys()
assert '@jupyterlab/python-tests' not in linked
def test_app_dir_use_sys_prefix(self):
app_dir = self.tempdir()
if os.path.exists(self.app_dir):
os.removedirs(self.app_dir)
install_extension(self.source_dir)
path = pjoin(app_dir, 'extensions', '*python-tests*.tgz')
assert not glob.glob(path)
assert '@jupyterlab/python-tests' in _get_extensions(app_dir)
def test_app_dir_shadowing(self):
app_dir = self.tempdir()
sys_dir = self.app_dir
if os.path.exists(sys_dir):
os.removedirs(sys_dir)
install_extension(self.source_dir)
sys_path = pjoin(sys_dir, 'extensions', '*python-tests*.tgz')
assert glob.glob(sys_path)
app_path = pjoin(app_dir, 'extensions', '*python-tests*.tgz')
assert not glob.glob(app_path)
assert '@jupyterlab/python-tests' in _get_extensions(app_dir)
install_extension(self.source_dir, app_dir)
assert glob.glob(app_path)
assert '@jupyterlab/python-tests' in _get_extensions(app_dir)
uninstall_extension('@jupyterlab/python-tests', app_dir)
assert not glob.glob(app_path)
assert glob.glob(sys_path)
assert '@jupyterlab/python-tests' in _get_extensions(app_dir)
uninstall_extension('@jupyterlab/python-tests', app_dir)
assert not glob.glob(app_path)
assert not glob.glob(sys_path)
assert '@jupyterlab/python-tests' not in _get_extensions(app_dir)
def test_build(self):
install_extension(self.source_dir)
build()
# check staging directory.
entry = pjoin(self.app_dir, 'staging', 'build', 'index.out.js')
with open(entry) as fid:
data = fid.read()
assert '@jupyterlab/python-tests' in data
# check static directory.
entry = pjoin(self.app_dir, 'static', 'index.out.js')
with open(entry) as fid:
data = fid.read()
assert '@jupyterlab/python-tests' in data
def test_build_custom(self):
install_extension(self.source_dir)
build(name='foo', version='1.0')
# check static directory.
entry = pjoin(self.app_dir, 'static', 'index.out.js')
with open(entry) as fid:
data = fid.read()
assert '@jupyterlab/python-tests' in data
pkg = pjoin(self.app_dir, 'static', 'package.json')
with open(pkg) as fid:
data = json.load(fid)
assert data['jupyterlab']['name'] == 'foo'
assert data['jupyterlab']['version'] == '1.0'
def test_load_extension(self):
app = NotebookApp()
stderr = sys.stderr
sys.stderr = self.devnull
app.initialize()
sys.stderr = stderr
load_jupyter_server_extension(app)
def test_disable_extension(self):
app_dir = self.tempdir()
install_extension(self.source_dir, app_dir)
disable_extension('@jupyterlab/python-tests', app_dir)
disabled = _get_disabled(app_dir)
assert '@jupyterlab/python-tests' in disabled
disable_extension('@jupyterlab/notebook-extension', app_dir)
disabled = _get_disabled(app_dir)
assert '@jupyterlab/notebook-extension' in disabled
def test_enable_extension(self):
app_dir = self.tempdir()
install_extension(self.source_dir, app_dir)
disable_extension('@jupyterlab/python-tests', app_dir)
enable_extension('@jupyterlab/python-tests', app_dir)
disabled = _get_disabled(app_dir)
assert '@jupyterlab/python-tests' not in disabled
def test_should_build(self):
assert not should_build()[0]
install_extension(self.source_dir)
assert should_build()[0]
build()
assert not should_build()[0]
uninstall_extension('@jupyterlab/python-tests')
assert should_build()[0]
def test_compatibility(self):
assert _test_overlap('^0.6.0', '^0.6.1')
assert _test_overlap('>0.1', '0.6')
assert _test_overlap('~0.5.0', '~0.5.2')
assert _test_overlap('0.5.2', '^0.5.0')
assert not _test_overlap('^0.5.0', '^0.6.0')
assert not _test_overlap('~1.5.0', '^1.6.0')
assert _test_overlap('*', '0.6') is None
assert _test_overlap('<0.6', '0.1') is None