test_jupyterlab.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. # coding: utf-8
  2. """Test installation of JupyterLab extensions"""
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. import glob
  6. import json
  7. import logging
  8. import os
  9. import platform
  10. import shutil
  11. import subprocess
  12. import sys
  13. from os.path import join as pjoin
  14. from pathlib import Path
  15. from tempfile import TemporaryDirectory
  16. from unittest import TestCase
  17. from unittest.mock import patch
  18. import pytest
  19. from jupyter_core import paths
  20. from jupyterlab import commands
  21. from jupyterlab.commands import (
  22. AppOptions, _compare_ranges, _test_overlap,
  23. build, build_check, check_extension,
  24. disable_extension, enable_extension,
  25. get_app_info, install_extension, link_package,
  26. list_extensions, uninstall_extension,
  27. unlink_package, update_extension, get_app_version
  28. )
  29. from jupyterlab.coreconfig import CoreConfig, _get_default_core_data
  30. here = os.path.dirname(os.path.abspath(__file__))
  31. def touch(file, mtime=None):
  32. """ensure a file exists, and set its modification time
  33. returns the modification time of the file
  34. """
  35. dirname = os.path.dirname(file)
  36. if not os.path.exists(dirname):
  37. os.makedirs(dirname)
  38. open(file, 'a').close()
  39. # set explicit mtime
  40. if mtime:
  41. atime = os.stat(file).st_atime
  42. os.utime(file, (atime, mtime))
  43. return os.stat(file).st_mtime
  44. # @pytest.fixture()
  45. # def resource():
  46. # print("setup")
  47. # yield "resource"
  48. # print("teardown")
  49. class AppHandlerTest(TestCase):
  50. def tempdir(self):
  51. td = TemporaryDirectory()
  52. self.tempdirs.append(td)
  53. return td.name
  54. def setUp(self):
  55. # Any TemporaryDirectory objects appended to this list will be cleaned
  56. # up at the end of the test run.
  57. self.tempdirs = []
  58. self.devnull = open(os.devnull, 'w')
  59. @self.addCleanup
  60. def cleanup_tempdirs():
  61. for d in self.tempdirs:
  62. d.cleanup()
  63. self.test_dir = self.tempdir()
  64. self.data_dir = pjoin(self.test_dir, 'data')
  65. self.config_dir = pjoin(self.test_dir, 'config')
  66. self.pkg_names = dict()
  67. # Copy in the mock packages.
  68. for name in ['extension', 'incompat', 'package', 'mimeextension']:
  69. src = pjoin(here, 'mock_packages', name)
  70. def ignore(dname, files):
  71. if 'node_modules' in dname:
  72. files = []
  73. if 'node_modules' in files:
  74. files.remove('node_modules')
  75. return dname, files
  76. dest = pjoin(self.test_dir, name)
  77. shutil.copytree(src, dest, ignore=ignore)
  78. # Make a node modules folder so npm install is not called.
  79. if not os.path.exists(pjoin(dest, 'node_modules')):
  80. os.makedirs(pjoin(dest, 'node_modules'))
  81. setattr(self, 'mock_' + name, dest)
  82. with open(pjoin(dest, 'package.json')) as fid:
  83. data = json.load(fid)
  84. self.pkg_names[name] = data['name']
  85. self.patches = []
  86. p = patch.dict('os.environ', {
  87. 'JUPYTER_CONFIG_DIR': self.config_dir,
  88. 'JUPYTER_DATA_DIR': self.data_dir,
  89. 'JUPYTERLAB_DIR': pjoin(self.data_dir, 'lab')
  90. })
  91. self.patches.append(p)
  92. for mod in [paths]:
  93. if hasattr(mod, 'ENV_JUPYTER_PATH'):
  94. p = patch.object(mod, 'ENV_JUPYTER_PATH', [self.data_dir])
  95. self.patches.append(p)
  96. if hasattr(mod, 'ENV_CONFIG_PATH'):
  97. p = patch.object(mod, 'ENV_CONFIG_PATH', [self.config_dir])
  98. self.patches.append(p)
  99. if hasattr(mod, 'CONFIG_PATH'):
  100. p = patch.object(mod, 'CONFIG_PATH', self.config_dir)
  101. self.patches.append(p)
  102. if hasattr(mod, 'BUILD_PATH'):
  103. p = patch.object(mod, 'BUILD_PATH', self.data_dir)
  104. self.patches.append(p)
  105. for p in self.patches:
  106. p.start()
  107. self.addCleanup(p.stop)
  108. # verify our patches
  109. self.assertEqual(paths.ENV_CONFIG_PATH, [self.config_dir])
  110. self.assertEqual(paths.ENV_JUPYTER_PATH, [self.data_dir])
  111. self.assertEqual(
  112. Path(commands.get_app_dir()).resolve(),
  113. (Path(self.data_dir) / 'lab').resolve()
  114. )
  115. self.app_dir = commands.get_app_dir()
  116. # Set pinned extension names
  117. self.pinned_packages = ['jupyterlab-test-extension@1.0', 'jupyterlab-test-extension@2.0']
  118. class TestExtension(AppHandlerTest):
  119. def test_install_extension(self):
  120. assert install_extension(self.mock_extension) is True
  121. path = pjoin(self.app_dir, 'extensions', '*.tgz')
  122. assert glob.glob(path)
  123. extensions = get_app_info()['extensions']
  124. name = self.pkg_names['extension']
  125. assert name in extensions
  126. assert check_extension(name)
  127. def test_install_twice(self):
  128. assert install_extension(self.mock_extension) is True
  129. path = pjoin(self.app_dir, 'extensions', '*.tgz')
  130. assert install_extension(self.mock_extension) is True
  131. assert glob.glob(path)
  132. extensions = get_app_info()['extensions']
  133. name = self.pkg_names['extension']
  134. assert name in extensions
  135. assert check_extension(name)
  136. def test_install_mime_renderer(self):
  137. install_extension(self.mock_mimeextension)
  138. name = self.pkg_names['mimeextension']
  139. assert name in get_app_info()['extensions']
  140. assert check_extension(name)
  141. assert uninstall_extension(name) is True
  142. assert name not in get_app_info()['extensions']
  143. assert not check_extension(name)
  144. def test_install_incompatible(self):
  145. with pytest.raises(ValueError) as excinfo:
  146. install_extension(self.mock_incompat)
  147. assert 'Conflicting Dependencies' in str(excinfo.value)
  148. assert not check_extension(self.pkg_names["incompat"])
  149. def test_install_failed(self):
  150. path = self.mock_package
  151. with pytest.raises(ValueError):
  152. install_extension(path)
  153. with open(pjoin(path, 'package.json')) as fid:
  154. data = json.load(fid)
  155. extensions = get_app_info()['extensions']
  156. name = data['name']
  157. assert name not in extensions
  158. assert not check_extension(name)
  159. def test_validation(self):
  160. path = self.mock_extension
  161. os.remove(pjoin(path, 'index.js'))
  162. with pytest.raises(ValueError):
  163. install_extension(path)
  164. assert not check_extension(self.pkg_names["extension"])
  165. path = self.mock_mimeextension
  166. os.remove(pjoin(path, 'index.js'))
  167. with pytest.raises(ValueError):
  168. install_extension(path)
  169. assert not check_extension(self.pkg_names["mimeextension"])
  170. def test_uninstall_extension(self):
  171. assert install_extension(self.mock_extension) is True
  172. name = self.pkg_names['extension']
  173. assert check_extension(name)
  174. assert uninstall_extension(self.pkg_names['extension']) is True
  175. path = pjoin(self.app_dir, 'extensions', '*.tgz')
  176. assert not glob.glob(path)
  177. extensions = get_app_info()['extensions']
  178. assert name not in extensions
  179. assert not check_extension(name)
  180. def test_uninstall_all_extensions(self):
  181. install_extension(self.mock_extension)
  182. install_extension(self.mock_mimeextension)
  183. ext_name = self.pkg_names['extension']
  184. mime_ext_name = self.pkg_names['mimeextension']
  185. assert check_extension(ext_name) is True
  186. assert check_extension(mime_ext_name) is True
  187. assert uninstall_extension(all_=True) is True
  188. extensions = get_app_info()['extensions']
  189. assert ext_name not in extensions
  190. assert mime_ext_name not in extensions
  191. @pytest.mark.slow
  192. def test_uninstall_core_extension(self):
  193. assert uninstall_extension('@jupyterlab/console-extension') is True
  194. app_dir = self.app_dir
  195. build()
  196. with open(pjoin(app_dir, 'staging', 'package.json')) as fid:
  197. data = json.load(fid)
  198. extensions = data['jupyterlab']['extensions']
  199. assert '@jupyterlab/console-extension' not in extensions
  200. assert not check_extension('@jupyterlab/console-extension')
  201. assert install_extension('@jupyterlab/console-extension') is True
  202. build()
  203. with open(pjoin(app_dir, 'staging', 'package.json')) as fid:
  204. data = json.load(fid)
  205. extensions = data['jupyterlab']['extensions']
  206. assert '@jupyterlab/console-extension' in extensions
  207. assert check_extension('@jupyterlab/console-extension')
  208. def test_install_and_uninstall_pinned(self):
  209. """
  210. You should be able to install different versions of the same extension with different
  211. pinned names and uninstall them with those names.
  212. """
  213. NAMES = ['test-1', 'test-2']
  214. assert install_extension(self.pinned_packages[0], pin=NAMES[0])
  215. assert install_extension(self.pinned_packages[1], pin=NAMES[1])
  216. extensions = get_app_info()['extensions']
  217. assert NAMES[0] in extensions
  218. assert NAMES[1] in extensions
  219. assert check_extension(NAMES[0])
  220. assert check_extension(NAMES[1])
  221. # Uninstall
  222. assert uninstall_extension(NAMES[0])
  223. assert uninstall_extension(NAMES[1])
  224. extensions = get_app_info()['extensions']
  225. assert NAMES[0] not in extensions
  226. assert NAMES[1] not in extensions
  227. assert not check_extension(NAMES[0])
  228. assert not check_extension(NAMES[1])
  229. @pytest.mark.skipif(platform.system() == 'Windows', reason='running npm pack fails on windows CI')
  230. def test_install_and_uninstall_pinned_folder(self):
  231. """
  232. Same as above test, but installs from a local folder instead of from npm.
  233. """
  234. # Download each version of the package from NPM:
  235. base_dir = Path(self.tempdir())
  236. # The archive file names are printed to stdout when run `npm pack`
  237. packages = [
  238. subprocess.run(
  239. ['npm', 'pack', name],
  240. stdout=subprocess.PIPE,
  241. universal_newlines=True,
  242. check=True,
  243. cwd=str(base_dir)
  244. ).stdout.strip()
  245. for name in self.pinned_packages
  246. ]
  247. shutil.unpack_archive(str(base_dir / packages[0]), str(base_dir / '1'))
  248. shutil.unpack_archive(str(base_dir / packages[1]), str(base_dir / '2'))
  249. # Change pinned packages to be these directories now, so we install from these folders
  250. self.pinned_packages = [str(base_dir / '1' / 'package'), str(base_dir / '2' / 'package')]
  251. self.test_install_and_uninstall_pinned()
  252. def test_link_extension(self):
  253. path = self.mock_extension
  254. name = self.pkg_names['extension']
  255. link_package(path)
  256. linked = get_app_info()['linked_packages']
  257. assert name not in linked
  258. assert name in get_app_info()['extensions']
  259. assert check_extension(name)
  260. assert unlink_package(path) is True
  261. linked = get_app_info()['linked_packages']
  262. assert name not in linked
  263. assert name not in get_app_info()['extensions']
  264. assert not check_extension(name)
  265. def test_link_package(self):
  266. path = self.mock_package
  267. name = self.pkg_names['package']
  268. assert link_package(path) is True
  269. linked = get_app_info()['linked_packages']
  270. assert name in linked
  271. assert name not in get_app_info()['extensions']
  272. assert check_extension(name)
  273. assert unlink_package(path)
  274. linked = get_app_info()['linked_packages']
  275. assert name not in linked
  276. assert not check_extension(name)
  277. def test_unlink_package(self):
  278. target = self.mock_package
  279. assert link_package(target) is True
  280. assert unlink_package(target) is True
  281. linked = get_app_info()['linked_packages']
  282. name = self.pkg_names['package']
  283. assert name not in linked
  284. assert not check_extension(name)
  285. def test_list_extensions(self):
  286. assert install_extension(self.mock_extension) is True
  287. list_extensions()
  288. def test_app_dir(self):
  289. app_dir = self.tempdir()
  290. options = AppOptions(app_dir=app_dir)
  291. assert install_extension(self.mock_extension, app_options=options) is True
  292. path = pjoin(app_dir, 'extensions', '*.tgz')
  293. assert glob.glob(path)
  294. extensions = get_app_info(app_options=options)['extensions']
  295. ext_name = self.pkg_names['extension']
  296. assert ext_name in extensions
  297. assert check_extension(ext_name, app_options=options)
  298. assert uninstall_extension(self.pkg_names['extension'], app_options=options) is True
  299. path = pjoin(app_dir, 'extensions', '*.tgz')
  300. assert not glob.glob(path)
  301. extensions = get_app_info(app_options=options)['extensions']
  302. assert ext_name not in extensions
  303. assert not check_extension(ext_name, app_options=options)
  304. assert link_package(self.mock_package, app_options=options) is True
  305. linked = get_app_info(app_options=options)['linked_packages']
  306. pkg_name = self.pkg_names['package']
  307. assert pkg_name in linked
  308. assert check_extension(pkg_name, app_options=options)
  309. assert unlink_package(self.mock_package, app_options=options) is True
  310. linked = get_app_info(app_options=options)['linked_packages']
  311. assert pkg_name not in linked
  312. assert not check_extension(pkg_name, app_options=options)
  313. def test_app_dir_use_sys_prefix(self):
  314. app_dir = self.tempdir()
  315. options = AppOptions(app_dir=app_dir)
  316. if os.path.exists(self.app_dir):
  317. os.removedirs(self.app_dir)
  318. assert install_extension(self.mock_extension) is True
  319. path = pjoin(app_dir, 'extensions', '*.tgz')
  320. assert not glob.glob(path)
  321. extensions = get_app_info(app_options=options)['extensions']
  322. ext_name = self.pkg_names['extension']
  323. assert ext_name in extensions
  324. assert check_extension(ext_name, app_options=options)
  325. def test_app_dir_disable_sys_prefix(self):
  326. app_dir = self.tempdir()
  327. options = AppOptions(app_dir=app_dir, use_sys_dir=False)
  328. if os.path.exists(self.app_dir):
  329. os.removedirs(self.app_dir)
  330. assert install_extension(self.mock_extension) is True
  331. path = pjoin(app_dir, 'extensions', '*.tgz')
  332. assert not glob.glob(path)
  333. extensions = get_app_info(app_options=options)['extensions']
  334. ext_name = self.pkg_names['extension']
  335. assert ext_name not in extensions
  336. assert not check_extension(ext_name, app_options=options)
  337. def test_app_dir_shadowing(self):
  338. app_dir = self.tempdir()
  339. sys_dir = self.app_dir
  340. app_options = AppOptions(app_dir=app_dir)
  341. if os.path.exists(sys_dir):
  342. os.removedirs(sys_dir)
  343. assert install_extension(self.mock_extension) is True
  344. sys_path = pjoin(sys_dir, 'extensions', '*.tgz')
  345. assert glob.glob(sys_path)
  346. app_path = pjoin(app_dir, 'extensions', '*.tgz')
  347. assert not glob.glob(app_path)
  348. extensions = get_app_info(app_options=app_options)['extensions']
  349. ext_name = self.pkg_names['extension']
  350. assert ext_name in extensions
  351. assert check_extension(ext_name, app_options=app_options)
  352. assert install_extension(self.mock_extension, app_options=app_options) is True
  353. assert glob.glob(app_path)
  354. extensions = get_app_info(app_options=app_options)['extensions']
  355. assert ext_name in extensions
  356. assert check_extension(ext_name, app_options=app_options)
  357. assert uninstall_extension(self.pkg_names['extension'], app_options=app_options) is True
  358. assert not glob.glob(app_path)
  359. assert glob.glob(sys_path)
  360. extensions = get_app_info(app_options=app_options)['extensions']
  361. assert ext_name in extensions
  362. assert check_extension(ext_name, app_options=app_options)
  363. assert uninstall_extension(self.pkg_names['extension'], app_options=app_options) is True
  364. assert not glob.glob(app_path)
  365. assert not glob.glob(sys_path)
  366. extensions = get_app_info(app_options=app_options)['extensions']
  367. assert ext_name not in extensions
  368. assert not check_extension(ext_name, app_options=app_options)
  369. @pytest.mark.slow
  370. def test_build(self):
  371. assert install_extension(self.mock_extension) is True
  372. build()
  373. # check staging directory.
  374. entry = pjoin(self.app_dir, 'staging', 'build', 'index.out.js')
  375. with open(entry) as fid:
  376. data = fid.read()
  377. assert self.pkg_names['extension'] in data
  378. # check static directory.
  379. entry = pjoin(self.app_dir, 'static', 'index.out.js')
  380. with open(entry) as fid:
  381. data = fid.read()
  382. assert self.pkg_names['extension'] in data
  383. @pytest.mark.slow
  384. def test_build_splice_packages(self):
  385. app_options = AppOptions(splice_source=True)
  386. assert install_extension(self.mock_extension) is True
  387. build(app_options=app_options)
  388. assert '-spliced' in get_app_version(app_options)
  389. # check staging directory.
  390. entry = pjoin(self.app_dir, 'staging', 'build', 'index.out.js')
  391. with open(entry) as fid:
  392. data = fid.read()
  393. assert self.pkg_names['extension'] in data
  394. # check static directory.
  395. entry = pjoin(self.app_dir, 'static', 'index.out.js')
  396. with open(entry) as fid:
  397. data = fid.read()
  398. assert self.pkg_names['extension'] in data
  399. @pytest.mark.slow
  400. def test_build_custom(self):
  401. assert install_extension(self.mock_extension) is True
  402. build(name='foo', version='1.0', static_url='bar')
  403. # check static directory.
  404. entry = pjoin(self.app_dir, 'static', 'index.out.js')
  405. with open(entry) as fid:
  406. data = fid.read()
  407. assert self.pkg_names['extension'] in data
  408. pkg = pjoin(self.app_dir, 'static', 'package.json')
  409. with open(pkg) as fid:
  410. data = json.load(fid)
  411. assert data['jupyterlab']['name'] == 'foo'
  412. assert data['jupyterlab']['version'] == '1.0'
  413. assert data['jupyterlab']['staticUrl'] == 'bar'
  414. @pytest.mark.slow
  415. def test_build_custom_minimal_core_config(self):
  416. default_config = CoreConfig()
  417. core_config = CoreConfig()
  418. core_config.clear_packages()
  419. logger = logging.getLogger('jupyterlab_test_logger')
  420. logger.setLevel('DEBUG')
  421. app_dir = self.tempdir()
  422. options = AppOptions(
  423. app_dir=app_dir,
  424. core_config=core_config,
  425. logger=logger,
  426. use_sys_dir=False,
  427. )
  428. extensions = (
  429. '@jupyterlab/application-extension',
  430. '@jupyterlab/apputils-extension',
  431. )
  432. singletons = (
  433. "@jupyterlab/application",
  434. "@jupyterlab/apputils",
  435. "@jupyterlab/coreutils",
  436. "@jupyterlab/services",
  437. )
  438. for name in extensions:
  439. semver = default_config.extensions[name]
  440. core_config.add(name, semver, extension=True)
  441. for name in singletons:
  442. semver = default_config.singletons[name]
  443. core_config.add(name, semver)
  444. assert install_extension(self.mock_extension, app_options=options) is True
  445. build(app_options=options)
  446. # check static directory.
  447. entry = pjoin(app_dir, 'static', 'index.out.js')
  448. with open(entry) as fid:
  449. data = fid.read()
  450. assert self.pkg_names['extension'] in data
  451. pkg = pjoin(app_dir, 'static', 'package.json')
  452. with open(pkg) as fid:
  453. data = json.load(fid)
  454. assert sorted(data['jupyterlab']['extensions'].keys()) == [
  455. '@jupyterlab/application-extension',
  456. '@jupyterlab/apputils-extension',
  457. '@jupyterlab/mock-extension',
  458. ]
  459. assert data['jupyterlab']['mimeExtensions'] == {}
  460. for pkg in data['jupyterlab']['singletonPackages']:
  461. if pkg.startswith('@jupyterlab/'):
  462. assert pkg in singletons
  463. def test_disable_extension(self):
  464. options = AppOptions(app_dir=self.tempdir())
  465. assert install_extension(self.mock_extension, app_options=options) is True
  466. assert disable_extension(self.pkg_names['extension'], app_options=options) is True
  467. info = get_app_info(app_options=options)
  468. name = self.pkg_names['extension']
  469. assert name in info['disabled']
  470. assert not check_extension(name, app_options=options)
  471. assert check_extension(name, installed=True, app_options=options)
  472. assert disable_extension('@jupyterlab/notebook-extension', app_options=options) is True
  473. info = get_app_info(app_options=options)
  474. assert '@jupyterlab/notebook-extension' in info['disabled']
  475. assert not check_extension('@jupyterlab/notebook-extension', app_options=options)
  476. assert check_extension('@jupyterlab/notebook-extension', installed=True, app_options=options)
  477. assert name in info['disabled']
  478. assert not check_extension(name, app_options=options)
  479. assert check_extension(name, installed=True, app_options=options)
  480. def test_enable_extension(self):
  481. options = AppOptions(app_dir=self.tempdir())
  482. assert install_extension(self.mock_extension, app_options=options) is True
  483. assert disable_extension(self.pkg_names['extension'], app_options=options) is True
  484. assert enable_extension(self.pkg_names['extension'], app_options=options) is True
  485. info = get_app_info(app_options=options)
  486. assert '@jupyterlab/notebook-extension' not in info['disabled']
  487. name = self.pkg_names['extension']
  488. assert name not in info['disabled']
  489. assert check_extension(name, app_options=options)
  490. assert disable_extension('@jupyterlab/notebook-extension', app_options=options) is True
  491. assert check_extension(name, app_options=options)
  492. assert not check_extension('@jupyterlab/notebook-extension', app_options=options)
  493. @pytest.mark.slow
  494. def test_build_check(self):
  495. # Do the initial build.
  496. assert build_check()
  497. assert install_extension(self.mock_extension) is True
  498. assert link_package(self.mock_package) is True
  499. build()
  500. assert not build_check()
  501. # Check installed extensions.
  502. assert install_extension(self.mock_mimeextension) is True
  503. assert build_check()
  504. assert uninstall_extension(self.pkg_names['mimeextension']) is True
  505. assert not build_check()
  506. # Check local extensions.
  507. pkg_path = pjoin(self.mock_extension, 'package.json')
  508. with open(pkg_path) as fid:
  509. data = json.load(fid)
  510. with open(pkg_path, 'rb') as fid:
  511. orig = fid.read()
  512. data['foo'] = 'bar'
  513. with open(pkg_path, 'w') as fid:
  514. json.dump(data, fid)
  515. assert build_check()
  516. assert build_check()
  517. with open(pkg_path, 'wb') as fid:
  518. fid.write(orig)
  519. assert not build_check()
  520. # Check linked packages.
  521. pkg_path = pjoin(self.mock_package, 'index.js')
  522. with open(pkg_path, 'rb') as fid:
  523. orig = fid.read()
  524. with open(pkg_path, 'wb') as fid:
  525. fid.write(orig + b'\nconsole.log("hello");')
  526. assert build_check()
  527. assert build_check()
  528. with open(pkg_path, 'wb') as fid:
  529. fid.write(orig)
  530. assert not build_check()
  531. def test_compatibility(self):
  532. assert _test_overlap('^0.6.0', '^0.6.1')
  533. assert _test_overlap('>0.1', '0.6')
  534. assert _test_overlap('~0.5.0', '~0.5.2')
  535. assert _test_overlap('0.5.2', '^0.5.0')
  536. assert not _test_overlap('^0.5.0', '^0.6.0')
  537. assert not _test_overlap('~1.5.0', '^1.6.0')
  538. assert _test_overlap('*', '0.6') is None
  539. assert _test_overlap('<0.6', '0.1') is None
  540. assert _test_overlap('^1 || ^2', '^1')
  541. assert _test_overlap('^1 || ^2', '^2')
  542. assert _test_overlap('^1', '^1 || ^2')
  543. assert _test_overlap('^2', '^1 || ^2')
  544. assert _test_overlap('^1 || ^2', '^2 || ^3')
  545. assert not _test_overlap('^1 || ^2', '^3 || ^4')
  546. assert not _test_overlap('^2', '^1 || ^3')
  547. def test_compare_ranges(self):
  548. assert _compare_ranges('^1 || ^2', '^1') == 0
  549. assert _compare_ranges('^1 || ^2', '^2 || ^3') == 0
  550. assert _compare_ranges('^1 || ^2', '^3 || ^4') == 1
  551. assert _compare_ranges('^3 || ^4', '^1 || ^2') == -1
  552. assert _compare_ranges('^2 || ^3', '^1 || ^4') is None
  553. def test_install_compatible(self):
  554. core_data = _get_default_core_data()
  555. current_app_dep = core_data['dependencies']['@jupyterlab/application']
  556. def _gen_dep(ver):
  557. return { "dependencies": {
  558. '@jupyterlab/application': ver
  559. }}
  560. def _mock_metadata(registry, name, logger):
  561. assert name == 'mockextension'
  562. return {
  563. "name": name,
  564. "versions": {
  565. "0.9.0": _gen_dep(current_app_dep),
  566. "1.0.0": _gen_dep(current_app_dep),
  567. "1.1.0": _gen_dep(current_app_dep),
  568. "2.0.0": _gen_dep('^2000.0.0'),
  569. "2.0.0-b0": _gen_dep(current_app_dep),
  570. "2.1.0-b0": _gen_dep('^2000.0.0'),
  571. "2.1.0": _gen_dep('^2000.0.0'),
  572. }
  573. }
  574. def _mock_extract(self, source, tempdir, *args, **kwargs):
  575. data = dict(
  576. name=source, version='2.1.0',
  577. jupyterlab=dict(extension=True),
  578. jupyterlab_extracted_files=['index.js'],
  579. )
  580. data.update(_gen_dep('^2000.0.0'))
  581. info = dict(
  582. source=source, is_dir=False, data=data,
  583. name=source, version=data['version'],
  584. filename='mockextension.tgz',
  585. path=pjoin(tempdir, 'mockextension.tgz'),
  586. )
  587. return info
  588. class Success(Exception):
  589. pass
  590. def _mock_install(self, name, *args, **kwargs):
  591. assert name in ('mockextension', 'mockextension@1.1.0')
  592. if name == 'mockextension@1.1.0':
  593. raise Success()
  594. return orig_install(self, name, *args, **kwargs)
  595. p1 = patch.object(
  596. commands,
  597. '_fetch_package_metadata',
  598. _mock_metadata)
  599. p2 = patch.object(
  600. commands._AppHandler,
  601. '_extract_package',
  602. _mock_extract)
  603. p3 = patch.object(
  604. commands._AppHandler,
  605. '_install_extension',
  606. _mock_install)
  607. with p1, p2:
  608. orig_install = commands._AppHandler._install_extension
  609. with p3, pytest.raises(Success):
  610. assert install_extension('mockextension') is True
  611. def test_update_single(self):
  612. installed = []
  613. def _mock_install(self, name, *args, **kwargs):
  614. installed.append(name[0] + name[1:].split('@')[0])
  615. return dict(name=name, is_dir=False, path='foo/bar/' + name)
  616. def _mock_latest(self, name):
  617. return '10000.0.0'
  618. p1 = patch.object(
  619. commands._AppHandler,
  620. '_install_extension',
  621. _mock_install)
  622. p2 = patch.object(
  623. commands._AppHandler,
  624. '_latest_compatible_package_version',
  625. _mock_latest)
  626. assert install_extension(self.mock_extension) is True
  627. assert install_extension(self.mock_mimeextension) is True
  628. with p1, p2:
  629. assert update_extension(self.pkg_names['extension']) is True
  630. assert installed == [self.pkg_names['extension']]
  631. def test_update_missing_extension(self):
  632. assert False == update_extension('foo')
  633. def test_update_multiple(self):
  634. installed = []
  635. def _mock_install(self, name, *args, **kwargs):
  636. installed.append(name[0] + name[1:].split('@')[0])
  637. return dict(name=name, is_dir=False, path='foo/bar/' + name)
  638. def _mock_latest(self, name):
  639. return '10000.0.0'
  640. p1 = patch.object(
  641. commands._AppHandler,
  642. '_install_extension',
  643. _mock_install)
  644. p2 = patch.object(
  645. commands._AppHandler,
  646. '_latest_compatible_package_version',
  647. _mock_latest)
  648. install_extension(self.mock_extension)
  649. install_extension(self.mock_mimeextension)
  650. with p1, p2:
  651. assert update_extension(self.pkg_names['extension']) is True
  652. assert update_extension(self.pkg_names['mimeextension']) is True
  653. assert installed == [self.pkg_names['extension'], self.pkg_names['mimeextension']]
  654. def test_update_all(self):
  655. updated = []
  656. def _mock_update(self, name, *args, **kwargs):
  657. updated.append(name[0] + name[1:].split('@')[0])
  658. return True
  659. original_app_info = commands._AppHandler._get_app_info
  660. def _mock_app_info(self):
  661. info = original_app_info(self)
  662. info['local_extensions'] = []
  663. return info
  664. assert install_extension(self.mock_extension) is True
  665. assert install_extension(self.mock_mimeextension) is True
  666. p1 = patch.object(
  667. commands._AppHandler,
  668. '_update_extension',
  669. _mock_update)
  670. # local packages are not updated, so mock them as non-local:
  671. p2 = patch.object(
  672. commands._AppHandler,
  673. '_get_app_info',
  674. _mock_app_info
  675. )
  676. with p1, p2:
  677. assert update_extension(None, all_=True) is True
  678. assert sorted(updated) == [self.pkg_names['extension'], self.pkg_names['mimeextension']]
  679. def test_load_extension(jp_serverapp, make_lab_app):
  680. app = make_lab_app()
  681. stderr = sys.stderr
  682. # sys.stderr = self.devnull
  683. app._link_jupyter_server_extension(jp_serverapp)
  684. app.initialize()
  685. sys.stderr = stderr