##// END OF EJS Templates
avoid use of testing.tools in IPython.html
Min RK -
Show More
@@ -1,332 +1,342
1 # coding: utf-8
1 # coding: utf-8
2 """Test installation of notebook extensions"""
2 """Test installation of notebook extensions"""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 import glob
7 import glob
8 import os
8 import os
9 import re
9 import re
10 import sys
10 import tarfile
11 import tarfile
11 import zipfile
12 import zipfile
12 from io import BytesIO
13 from io import BytesIO, StringIO
13 from os.path import basename, join as pjoin
14 from os.path import basename, join as pjoin
14 from unittest import TestCase
15 from unittest import TestCase
15
16
16 import IPython.testing.tools as tt
17 try:
18 from unittest import mock
19 except ImportError:
20 import mock # py2
21
17 import IPython.testing.decorators as dec
22 import IPython.testing.decorators as dec
18 from IPython.utils import py3compat
23 from IPython.utils import py3compat
19 from IPython.utils.tempdir import TemporaryDirectory
24 from IPython.utils.tempdir import TemporaryDirectory
20 from IPython.html import nbextensions
25 from IPython.html import nbextensions
21 from IPython.html.nbextensions import install_nbextension, check_nbextension
26 from IPython.html.nbextensions import install_nbextension, check_nbextension
22
27
23
28
24 def touch(file, mtime=None):
29 def touch(file, mtime=None):
25 """ensure a file exists, and set its modification time
30 """ensure a file exists, and set its modification time
26
31
27 returns the modification time of the file
32 returns the modification time of the file
28 """
33 """
29 open(file, 'a').close()
34 open(file, 'a').close()
30 # set explicit mtime
35 # set explicit mtime
31 if mtime:
36 if mtime:
32 atime = os.stat(file).st_atime
37 atime = os.stat(file).st_atime
33 os.utime(file, (atime, mtime))
38 os.utime(file, (atime, mtime))
34 return os.stat(file).st_mtime
39 return os.stat(file).st_mtime
35
40
36 class TestInstallNBExtension(TestCase):
41 class TestInstallNBExtension(TestCase):
37
42
38 def tempdir(self):
43 def tempdir(self):
39 td = TemporaryDirectory()
44 td = TemporaryDirectory()
40 self.tempdirs.append(td)
45 self.tempdirs.append(td)
41 return py3compat.cast_unicode(td.name)
46 return py3compat.cast_unicode(td.name)
42
47
43 def setUp(self):
48 def setUp(self):
44 self.tempdirs = []
49 self.tempdirs = []
45 src = self.src = self.tempdir()
50 src = self.src = self.tempdir()
46 self.files = files = [
51 self.files = files = [
47 pjoin(u'ƒile'),
52 pjoin(u'ƒile'),
48 pjoin(u'∂ir', u'ƒile1'),
53 pjoin(u'∂ir', u'ƒile1'),
49 pjoin(u'∂ir', u'∂ir2', u'ƒile2'),
54 pjoin(u'∂ir', u'∂ir2', u'ƒile2'),
50 ]
55 ]
51 for file in files:
56 for file in files:
52 fullpath = os.path.join(self.src, file)
57 fullpath = os.path.join(self.src, file)
53 parent = os.path.dirname(fullpath)
58 parent = os.path.dirname(fullpath)
54 if not os.path.exists(parent):
59 if not os.path.exists(parent):
55 os.makedirs(parent)
60 os.makedirs(parent)
56 touch(fullpath)
61 touch(fullpath)
57
62
58 self.ipdir = self.tempdir()
63 self.ipdir = self.tempdir()
59 self.save_get_ipython_dir = nbextensions.get_ipython_dir
64 self.save_get_ipython_dir = nbextensions.get_ipython_dir
60 nbextensions.get_ipython_dir = lambda : self.ipdir
65 nbextensions.get_ipython_dir = lambda : self.ipdir
61 self.save_system_dir = nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR
66 self.save_system_dir = nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR
62 nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR = self.system_nbext = self.tempdir()
67 nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR = self.system_nbext = self.tempdir()
63
68
64 def tearDown(self):
69 def tearDown(self):
65 nbextensions.get_ipython_dir = self.save_get_ipython_dir
70 nbextensions.get_ipython_dir = self.save_get_ipython_dir
66 nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR = self.save_system_dir
71 nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR = self.save_system_dir
67 for td in self.tempdirs:
72 for td in self.tempdirs:
68 td.cleanup()
73 td.cleanup()
69
74
70 def assert_dir_exists(self, path):
75 def assert_dir_exists(self, path):
71 if not os.path.exists(path):
76 if not os.path.exists(path):
72 do_exist = os.listdir(os.path.dirname(path))
77 do_exist = os.listdir(os.path.dirname(path))
73 self.fail(u"%s should exist (found %s)" % (path, do_exist))
78 self.fail(u"%s should exist (found %s)" % (path, do_exist))
74
79
75 def assert_not_dir_exists(self, path):
80 def assert_not_dir_exists(self, path):
76 if os.path.exists(path):
81 if os.path.exists(path):
77 self.fail(u"%s should not exist" % path)
82 self.fail(u"%s should not exist" % path)
78
83
79 def assert_installed(self, relative_path, user=False):
84 def assert_installed(self, relative_path, user=False):
80 if user:
85 if user:
81 nbext = pjoin(self.ipdir, u'nbextensions')
86 nbext = pjoin(self.ipdir, u'nbextensions')
82 else:
87 else:
83 nbext = self.system_nbext
88 nbext = self.system_nbext
84 self.assert_dir_exists(
89 self.assert_dir_exists(
85 pjoin(nbext, relative_path)
90 pjoin(nbext, relative_path)
86 )
91 )
87
92
88 def assert_not_installed(self, relative_path, user=False):
93 def assert_not_installed(self, relative_path, user=False):
89 if user:
94 if user:
90 nbext = pjoin(self.ipdir, u'nbextensions')
95 nbext = pjoin(self.ipdir, u'nbextensions')
91 else:
96 else:
92 nbext = self.system_nbext
97 nbext = self.system_nbext
93 self.assert_not_dir_exists(
98 self.assert_not_dir_exists(
94 pjoin(nbext, relative_path)
99 pjoin(nbext, relative_path)
95 )
100 )
96
101
97 def test_create_ipython_dir(self):
102 def test_create_ipython_dir(self):
98 """install_nbextension when ipython_dir doesn't exist"""
103 """install_nbextension when ipython_dir doesn't exist"""
99 with TemporaryDirectory() as td:
104 with TemporaryDirectory() as td:
100 self.ipdir = ipdir = pjoin(td, u'ipython')
105 self.ipdir = ipdir = pjoin(td, u'ipython')
101 install_nbextension(self.src, user=True)
106 install_nbextension(self.src, user=True)
102 self.assert_dir_exists(ipdir)
107 self.assert_dir_exists(ipdir)
103 for file in self.files:
108 for file in self.files:
104 self.assert_installed(
109 self.assert_installed(
105 pjoin(basename(self.src), file),
110 pjoin(basename(self.src), file),
106 user=bool(ipdir)
111 user=bool(ipdir)
107 )
112 )
108
113
109 def test_create_nbextensions_user(self):
114 def test_create_nbextensions_user(self):
110 with TemporaryDirectory() as td:
115 with TemporaryDirectory() as td:
111 self.ipdir = ipdir = pjoin(td, u'ipython')
116 self.ipdir = ipdir = pjoin(td, u'ipython')
112 install_nbextension(self.src, user=True)
117 install_nbextension(self.src, user=True)
113 self.assert_installed(
118 self.assert_installed(
114 pjoin(basename(self.src), u'ƒile'),
119 pjoin(basename(self.src), u'ƒile'),
115 user=True
120 user=True
116 )
121 )
117
122
118 def test_create_nbextensions_system(self):
123 def test_create_nbextensions_system(self):
119 with TemporaryDirectory() as td:
124 with TemporaryDirectory() as td:
120 nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR = self.system_nbext = pjoin(td, u'nbextensions')
125 nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR = self.system_nbext = pjoin(td, u'nbextensions')
121 install_nbextension(self.src, user=False)
126 install_nbextension(self.src, user=False)
122 self.assert_installed(
127 self.assert_installed(
123 pjoin(basename(self.src), u'ƒile'),
128 pjoin(basename(self.src), u'ƒile'),
124 user=False
129 user=False
125 )
130 )
126
131
127 def test_single_file(self):
132 def test_single_file(self):
128 file = self.files[0]
133 file = self.files[0]
129 install_nbextension(pjoin(self.src, file))
134 install_nbextension(pjoin(self.src, file))
130 self.assert_installed(file)
135 self.assert_installed(file)
131
136
132 def test_single_dir(self):
137 def test_single_dir(self):
133 d = u'∂ir'
138 d = u'∂ir'
134 install_nbextension(pjoin(self.src, d))
139 install_nbextension(pjoin(self.src, d))
135 self.assert_installed(self.files[-1])
140 self.assert_installed(self.files[-1])
136
141
137
142
138 def test_destination_file(self):
143 def test_destination_file(self):
139 file = self.files[0]
144 file = self.files[0]
140 install_nbextension(pjoin(self.src, file), destination = u'ƒiledest')
145 install_nbextension(pjoin(self.src, file), destination = u'ƒiledest')
141 self.assert_installed(u'ƒiledest')
146 self.assert_installed(u'ƒiledest')
142
147
143 def test_destination_dir(self):
148 def test_destination_dir(self):
144 d = u'∂ir'
149 d = u'∂ir'
145 install_nbextension(pjoin(self.src, d), destination = u'ƒiledest2')
150 install_nbextension(pjoin(self.src, d), destination = u'ƒiledest2')
146 self.assert_installed(pjoin(u'ƒiledest2', u'∂ir2', u'ƒile2'))
151 self.assert_installed(pjoin(u'ƒiledest2', u'∂ir2', u'ƒile2'))
147
152
148 def test_install_nbextension(self):
153 def test_install_nbextension(self):
149 with self.assertRaises(TypeError):
154 with self.assertRaises(TypeError):
150 install_nbextension(glob.glob(pjoin(self.src, '*')))
155 install_nbextension(glob.glob(pjoin(self.src, '*')))
151
156
152 def test_overwrite_file(self):
157 def test_overwrite_file(self):
153 with TemporaryDirectory() as d:
158 with TemporaryDirectory() as d:
154 fname = u'ƒ.js'
159 fname = u'ƒ.js'
155 src = pjoin(d, fname)
160 src = pjoin(d, fname)
156 with open(src, 'w') as f:
161 with open(src, 'w') as f:
157 f.write('first')
162 f.write('first')
158 mtime = touch(src)
163 mtime = touch(src)
159 dest = pjoin(self.system_nbext, fname)
164 dest = pjoin(self.system_nbext, fname)
160 install_nbextension(src)
165 install_nbextension(src)
161 with open(src, 'w') as f:
166 with open(src, 'w') as f:
162 f.write('overwrite')
167 f.write('overwrite')
163 mtime = touch(src, mtime - 100)
168 mtime = touch(src, mtime - 100)
164 install_nbextension(src, overwrite=True)
169 install_nbextension(src, overwrite=True)
165 with open(dest) as f:
170 with open(dest) as f:
166 self.assertEqual(f.read(), 'overwrite')
171 self.assertEqual(f.read(), 'overwrite')
167
172
168 def test_overwrite_dir(self):
173 def test_overwrite_dir(self):
169 with TemporaryDirectory() as src:
174 with TemporaryDirectory() as src:
170 base = basename(src)
175 base = basename(src)
171 fname = u'ƒ.js'
176 fname = u'ƒ.js'
172 touch(pjoin(src, fname))
177 touch(pjoin(src, fname))
173 install_nbextension(src)
178 install_nbextension(src)
174 self.assert_installed(pjoin(base, fname))
179 self.assert_installed(pjoin(base, fname))
175 os.remove(pjoin(src, fname))
180 os.remove(pjoin(src, fname))
176 fname2 = u'∂.js'
181 fname2 = u'∂.js'
177 touch(pjoin(src, fname2))
182 touch(pjoin(src, fname2))
178 install_nbextension(src, overwrite=True)
183 install_nbextension(src, overwrite=True)
179 self.assert_installed(pjoin(base, fname2))
184 self.assert_installed(pjoin(base, fname2))
180 self.assert_not_installed(pjoin(base, fname))
185 self.assert_not_installed(pjoin(base, fname))
181
186
182 def test_update_file(self):
187 def test_update_file(self):
183 with TemporaryDirectory() as d:
188 with TemporaryDirectory() as d:
184 fname = u'ƒ.js'
189 fname = u'ƒ.js'
185 src = pjoin(d, fname)
190 src = pjoin(d, fname)
186 with open(src, 'w') as f:
191 with open(src, 'w') as f:
187 f.write('first')
192 f.write('first')
188 mtime = touch(src)
193 mtime = touch(src)
189 install_nbextension(src)
194 install_nbextension(src)
190 self.assert_installed(fname)
195 self.assert_installed(fname)
191 dest = pjoin(self.system_nbext, fname)
196 dest = pjoin(self.system_nbext, fname)
192 old_mtime = os.stat(dest).st_mtime
197 old_mtime = os.stat(dest).st_mtime
193 with open(src, 'w') as f:
198 with open(src, 'w') as f:
194 f.write('overwrite')
199 f.write('overwrite')
195 touch(src, mtime + 10)
200 touch(src, mtime + 10)
196 install_nbextension(src)
201 install_nbextension(src)
197 with open(dest) as f:
202 with open(dest) as f:
198 self.assertEqual(f.read(), 'overwrite')
203 self.assertEqual(f.read(), 'overwrite')
199
204
200 def test_skip_old_file(self):
205 def test_skip_old_file(self):
201 with TemporaryDirectory() as d:
206 with TemporaryDirectory() as d:
202 fname = u'ƒ.js'
207 fname = u'ƒ.js'
203 src = pjoin(d, fname)
208 src = pjoin(d, fname)
204 mtime = touch(src)
209 mtime = touch(src)
205 install_nbextension(src)
210 install_nbextension(src)
206 self.assert_installed(fname)
211 self.assert_installed(fname)
207 dest = pjoin(self.system_nbext, fname)
212 dest = pjoin(self.system_nbext, fname)
208 old_mtime = os.stat(dest).st_mtime
213 old_mtime = os.stat(dest).st_mtime
209
214
210 mtime = touch(src, mtime - 100)
215 mtime = touch(src, mtime - 100)
211 install_nbextension(src)
216 install_nbextension(src)
212 new_mtime = os.stat(dest).st_mtime
217 new_mtime = os.stat(dest).st_mtime
213 self.assertEqual(new_mtime, old_mtime)
218 self.assertEqual(new_mtime, old_mtime)
214
219
215 def test_quiet(self):
220 def test_quiet(self):
216 with tt.AssertNotPrints(re.compile(r'.+')):
221 stdout = StringIO()
222 stderr = StringIO()
223 with mock.patch.object(sys, 'stdout', stdout), \
224 mock.patch.object(sys, 'stderr', stderr):
217 install_nbextension(self.src, verbose=0)
225 install_nbextension(self.src, verbose=0)
226 self.assertEqual(stdout.getvalue(), '')
227 self.assertEqual(stderr.getvalue(), '')
218
228
219 def test_install_zip(self):
229 def test_install_zip(self):
220 path = pjoin(self.src, "myjsext.zip")
230 path = pjoin(self.src, "myjsext.zip")
221 with zipfile.ZipFile(path, 'w') as f:
231 with zipfile.ZipFile(path, 'w') as f:
222 f.writestr("a.js", b"b();")
232 f.writestr("a.js", b"b();")
223 f.writestr("foo/a.js", b"foo();")
233 f.writestr("foo/a.js", b"foo();")
224 install_nbextension(path)
234 install_nbextension(path)
225 self.assert_installed("a.js")
235 self.assert_installed("a.js")
226 self.assert_installed(pjoin("foo", "a.js"))
236 self.assert_installed(pjoin("foo", "a.js"))
227
237
228 def test_install_tar(self):
238 def test_install_tar(self):
229 def _add_file(f, fname, buf):
239 def _add_file(f, fname, buf):
230 info = tarfile.TarInfo(fname)
240 info = tarfile.TarInfo(fname)
231 info.size = len(buf)
241 info.size = len(buf)
232 f.addfile(info, BytesIO(buf))
242 f.addfile(info, BytesIO(buf))
233
243
234 for i,ext in enumerate((".tar.gz", ".tgz", ".tar.bz2")):
244 for i,ext in enumerate((".tar.gz", ".tgz", ".tar.bz2")):
235 path = pjoin(self.src, "myjsext" + ext)
245 path = pjoin(self.src, "myjsext" + ext)
236 with tarfile.open(path, 'w') as f:
246 with tarfile.open(path, 'w') as f:
237 _add_file(f, "b%i.js" % i, b"b();")
247 _add_file(f, "b%i.js" % i, b"b();")
238 _add_file(f, "foo/b%i.js" % i, b"foo();")
248 _add_file(f, "foo/b%i.js" % i, b"foo();")
239 install_nbextension(path)
249 install_nbextension(path)
240 self.assert_installed("b%i.js" % i)
250 self.assert_installed("b%i.js" % i)
241 self.assert_installed(pjoin("foo", "b%i.js" % i))
251 self.assert_installed(pjoin("foo", "b%i.js" % i))
242
252
243 def test_install_url(self):
253 def test_install_url(self):
244 def fake_urlretrieve(url, dest):
254 def fake_urlretrieve(url, dest):
245 touch(dest)
255 touch(dest)
246 save_urlretrieve = nbextensions.urlretrieve
256 save_urlretrieve = nbextensions.urlretrieve
247 nbextensions.urlretrieve = fake_urlretrieve
257 nbextensions.urlretrieve = fake_urlretrieve
248 try:
258 try:
249 install_nbextension("http://example.com/path/to/foo.js")
259 install_nbextension("http://example.com/path/to/foo.js")
250 self.assert_installed("foo.js")
260 self.assert_installed("foo.js")
251 install_nbextension("https://example.com/path/to/another/bar.js")
261 install_nbextension("https://example.com/path/to/another/bar.js")
252 self.assert_installed("bar.js")
262 self.assert_installed("bar.js")
253 install_nbextension("https://example.com/path/to/another/bar.js",
263 install_nbextension("https://example.com/path/to/another/bar.js",
254 destination = 'foobar.js')
264 destination = 'foobar.js')
255 self.assert_installed("foobar.js")
265 self.assert_installed("foobar.js")
256 finally:
266 finally:
257 nbextensions.urlretrieve = save_urlretrieve
267 nbextensions.urlretrieve = save_urlretrieve
258
268
259 def test_check_nbextension(self):
269 def test_check_nbextension(self):
260 with TemporaryDirectory() as d:
270 with TemporaryDirectory() as d:
261 f = u'ƒ.js'
271 f = u'ƒ.js'
262 src = pjoin(d, f)
272 src = pjoin(d, f)
263 touch(src)
273 touch(src)
264 install_nbextension(src, user=True)
274 install_nbextension(src, user=True)
265
275
266 assert check_nbextension(f, user=True)
276 assert check_nbextension(f, user=True)
267 assert check_nbextension([f], user=True)
277 assert check_nbextension([f], user=True)
268 assert not check_nbextension([f, pjoin('dne', f)], user=True)
278 assert not check_nbextension([f, pjoin('dne', f)], user=True)
269
279
270 @dec.skip_win32
280 @dec.skip_win32
271 def test_install_symlink(self):
281 def test_install_symlink(self):
272 with TemporaryDirectory() as d:
282 with TemporaryDirectory() as d:
273 f = u'ƒ.js'
283 f = u'ƒ.js'
274 src = pjoin(d, f)
284 src = pjoin(d, f)
275 touch(src)
285 touch(src)
276 install_nbextension(src, symlink=True)
286 install_nbextension(src, symlink=True)
277 dest = pjoin(self.system_nbext, f)
287 dest = pjoin(self.system_nbext, f)
278 assert os.path.islink(dest)
288 assert os.path.islink(dest)
279 link = os.readlink(dest)
289 link = os.readlink(dest)
280 self.assertEqual(link, src)
290 self.assertEqual(link, src)
281
291
282 @dec.skip_win32
292 @dec.skip_win32
283 def test_overwrite_broken_symlink(self):
293 def test_overwrite_broken_symlink(self):
284 with TemporaryDirectory() as d:
294 with TemporaryDirectory() as d:
285 f = u'ƒ.js'
295 f = u'ƒ.js'
286 f2 = u'ƒ2.js'
296 f2 = u'ƒ2.js'
287 src = pjoin(d, f)
297 src = pjoin(d, f)
288 src2 = pjoin(d, f2)
298 src2 = pjoin(d, f2)
289 touch(src)
299 touch(src)
290 install_nbextension(src, symlink=True)
300 install_nbextension(src, symlink=True)
291 os.rename(src, src2)
301 os.rename(src, src2)
292 install_nbextension(src2, symlink=True, overwrite=True, destination=f)
302 install_nbextension(src2, symlink=True, overwrite=True, destination=f)
293 dest = pjoin(self.system_nbext, f)
303 dest = pjoin(self.system_nbext, f)
294 assert os.path.islink(dest)
304 assert os.path.islink(dest)
295 link = os.readlink(dest)
305 link = os.readlink(dest)
296 self.assertEqual(link, src2)
306 self.assertEqual(link, src2)
297
307
298 @dec.skip_win32
308 @dec.skip_win32
299 def test_install_symlink_destination(self):
309 def test_install_symlink_destination(self):
300 with TemporaryDirectory() as d:
310 with TemporaryDirectory() as d:
301 f = u'ƒ.js'
311 f = u'ƒ.js'
302 flink = u'ƒlink.js'
312 flink = u'ƒlink.js'
303 src = pjoin(d, f)
313 src = pjoin(d, f)
304 touch(src)
314 touch(src)
305 install_nbextension(src, symlink=True, destination=flink)
315 install_nbextension(src, symlink=True, destination=flink)
306 dest = pjoin(self.system_nbext, flink)
316 dest = pjoin(self.system_nbext, flink)
307 assert os.path.islink(dest)
317 assert os.path.islink(dest)
308 link = os.readlink(dest)
318 link = os.readlink(dest)
309 self.assertEqual(link, src)
319 self.assertEqual(link, src)
310
320
311 def test_install_symlink_bad(self):
321 def test_install_symlink_bad(self):
312 with self.assertRaises(ValueError):
322 with self.assertRaises(ValueError):
313 install_nbextension("http://example.com/foo.js", symlink=True)
323 install_nbextension("http://example.com/foo.js", symlink=True)
314
324
315 with TemporaryDirectory() as d:
325 with TemporaryDirectory() as d:
316 zf = u'ƒ.zip'
326 zf = u'ƒ.zip'
317 zsrc = pjoin(d, zf)
327 zsrc = pjoin(d, zf)
318 with zipfile.ZipFile(zsrc, 'w') as z:
328 with zipfile.ZipFile(zsrc, 'w') as z:
319 z.writestr("a.js", b"b();")
329 z.writestr("a.js", b"b();")
320
330
321 with self.assertRaises(ValueError):
331 with self.assertRaises(ValueError):
322 install_nbextension(zsrc, symlink=True)
332 install_nbextension(zsrc, symlink=True)
323
333
324 def test_install_destination_bad(self):
334 def test_install_destination_bad(self):
325 with TemporaryDirectory() as d:
335 with TemporaryDirectory() as d:
326 zf = u'ƒ.zip'
336 zf = u'ƒ.zip'
327 zsrc = pjoin(d, zf)
337 zsrc = pjoin(d, zf)
328 with zipfile.ZipFile(zsrc, 'w') as z:
338 with zipfile.ZipFile(zsrc, 'w') as z:
329 z.writestr("a.js", b"b();")
339 z.writestr("a.js", b"b();")
330
340
331 with self.assertRaises(ValueError):
341 with self.assertRaises(ValueError):
332 install_nbextension(zsrc, destination='foo')
342 install_nbextension(zsrc, destination='foo')
General Comments 0
You need to be logged in to leave comments. Login now