Show More
@@ -1,234 +1,246 b'' | |||||
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) 2014 The IPython Development Team |
|
4 | # Copyright (C) 2014 The IPython Development Team | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
7 | # the file COPYING, distributed as part of this software. |
|
7 | # the file COPYING, distributed as part of this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | import glob |
|
14 | import glob | |
15 | import os |
|
15 | import os | |
16 | import re |
|
16 | import re | |
17 | import tarfile |
|
17 | import tarfile | |
18 | import zipfile |
|
18 | import zipfile | |
19 | from io import BytesIO |
|
19 | from io import BytesIO | |
20 | from os.path import basename, join as pjoin |
|
20 | from os.path import basename, join as pjoin | |
21 | from unittest import TestCase |
|
21 | from unittest import TestCase | |
22 |
|
22 | |||
23 | import IPython.testing.tools as tt |
|
23 | import IPython.testing.tools as tt | |
24 | from IPython.utils import py3compat |
|
24 | from IPython.utils import py3compat | |
25 | from IPython.utils.tempdir import TemporaryDirectory |
|
25 | from IPython.utils.tempdir import TemporaryDirectory | |
26 | from IPython.html import nbextensions |
|
26 | from IPython.html import nbextensions | |
27 | from IPython.html.nbextensions import install_nbextension |
|
27 | from IPython.html.nbextensions import install_nbextension, check_nbextension | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Test functions |
|
30 | # Test functions | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | def touch(file, mtime=None): |
|
33 | def touch(file, mtime=None): | |
34 | """ensure a file exists, and set its modification time |
|
34 | """ensure a file exists, and set its modification time | |
35 |
|
35 | |||
36 | returns the modification time of the file |
|
36 | returns the modification time of the file | |
37 | """ |
|
37 | """ | |
38 | open(file, 'a').close() |
|
38 | open(file, 'a').close() | |
39 | # set explicit mtime |
|
39 | # set explicit mtime | |
40 | if mtime: |
|
40 | if mtime: | |
41 | atime = os.stat(file).st_atime |
|
41 | atime = os.stat(file).st_atime | |
42 | os.utime(file, (atime, mtime)) |
|
42 | os.utime(file, (atime, mtime)) | |
43 | return os.stat(file).st_mtime |
|
43 | return os.stat(file).st_mtime | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | class TestInstallNBExtension(TestCase): |
|
46 | class TestInstallNBExtension(TestCase): | |
47 |
|
47 | |||
48 | def tempdir(self): |
|
48 | def tempdir(self): | |
49 | td = TemporaryDirectory() |
|
49 | td = TemporaryDirectory() | |
50 | self.tempdirs.append(td) |
|
50 | self.tempdirs.append(td) | |
51 | return py3compat.cast_unicode(td.name) |
|
51 | return py3compat.cast_unicode(td.name) | |
52 |
|
52 | |||
53 | def setUp(self): |
|
53 | def setUp(self): | |
54 | self.tempdirs = [] |
|
54 | self.tempdirs = [] | |
55 | src = self.src = self.tempdir() |
|
55 | src = self.src = self.tempdir() | |
56 | self.files = files = [ |
|
56 | self.files = files = [ | |
57 | pjoin(u'ƒile'), |
|
57 | pjoin(u'ƒile'), | |
58 | pjoin(u'∂ir', u'ƒile1'), |
|
58 | pjoin(u'∂ir', u'ƒile1'), | |
59 | pjoin(u'∂ir', u'∂ir2', u'ƒile2'), |
|
59 | pjoin(u'∂ir', u'∂ir2', u'ƒile2'), | |
60 | ] |
|
60 | ] | |
61 | for file in files: |
|
61 | for file in files: | |
62 | fullpath = os.path.join(self.src, file) |
|
62 | fullpath = os.path.join(self.src, file) | |
63 | parent = os.path.dirname(fullpath) |
|
63 | parent = os.path.dirname(fullpath) | |
64 | if not os.path.exists(parent): |
|
64 | if not os.path.exists(parent): | |
65 | os.makedirs(parent) |
|
65 | os.makedirs(parent) | |
66 | touch(fullpath) |
|
66 | touch(fullpath) | |
67 |
|
67 | |||
68 | self.ipdir = self.tempdir() |
|
68 | self.ipdir = self.tempdir() | |
69 | self.save_get_ipython_dir = nbextensions.get_ipython_dir |
|
69 | self.save_get_ipython_dir = nbextensions.get_ipython_dir | |
70 | nbextensions.get_ipython_dir = lambda : self.ipdir |
|
70 | nbextensions.get_ipython_dir = lambda : self.ipdir | |
71 |
|
71 | |||
72 | def tearDown(self): |
|
72 | def tearDown(self): | |
73 | for td in self.tempdirs: |
|
73 | for td in self.tempdirs: | |
74 | td.cleanup() |
|
74 | td.cleanup() | |
75 | nbextensions.get_ipython_dir = self.save_get_ipython_dir |
|
75 | nbextensions.get_ipython_dir = self.save_get_ipython_dir | |
76 |
|
76 | |||
77 | def assert_path_exists(self, path): |
|
77 | def assert_path_exists(self, path): | |
78 | if not os.path.exists(path): |
|
78 | if not os.path.exists(path): | |
79 | do_exist = os.listdir(os.path.dirname(path)) |
|
79 | do_exist = os.listdir(os.path.dirname(path)) | |
80 | self.fail(u"%s should exist (found %s)" % (path, do_exist)) |
|
80 | self.fail(u"%s should exist (found %s)" % (path, do_exist)) | |
81 |
|
81 | |||
82 | def assert_not_path_exists(self, path): |
|
82 | def assert_not_path_exists(self, path): | |
83 | if os.path.exists(path): |
|
83 | if os.path.exists(path): | |
84 | self.fail(u"%s should not exist" % path) |
|
84 | self.fail(u"%s should not exist" % path) | |
85 |
|
85 | |||
86 | def assert_installed(self, relative_path, ipdir=None): |
|
86 | def assert_installed(self, relative_path, ipdir=None): | |
87 | self.assert_path_exists( |
|
87 | self.assert_path_exists( | |
88 | pjoin(ipdir or self.ipdir, u'nbextensions', relative_path) |
|
88 | pjoin(ipdir or self.ipdir, u'nbextensions', relative_path) | |
89 | ) |
|
89 | ) | |
90 |
|
90 | |||
91 | def assert_not_installed(self, relative_path, ipdir=None): |
|
91 | def assert_not_installed(self, relative_path, ipdir=None): | |
92 | self.assert_not_path_exists( |
|
92 | self.assert_not_path_exists( | |
93 | pjoin(ipdir or self.ipdir, u'nbextensions', relative_path) |
|
93 | pjoin(ipdir or self.ipdir, u'nbextensions', relative_path) | |
94 | ) |
|
94 | ) | |
95 |
|
95 | |||
96 | def test_create_ipython_dir(self): |
|
96 | def test_create_ipython_dir(self): | |
97 | """install_nbextension when ipython_dir doesn't exist""" |
|
97 | """install_nbextension when ipython_dir doesn't exist""" | |
98 | with TemporaryDirectory() as td: |
|
98 | with TemporaryDirectory() as td: | |
99 | ipdir = pjoin(td, u'ipython') |
|
99 | ipdir = pjoin(td, u'ipython') | |
100 | install_nbextension(self.src, ipython_dir=ipdir) |
|
100 | install_nbextension(self.src, ipython_dir=ipdir) | |
101 | self.assert_path_exists(ipdir) |
|
101 | self.assert_path_exists(ipdir) | |
102 | for file in self.files: |
|
102 | for file in self.files: | |
103 | self.assert_installed( |
|
103 | self.assert_installed( | |
104 | pjoin(basename(self.src), file), |
|
104 | pjoin(basename(self.src), file), | |
105 | ipdir |
|
105 | ipdir | |
106 | ) |
|
106 | ) | |
107 |
|
107 | |||
108 | def test_create_nbextensions(self): |
|
108 | def test_create_nbextensions(self): | |
109 | with TemporaryDirectory() as ipdir: |
|
109 | with TemporaryDirectory() as ipdir: | |
110 | install_nbextension(self.src, ipython_dir=ipdir) |
|
110 | install_nbextension(self.src, ipython_dir=ipdir) | |
111 | self.assert_installed( |
|
111 | self.assert_installed( | |
112 | pjoin(basename(self.src), u'ƒile'), |
|
112 | pjoin(basename(self.src), u'ƒile'), | |
113 | ipdir |
|
113 | ipdir | |
114 | ) |
|
114 | ) | |
115 |
|
115 | |||
116 | def test_single_file(self): |
|
116 | def test_single_file(self): | |
117 | file = self.files[0] |
|
117 | file = self.files[0] | |
118 | install_nbextension(pjoin(self.src, file)) |
|
118 | install_nbextension(pjoin(self.src, file)) | |
119 | self.assert_installed(file) |
|
119 | self.assert_installed(file) | |
120 |
|
120 | |||
121 | def test_single_dir(self): |
|
121 | def test_single_dir(self): | |
122 | d = u'∂ir' |
|
122 | d = u'∂ir' | |
123 | install_nbextension(pjoin(self.src, d)) |
|
123 | install_nbextension(pjoin(self.src, d)) | |
124 | self.assert_installed(self.files[-1]) |
|
124 | self.assert_installed(self.files[-1]) | |
125 |
|
125 | |||
126 | def test_install_nbextension(self): |
|
126 | def test_install_nbextension(self): | |
127 | install_nbextension(glob.glob(pjoin(self.src, '*'))) |
|
127 | install_nbextension(glob.glob(pjoin(self.src, '*'))) | |
128 | for file in self.files: |
|
128 | for file in self.files: | |
129 | self.assert_installed(file) |
|
129 | self.assert_installed(file) | |
130 |
|
130 | |||
131 | def test_overwrite_file(self): |
|
131 | def test_overwrite_file(self): | |
132 | with TemporaryDirectory() as d: |
|
132 | with TemporaryDirectory() as d: | |
133 | fname = u'ƒ.js' |
|
133 | fname = u'ƒ.js' | |
134 | src = pjoin(d, fname) |
|
134 | src = pjoin(d, fname) | |
135 | with open(src, 'w') as f: |
|
135 | with open(src, 'w') as f: | |
136 | f.write('first') |
|
136 | f.write('first') | |
137 | mtime = touch(src) |
|
137 | mtime = touch(src) | |
138 | dest = pjoin(self.ipdir, u'nbextensions', fname) |
|
138 | dest = pjoin(self.ipdir, u'nbextensions', fname) | |
139 | install_nbextension(src) |
|
139 | install_nbextension(src) | |
140 | with open(src, 'w') as f: |
|
140 | with open(src, 'w') as f: | |
141 | f.write('overwrite') |
|
141 | f.write('overwrite') | |
142 | mtime = touch(src, mtime - 100) |
|
142 | mtime = touch(src, mtime - 100) | |
143 | install_nbextension(src, overwrite=True) |
|
143 | install_nbextension(src, overwrite=True) | |
144 | with open(dest) as f: |
|
144 | with open(dest) as f: | |
145 | self.assertEqual(f.read(), 'overwrite') |
|
145 | self.assertEqual(f.read(), 'overwrite') | |
146 |
|
146 | |||
147 | def test_overwrite_dir(self): |
|
147 | def test_overwrite_dir(self): | |
148 | with TemporaryDirectory() as src: |
|
148 | with TemporaryDirectory() as src: | |
149 | # src = py3compat.cast_unicode_py2(src) |
|
149 | # src = py3compat.cast_unicode_py2(src) | |
150 | base = basename(src) |
|
150 | base = basename(src) | |
151 | fname = u'ƒ.js' |
|
151 | fname = u'ƒ.js' | |
152 | touch(pjoin(src, fname)) |
|
152 | touch(pjoin(src, fname)) | |
153 | install_nbextension(src) |
|
153 | install_nbextension(src) | |
154 | self.assert_installed(pjoin(base, fname)) |
|
154 | self.assert_installed(pjoin(base, fname)) | |
155 | os.remove(pjoin(src, fname)) |
|
155 | os.remove(pjoin(src, fname)) | |
156 | fname2 = u'∂.js' |
|
156 | fname2 = u'∂.js' | |
157 | touch(pjoin(src, fname2)) |
|
157 | touch(pjoin(src, fname2)) | |
158 | install_nbextension(src, overwrite=True) |
|
158 | install_nbextension(src, overwrite=True) | |
159 | self.assert_installed(pjoin(base, fname2)) |
|
159 | self.assert_installed(pjoin(base, fname2)) | |
160 | self.assert_not_installed(pjoin(base, fname)) |
|
160 | self.assert_not_installed(pjoin(base, fname)) | |
161 |
|
161 | |||
162 | def test_update_file(self): |
|
162 | def test_update_file(self): | |
163 | with TemporaryDirectory() as d: |
|
163 | with TemporaryDirectory() as d: | |
164 | fname = u'ƒ.js' |
|
164 | fname = u'ƒ.js' | |
165 | src = pjoin(d, fname) |
|
165 | src = pjoin(d, fname) | |
166 | with open(src, 'w') as f: |
|
166 | with open(src, 'w') as f: | |
167 | f.write('first') |
|
167 | f.write('first') | |
168 | mtime = touch(src) |
|
168 | mtime = touch(src) | |
169 | install_nbextension(src) |
|
169 | install_nbextension(src) | |
170 | self.assert_installed(fname) |
|
170 | self.assert_installed(fname) | |
171 | dest = pjoin(self.ipdir, u'nbextensions', fname) |
|
171 | dest = pjoin(self.ipdir, u'nbextensions', fname) | |
172 | old_mtime = os.stat(dest).st_mtime |
|
172 | old_mtime = os.stat(dest).st_mtime | |
173 | with open(src, 'w') as f: |
|
173 | with open(src, 'w') as f: | |
174 | f.write('overwrite') |
|
174 | f.write('overwrite') | |
175 | touch(src, mtime + 10) |
|
175 | touch(src, mtime + 10) | |
176 | install_nbextension(src) |
|
176 | install_nbextension(src) | |
177 | with open(dest) as f: |
|
177 | with open(dest) as f: | |
178 | self.assertEqual(f.read(), 'overwrite') |
|
178 | self.assertEqual(f.read(), 'overwrite') | |
179 |
|
179 | |||
180 | def test_skip_old_file(self): |
|
180 | def test_skip_old_file(self): | |
181 | with TemporaryDirectory() as d: |
|
181 | with TemporaryDirectory() as d: | |
182 | fname = u'ƒ.js' |
|
182 | fname = u'ƒ.js' | |
183 | src = pjoin(d, fname) |
|
183 | src = pjoin(d, fname) | |
184 | mtime = touch(src) |
|
184 | mtime = touch(src) | |
185 | install_nbextension(src) |
|
185 | install_nbextension(src) | |
186 | self.assert_installed(fname) |
|
186 | self.assert_installed(fname) | |
187 | dest = pjoin(self.ipdir, u'nbextensions', fname) |
|
187 | dest = pjoin(self.ipdir, u'nbextensions', fname) | |
188 | old_mtime = os.stat(dest).st_mtime |
|
188 | old_mtime = os.stat(dest).st_mtime | |
189 |
|
189 | |||
190 | mtime = touch(src, mtime - 100) |
|
190 | mtime = touch(src, mtime - 100) | |
191 | install_nbextension(src) |
|
191 | install_nbextension(src) | |
192 | new_mtime = os.stat(dest).st_mtime |
|
192 | new_mtime = os.stat(dest).st_mtime | |
193 | self.assertEqual(new_mtime, old_mtime) |
|
193 | self.assertEqual(new_mtime, old_mtime) | |
194 |
|
194 | |||
195 | def test_quiet(self): |
|
195 | def test_quiet(self): | |
196 | with tt.AssertNotPrints(re.compile(r'.+')): |
|
196 | with tt.AssertNotPrints(re.compile(r'.+')): | |
197 | install_nbextension(self.src, verbose=0) |
|
197 | install_nbextension(self.src, verbose=0) | |
198 |
|
198 | |||
199 | def test_install_zip(self): |
|
199 | def test_install_zip(self): | |
200 | path = pjoin(self.src, "myjsext.zip") |
|
200 | path = pjoin(self.src, "myjsext.zip") | |
201 | with zipfile.ZipFile(path, 'w') as f: |
|
201 | with zipfile.ZipFile(path, 'w') as f: | |
202 | f.writestr("a.js", b"b();") |
|
202 | f.writestr("a.js", b"b();") | |
203 | f.writestr("foo/a.js", b"foo();") |
|
203 | f.writestr("foo/a.js", b"foo();") | |
204 | install_nbextension(path) |
|
204 | install_nbextension(path) | |
205 | self.assert_installed("a.js") |
|
205 | self.assert_installed("a.js") | |
206 | self.assert_installed(pjoin("foo", "a.js")) |
|
206 | self.assert_installed(pjoin("foo", "a.js")) | |
207 |
|
207 | |||
208 | def test_install_tar(self): |
|
208 | def test_install_tar(self): | |
209 | def _add_file(f, fname, buf): |
|
209 | def _add_file(f, fname, buf): | |
210 | info = tarfile.TarInfo(fname) |
|
210 | info = tarfile.TarInfo(fname) | |
211 | info.size = len(buf) |
|
211 | info.size = len(buf) | |
212 | f.addfile(info, BytesIO(buf)) |
|
212 | f.addfile(info, BytesIO(buf)) | |
213 |
|
213 | |||
214 | for i,ext in enumerate((".tar.gz", ".tgz", ".tar.bz2")): |
|
214 | for i,ext in enumerate((".tar.gz", ".tgz", ".tar.bz2")): | |
215 | path = pjoin(self.src, "myjsext" + ext) |
|
215 | path = pjoin(self.src, "myjsext" + ext) | |
216 | with tarfile.open(path, 'w') as f: |
|
216 | with tarfile.open(path, 'w') as f: | |
217 | _add_file(f, "b%i.js" % i, b"b();") |
|
217 | _add_file(f, "b%i.js" % i, b"b();") | |
218 | _add_file(f, "foo/b%i.js" % i, b"foo();") |
|
218 | _add_file(f, "foo/b%i.js" % i, b"foo();") | |
219 | install_nbextension(path) |
|
219 | install_nbextension(path) | |
220 | self.assert_installed("b%i.js" % i) |
|
220 | self.assert_installed("b%i.js" % i) | |
221 | self.assert_installed(pjoin("foo", "b%i.js" % i)) |
|
221 | self.assert_installed(pjoin("foo", "b%i.js" % i)) | |
222 |
|
222 | |||
223 | def test_install_url(self): |
|
223 | def test_install_url(self): | |
224 | def fake_urlretrieve(url, dest): |
|
224 | def fake_urlretrieve(url, dest): | |
225 | touch(dest) |
|
225 | touch(dest) | |
226 | save_urlretrieve = nbextensions.urlretrieve |
|
226 | save_urlretrieve = nbextensions.urlretrieve | |
227 | nbextensions.urlretrieve = fake_urlretrieve |
|
227 | nbextensions.urlretrieve = fake_urlretrieve | |
228 | try: |
|
228 | try: | |
229 | install_nbextension("http://example.com/path/to/foo.js") |
|
229 | install_nbextension("http://example.com/path/to/foo.js") | |
230 | self.assert_installed("foo.js") |
|
230 | self.assert_installed("foo.js") | |
231 | install_nbextension("https://example.com/path/to/another/bar.js") |
|
231 | install_nbextension("https://example.com/path/to/another/bar.js") | |
232 | self.assert_installed("bar.js") |
|
232 | self.assert_installed("bar.js") | |
233 | finally: |
|
233 | finally: | |
234 | nbextensions.urlretrieve = save_urlretrieve |
|
234 | nbextensions.urlretrieve = save_urlretrieve | |
|
235 | ||||
|
236 | def test_check_nbextension(self): | |||
|
237 | with TemporaryDirectory() as d: | |||
|
238 | f = u'ƒ.js' | |||
|
239 | src = pjoin(d, f) | |||
|
240 | touch(src) | |||
|
241 | install_nbextension(src) | |||
|
242 | ||||
|
243 | assert check_nbextension(f, self.ipdir) | |||
|
244 | assert check_nbextension([f], self.ipdir) | |||
|
245 | assert not check_nbextension([f, pjoin('dne', f)], self.ipdir) | |||
|
246 |
General Comments 0
You need to be logged in to leave comments.
Login now