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