test_bundlerextension.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Test the bundlerextension CLI."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import os
  5. import shutil
  6. import unittest
  7. try:
  8. from unittest.mock import patch
  9. except ImportError:
  10. from mock import patch # py2
  11. from ipython_genutils.tempdir import TemporaryDirectory
  12. from ipython_genutils import py3compat
  13. from traitlets.tests.utils import check_help_all_output
  14. import notebook.nbextensions as nbextensions
  15. from notebook.config_manager import BaseJSONConfigManager
  16. from ..bundlerextensions import (_get_config_dir, enable_bundler_python,
  17. disable_bundler_python)
  18. def test_help_output():
  19. check_help_all_output('notebook.bundler.bundlerextensions')
  20. check_help_all_output('notebook.bundler.bundlerextensions', ['enable'])
  21. check_help_all_output('notebook.bundler.bundlerextensions', ['disable'])
  22. class TestBundlerExtensionCLI(unittest.TestCase):
  23. """Tests the bundlerextension CLI against the example zip_bundler."""
  24. def setUp(self):
  25. """Build an isolated config environment."""
  26. td = TemporaryDirectory()
  27. self.test_dir = py3compat.cast_unicode(td.name)
  28. self.data_dir = os.path.join(self.test_dir, 'data')
  29. self.config_dir = os.path.join(self.test_dir, 'config')
  30. self.system_data_dir = os.path.join(self.test_dir, 'system_data')
  31. self.system_path = [self.system_data_dir]
  32. # Use temp directory, not real user or system config paths
  33. self.patch_env = patch.dict('os.environ', {
  34. 'JUPYTER_CONFIG_DIR': self.config_dir,
  35. 'JUPYTER_DATA_DIR': self.data_dir,
  36. })
  37. self.patch_env.start()
  38. self.patch_system_path = patch.object(nbextensions,
  39. 'SYSTEM_JUPYTER_PATH', self.system_path)
  40. self.patch_system_path.start()
  41. def tearDown(self):
  42. """Remove the test config environment."""
  43. shutil.rmtree(self.test_dir, ignore_errors=True)
  44. self.patch_env.stop()
  45. self.patch_system_path.stop()
  46. def test_enable(self):
  47. """Should add the bundler to the notebook configuration."""
  48. enable_bundler_python('notebook.bundler.zip_bundler')
  49. config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
  50. cm = BaseJSONConfigManager(config_dir=config_dir)
  51. bundlers = cm.get('notebook').get('bundlerextensions', {})
  52. self.assertEqual(len(bundlers), 1)
  53. self.assertIn('notebook_zip_download', bundlers)
  54. def test_disable(self):
  55. """Should remove the bundler from the notebook configuration."""
  56. self.test_enable()
  57. disable_bundler_python('notebook.bundler.zip_bundler')
  58. config_dir = os.path.join(_get_config_dir(user=True), 'nbconfig')
  59. cm = BaseJSONConfigManager(config_dir=config_dir)
  60. bundlers = cm.get('notebook').get('bundlerextensions', {})
  61. self.assertEqual(len(bundlers), 0)