/
test_nbextensions.py
514 lines (428 loc) · 18.4 KB
/
test_nbextensions.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
"""Test installation of notebook extensions"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import glob
import os
import pytest
import sys
import tarfile
import zipfile
from io import BytesIO, StringIO
from os.path import basename, join as pjoin
from traitlets.tests.utils import check_help_all_output
from unittest import TestCase
from unittest.mock import patch
from ipython_genutils import py3compat
from ipython_genutils.tempdir import TemporaryDirectory
from notebook import nbextensions
from notebook.nbextensions import (install_nbextension, check_nbextension,
enable_nbextension, disable_nbextension,
install_nbextension_python, uninstall_nbextension_python,
enable_nbextension_python, disable_nbextension_python, _get_config_dir,
validate_nbextension, validate_nbextension_python
)
from notebook.config_manager import BaseJSONConfigManager
def touch(file_name, mtime=None):
"""ensure a file exists, and set its modification time
returns the modification time of the file
"""
open(file_name, 'a').close()
# set explicit mtime
if mtime:
atime = os.stat(file_name).st_atime
os.utime(file_name, (atime, mtime))
return os.stat(file_name).st_mtime
def test_help_output():
check_help_all_output('notebook.nbextensions')
check_help_all_output('notebook.nbextensions', ['enable'])
check_help_all_output('notebook.nbextensions', ['disable'])
check_help_all_output('notebook.nbextensions', ['install'])
check_help_all_output('notebook.nbextensions', ['uninstall'])
class TestInstallNBExtension(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.addCleanup
def cleanup_tempdirs():
for d in self.tempdirs:
d.cleanup()
self.src = self.tempdir()
self.files = files = [
pjoin(u'ƒile'),
pjoin(u'∂ir', u'ƒile1'),
pjoin(u'∂ir', u'∂ir2', u'ƒile2'),
]
for file_name in files:
fullpath = os.path.join(self.src, file_name)
parent = os.path.dirname(fullpath)
if not os.path.exists(parent):
os.makedirs(parent)
touch(fullpath)
self.test_dir = self.tempdir()
self.data_dir = os.path.join(self.test_dir, 'data')
self.config_dir = os.path.join(self.test_dir, 'config')
self.system_data_dir = os.path.join(self.test_dir, 'system_data')
self.system_path = [self.system_data_dir]
self.system_nbext = os.path.join(self.system_data_dir, 'nbextensions')
# Patch out os.environ so that tests are isolated from the real OS
# environment.
self.patch_env = patch.dict('os.environ', {
'JUPYTER_CONFIG_DIR': self.config_dir,
'JUPYTER_DATA_DIR': self.data_dir,
})
self.patch_env.start()
self.addCleanup(self.patch_env.stop)
# Patch out the system path os that we consistently use our own
# temporary directory instead.
self.patch_system_path = patch.object(
nbextensions, 'SYSTEM_JUPYTER_PATH', self.system_path
)
self.patch_system_path.start()
self.addCleanup(self.patch_system_path.stop)
def assert_dir_exists(self, path):
if not os.path.exists(path):
do_exist = os.listdir(os.path.dirname(path))
self.fail(u"%s should exist (found %s)" % (path, do_exist))
def assert_not_dir_exists(self, path):
if os.path.exists(path):
self.fail(u"%s should not exist" % path)
def assert_installed(self, relative_path, user=False):
if user:
nbext = pjoin(self.data_dir, u'nbextensions')
else:
nbext = self.system_nbext
self.assert_dir_exists(
pjoin(nbext, relative_path)
)
def assert_not_installed(self, relative_path, user=False):
if user:
nbext = pjoin(self.data_dir, u'nbextensions')
else:
nbext = self.system_nbext
self.assert_not_dir_exists(
pjoin(nbext, relative_path)
)
def test_create_data_dir(self):
"""install_nbextension when data_dir doesn't exist"""
with TemporaryDirectory() as td:
data_dir = os.path.join(td, self.data_dir)
with patch.dict('os.environ', {
'JUPYTER_DATA_DIR': data_dir,
}):
install_nbextension(self.src, user=True)
self.assert_dir_exists(data_dir)
for file_name in self.files:
self.assert_installed(
pjoin(basename(self.src), file_name),
user=True,
)
def test_create_nbextensions_user(self):
with TemporaryDirectory() as td:
install_nbextension(self.src, user=True)
self.assert_installed(
pjoin(basename(self.src), u'ƒile'),
user=True
)
def test_create_nbextensions_system(self):
with TemporaryDirectory() as td:
self.system_nbext = pjoin(td, u'nbextensions')
with patch.object(nbextensions, 'SYSTEM_JUPYTER_PATH', [td]):
install_nbextension(self.src, user=False)
self.assert_installed(
pjoin(basename(self.src), u'ƒile'),
user=False
)
def test_single_file(self):
file_name = self.files[0]
install_nbextension(pjoin(self.src, file_name))
self.assert_installed(file_name)
def test_single_dir(self):
d = u'∂ir'
install_nbextension(pjoin(self.src, d))
self.assert_installed(self.files[-1])
def test_single_dir_trailing_slash(self):
d = u'∂ir/'
install_nbextension(pjoin(self.src, d))
self.assert_installed(self.files[-1])
if os.name == 'nt':
d = u'∂ir\\'
install_nbextension(pjoin(self.src, d))
self.assert_installed(self.files[-1])
def test_destination_file(self):
file_name = self.files[0]
install_nbextension(pjoin(self.src, file_name), destination = u'ƒiledest')
self.assert_installed(u'ƒiledest')
def test_destination_dir(self):
d = u'∂ir'
install_nbextension(pjoin(self.src, d), destination = u'ƒiledest2')
self.assert_installed(pjoin(u'ƒiledest2', u'∂ir2', u'ƒile2'))
def test_install_nbextension(self):
with self.assertRaises(TypeError):
install_nbextension(glob.glob(pjoin(self.src, '*')))
def test_overwrite_file(self):
with TemporaryDirectory() as d:
fname = u'ƒ.js'
src = pjoin(d, fname)
with open(src, 'w') as f:
f.write('first')
mtime = touch(src)
dest = pjoin(self.system_nbext, fname)
install_nbextension(src)
with open(src, 'w') as f:
f.write('overwrite')
mtime = touch(src, mtime - 100)
install_nbextension(src, overwrite=True)
with open(dest) as f:
self.assertEqual(f.read(), 'overwrite')
def test_overwrite_dir(self):
with TemporaryDirectory() as src:
base = basename(src)
fname = u'ƒ.js'
touch(pjoin(src, fname))
install_nbextension(src)
self.assert_installed(pjoin(base, fname))
os.remove(pjoin(src, fname))
fname2 = u'∂.js'
touch(pjoin(src, fname2))
install_nbextension(src, overwrite=True)
self.assert_installed(pjoin(base, fname2))
self.assert_not_installed(pjoin(base, fname))
def test_update_file(self):
with TemporaryDirectory() as d:
fname = u'ƒ.js'
src = pjoin(d, fname)
with open(src, 'w') as f:
f.write('first')
mtime = touch(src)
install_nbextension(src)
self.assert_installed(fname)
dest = pjoin(self.system_nbext, fname)
os.stat(dest).st_mtime
with open(src, 'w') as f:
f.write('overwrite')
touch(src, mtime + 10)
install_nbextension(src)
with open(dest) as f:
self.assertEqual(f.read(), 'overwrite')
def test_skip_old_file(self):
with TemporaryDirectory() as d:
fname = u'ƒ.js'
src = pjoin(d, fname)
mtime = touch(src)
install_nbextension(src)
self.assert_installed(fname)
dest = pjoin(self.system_nbext, fname)
old_mtime = os.stat(dest).st_mtime
mtime = touch(src, mtime - 100)
install_nbextension(src)
new_mtime = os.stat(dest).st_mtime
self.assertEqual(new_mtime, old_mtime)
def test_quiet(self):
stdout = StringIO()
stderr = StringIO()
with patch.object(sys, 'stdout', stdout), \
patch.object(sys, 'stderr', stderr):
install_nbextension(self.src)
self.assertEqual(stdout.getvalue(), '')
self.assertEqual(stderr.getvalue(), '')
def test_install_zip(self):
path = pjoin(self.src, "myjsext.zip")
with zipfile.ZipFile(path, 'w') as f:
f.writestr("a.js", b"b();")
f.writestr("foo/a.js", b"foo();")
install_nbextension(path)
self.assert_installed("a.js")
self.assert_installed(pjoin("foo", "a.js"))
def test_install_tar(self):
def _add_file(f, fname, buf):
info = tarfile.TarInfo(fname)
info.size = len(buf)
f.addfile(info, BytesIO(buf))
for i,ext in enumerate((".tar.gz", ".tgz", ".tar.bz2")):
path = pjoin(self.src, "myjsext" + ext)
with tarfile.open(path, 'w') as f:
_add_file(f, "b%i.js" % i, b"b();")
_add_file(f, "foo/b%i.js" % i, b"foo();")
install_nbextension(path)
self.assert_installed("b%i.js" % i)
self.assert_installed(pjoin("foo", "b%i.js" % i))
def test_install_url(self):
def fake_urlretrieve(url, dest):
touch(dest)
save_urlretrieve = nbextensions.urlretrieve
nbextensions.urlretrieve = fake_urlretrieve
try:
install_nbextension("http://example.com/path/to/foo.js")
self.assert_installed("foo.js")
install_nbextension("https://example.com/path/to/another/bar.js")
self.assert_installed("bar.js")
install_nbextension("https://example.com/path/to/another/bar.js",
destination = 'foobar.js')
self.assert_installed("foobar.js")
finally:
nbextensions.urlretrieve = save_urlretrieve
def test_check_nbextension(self):
with TemporaryDirectory() as d:
f = u'ƒ.js'
src = pjoin(d, f)
touch(src)
install_nbextension(src, user=True)
assert check_nbextension(f, user=True)
assert check_nbextension([f], user=True)
assert not check_nbextension([f, pjoin('dne', f)], user=True)
@pytest.mark.skipif(sys.platform == "win32", reason="do not run on windows")
def test_install_symlink(self):
with TemporaryDirectory() as d:
f = u'ƒ.js'
src = pjoin(d, f)
touch(src)
install_nbextension(src, symlink=True)
dest = pjoin(self.system_nbext, f)
assert os.path.islink(dest)
link = os.readlink(dest)
self.assertEqual(link, src)
@pytest.mark.skipif(sys.platform == "win32", reason="do not run on windows")
def test_overwrite_broken_symlink(self):
with TemporaryDirectory() as d:
f = u'ƒ.js'
f2 = u'ƒ2.js'
src = pjoin(d, f)
src2 = pjoin(d, f2)
touch(src)
install_nbextension(src, symlink=True)
os.rename(src, src2)
install_nbextension(src2, symlink=True, overwrite=True, destination=f)
dest = pjoin(self.system_nbext, f)
assert os.path.islink(dest)
link = os.readlink(dest)
self.assertEqual(link, src2)
@pytest.mark.skipif(sys.platform == "win32", reason="do not run on windows")
def test_install_symlink_destination(self):
with TemporaryDirectory() as d:
f = u'ƒ.js'
flink = u'ƒlink.js'
src = pjoin(d, f)
touch(src)
install_nbextension(src, symlink=True, destination=flink)
dest = pjoin(self.system_nbext, flink)
assert os.path.islink(dest)
link = os.readlink(dest)
self.assertEqual(link, src)
@pytest.mark.skipif(sys.platform == "win32", reason="do not run on windows")
def test_install_symlink_bad(self):
with self.assertRaises(ValueError):
install_nbextension("http://example.com/foo.js", symlink=True)
with TemporaryDirectory() as d:
zf = u'ƒ.zip'
zsrc = pjoin(d, zf)
with zipfile.ZipFile(zsrc, 'w') as z:
z.writestr("a.js", b"b();")
with self.assertRaises(ValueError):
install_nbextension(zsrc, symlink=True)
def test_install_destination_bad(self):
with TemporaryDirectory() as d:
zf = u'ƒ.zip'
zsrc = pjoin(d, zf)
with zipfile.ZipFile(zsrc, 'w') as z:
z.writestr("a.js", b"b();")
with self.assertRaises(ValueError):
install_nbextension(zsrc, destination='foo')
def test_nbextension_enable(self):
with TemporaryDirectory() as d:
f = u'ƒ.js'
src = pjoin(d, f)
touch(src)
install_nbextension(src, user=True)
enable_nbextension(section='notebook', require=u'ƒ')
config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
enabled = cm.get('notebook').get('load_extensions', {}).get(u'ƒ', False)
assert enabled
def test_nbextension_disable(self):
self.test_nbextension_enable()
disable_nbextension(section='notebook', require=u'ƒ')
config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
enabled = cm.get('notebook').get('load_extensions', {}).get(u'ƒ', False)
assert not enabled
def _mock_extension_spec_meta(self, section='notebook'):
return {
'section': section,
'src': 'mockextension',
'dest': '_mockdestination',
'require': '_mockdestination/index'
}
def _inject_mock_extension(self, section='notebook'):
outer_file = __file__
meta = self._mock_extension_spec_meta(section)
class mock():
__file__ = outer_file
@staticmethod
def _jupyter_nbextension_paths():
return [meta]
import sys
sys.modules['mockextension'] = mock
def test_nbextensionpy_files(self):
self._inject_mock_extension()
install_nbextension_python('mockextension')
assert check_nbextension('_mockdestination/index.js')
assert check_nbextension(['_mockdestination/index.js'])
def test_nbextensionpy_user_files(self):
self._inject_mock_extension()
install_nbextension_python('mockextension', user=True)
assert check_nbextension('_mockdestination/index.js', user=True)
assert check_nbextension(['_mockdestination/index.js'], user=True)
def test_nbextensionpy_uninstall_files(self):
self._inject_mock_extension()
install_nbextension_python('mockextension', user=True)
uninstall_nbextension_python('mockextension', user=True)
assert not check_nbextension('_mockdestination/index.js')
assert not check_nbextension(['_mockdestination/index.js'])
def test_nbextensionpy_enable(self):
self._inject_mock_extension('notebook')
install_nbextension_python('mockextension', user=True)
enable_nbextension_python('mockextension')
config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
enabled = cm.get('notebook').get('load_extensions', {}).get('_mockdestination/index', False)
assert enabled
def test_nbextensionpy_disable(self):
self._inject_mock_extension('notebook')
install_nbextension_python('mockextension', user=True)
enable_nbextension_python('mockextension')
disable_nbextension_python('mockextension', user=True)
config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
cm = BaseJSONConfigManager(config_dir=config_dir)
enabled = cm.get('notebook').get('load_extensions', {}).get('_mockdestination/index', False)
assert not enabled
def test_nbextensionpy_validate(self):
self._inject_mock_extension('notebook')
paths = install_nbextension_python('mockextension', user=True)
enable_nbextension_python('mockextension')
meta = self._mock_extension_spec_meta()
warnings = validate_nbextension_python(meta, paths[0])
self.assertEqual([], warnings, warnings)
def test_nbextensionpy_validate_bad(self):
# Break the metadata (correct file will still be copied)
self._inject_mock_extension('notebook')
paths = install_nbextension_python('mockextension', user=True)
enable_nbextension_python('mockextension')
meta = self._mock_extension_spec_meta()
meta.update(require="bad-require")
warnings = validate_nbextension_python(meta, paths[0])
self.assertNotEqual([], warnings, warnings)
def test_nbextension_validate(self):
# Break the metadata (correct file will still be copied)
self._inject_mock_extension('notebook')
install_nbextension_python('mockextension', user=True)
enable_nbextension_python('mockextension')
warnings = validate_nbextension("_mockdestination/index")
self.assertEqual([], warnings, warnings)
def test_nbextension_validate_bad(self):
warnings = validate_nbextension("this-doesn't-exist")
self.assertNotEqual([], warnings, warnings)