test_bundler_tools.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """Test the bundler tools."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import unittest
  5. import os
  6. import shutil
  7. import tempfile
  8. import notebook.bundler.tools as tools
  9. HERE = os.path.abspath(os.path.dirname(__file__))
  10. class TestBundlerTools(unittest.TestCase):
  11. def setUp(self):
  12. self.tmp = tempfile.mkdtemp()
  13. def tearDown(self):
  14. shutil.rmtree(self.tmp, ignore_errors=True)
  15. def test_get_no_cell_references(self):
  16. '''Should find no references in a regular HTML comment.'''
  17. no_references = tools.get_cell_reference_patterns({'source':'''!<--
  18. a
  19. b
  20. c
  21. -->''', 'cell_type':'markdown'})
  22. self.assertEqual(len(no_references), 0)
  23. def test_get_cell_reference_patterns_comment_multiline(self):
  24. '''Should find two references and ignore a comment within an HTML comment.'''
  25. cell = {'cell_type':'markdown', 'source':'''<!--associate:
  26. a
  27. b/
  28. #comment
  29. -->'''}
  30. references = tools.get_cell_reference_patterns(cell)
  31. self.assertTrue('a' in references and 'b/' in references, str(references))
  32. self.assertEqual(len(references), 2, str(references))
  33. def test_get_cell_reference_patterns_comment_trailing_filename(self):
  34. '''Should find three references within an HTML comment.'''
  35. cell = {'cell_type':'markdown', 'source':'''<!--associate:c
  36. a
  37. b/
  38. #comment
  39. -->'''}
  40. references = tools.get_cell_reference_patterns(cell)
  41. self.assertTrue('a' in references and 'b/' in references and 'c' in references, str(references))
  42. self.assertEqual(len(references), 3, str(references))
  43. def test_get_cell_reference_patterns_precode(self):
  44. '''Should find no references in a fenced code block in a *code* cell.'''
  45. self.assertTrue(tools.get_cell_reference_patterns)
  46. no_references = tools.get_cell_reference_patterns({'source':'''```
  47. foo
  48. bar
  49. baz
  50. ```
  51. ''', 'cell_type':'code'})
  52. self.assertEqual(len(no_references), 0)
  53. def test_get_cell_reference_patterns_precode_mdcomment(self):
  54. '''Should find two references and ignore a comment in a fenced code block.'''
  55. cell = {'cell_type':'markdown', 'source':'''```
  56. a
  57. b/
  58. #comment
  59. ```'''}
  60. references = tools.get_cell_reference_patterns(cell)
  61. self.assertTrue('a' in references and 'b/' in references, str(references))
  62. self.assertEqual(len(references), 2, str(references))
  63. def test_get_cell_reference_patterns_precode_backticks(self):
  64. '''Should find three references in a fenced code block.'''
  65. cell = {'cell_type':'markdown', 'source':'''```c
  66. a
  67. b/
  68. #comment
  69. ```'''}
  70. references = tools.get_cell_reference_patterns(cell)
  71. self.assertTrue('a' in references and 'b/' in references and 'c' in references, str(references))
  72. self.assertEqual(len(references), 3, str(references))
  73. def test_glob_dir(self):
  74. '''Should expand to single file in the resources/ subfolder.'''
  75. self.assertIn(os.path.join('resources', 'empty.ipynb'),
  76. tools.expand_references(HERE, ['resources/empty.ipynb']))
  77. def test_glob_subdir(self):
  78. '''Should expand to all files in the resources/ subfolder.'''
  79. self.assertIn(os.path.join('resources', 'empty.ipynb'),
  80. tools.expand_references(HERE, ['resources/']))
  81. def test_glob_splat(self):
  82. '''Should expand to all contents under this test/ directory.'''
  83. globs = tools.expand_references(HERE, ['*'])
  84. self.assertIn('test_bundler_tools.py', globs, globs)
  85. self.assertIn('resources', globs, globs)
  86. def test_glob_splatsplat_in_middle(self):
  87. '''Should expand to test_file.txt deep under this test/ directory.'''
  88. globs = tools.expand_references(HERE, ['resources/**/test_file.txt'])
  89. self.assertIn(os.path.join('resources', 'subdir', 'test_file.txt'), globs, globs)
  90. def test_glob_splatsplat_trailing(self):
  91. '''Should expand to all descendants of this test/ directory.'''
  92. globs = tools.expand_references(HERE, ['resources/**'])
  93. self.assertIn(os.path.join('resources', 'empty.ipynb'), globs, globs)
  94. self.assertIn(os.path.join('resources', 'subdir', 'test_file.txt'), globs, globs)
  95. def test_glob_splatsplat_leading(self):
  96. '''Should expand to test_file.txt under any path.'''
  97. globs = tools.expand_references(HERE, ['**/test_file.txt'])
  98. self.assertIn(os.path.join('resources', 'subdir', 'test_file.txt'), globs, globs)
  99. self.assertIn(os.path.join('resources', 'another_subdir', 'test_file.txt'), globs, globs)
  100. def test_copy_filelist(self):
  101. '''Should copy select files from source to destination'''
  102. globs = tools.expand_references(HERE, ['**/test_file.txt'])
  103. tools.copy_filelist(HERE, self.tmp, globs)
  104. self.assertTrue(os.path.isfile(os.path.join(self.tmp, 'resources', 'subdir', 'test_file.txt')))
  105. self.assertTrue(os.path.isfile(os.path.join(self.tmp, 'resources', 'another_subdir', 'test_file.txt')))
  106. self.assertFalse(os.path.isfile(os.path.join(self.tmp, 'resources', 'empty.ipynb')))