PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/python/pants_test/backend/docgen/tasks/test_markdown_to_html.py

https://gitlab.com/Ivy001/pants
Python | 186 lines | 177 code | 6 blank | 3 comment | 0 complexity | 2deecd8fd33e03059adcd49714c74d44 MD5 | raw file
  1. # coding=utf-8
  2. # Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
  3. # Licensed under the Apache License, Version 2.0 (see LICENSE).
  4. from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
  5. unicode_literals, with_statement)
  6. import os
  7. import unittest
  8. from textwrap import dedent
  9. import bs4
  10. import mock
  11. from pants.backend.docgen.targets.doc import Page
  12. from pants.backend.docgen.tasks import markdown_to_html_utils
  13. from pants.backend.docgen.tasks.markdown_to_html import MarkdownToHtml
  14. from pants.base.exceptions import TaskError
  15. from pants_test.tasks.task_test_base import TaskTestBase
  16. ABC = """able
  17. baker
  18. charlie"""
  19. class ChooseLinesTest(unittest.TestCase):
  20. def test_include_no_params(self):
  21. self.assertEquals(
  22. markdown_to_html_utils.choose_include_text(ABC, '', 'fake.md'),
  23. '\n'.join(['able', 'baker', 'charlie']))
  24. def test_include_start_at(self):
  25. self.assertEquals(
  26. markdown_to_html_utils.choose_include_text(ABC, 'start-at=abl', 'fake.md'),
  27. '\n'.join(['able', 'baker', 'charlie']))
  28. self.assertEquals(
  29. markdown_to_html_utils.choose_include_text(ABC, 'start-at=bak', 'fake.md'),
  30. '\n'.join(['baker', 'charlie']))
  31. self.assertEquals(
  32. markdown_to_html_utils.choose_include_text(ABC, 'start-at=xxx', 'fake.md'),
  33. '')
  34. def test_include_start_after(self):
  35. self.assertEquals(
  36. markdown_to_html_utils.choose_include_text(ABC, 'start-after=bak', 'fake.md'),
  37. 'charlie')
  38. self.assertEquals(
  39. markdown_to_html_utils.choose_include_text(ABC, 'start-after=cha', 'fake.md'),
  40. '')
  41. self.assertEquals(
  42. markdown_to_html_utils.choose_include_text(ABC, 'start-after=xxx', 'fake.md'),
  43. '')
  44. def test_include_end_at(self):
  45. self.assertEquals(
  46. markdown_to_html_utils.choose_include_text(ABC, 'end-at=abl', 'fake.md'),
  47. 'able')
  48. self.assertEquals(
  49. markdown_to_html_utils.choose_include_text(ABC, 'end-at=bak', 'fake.md'),
  50. '\n'.join(['able', 'baker']))
  51. self.assertEquals(
  52. markdown_to_html_utils.choose_include_text(ABC, 'end-at=xxx', 'fake.md'),
  53. '')
  54. def test_include_end_before(self):
  55. self.assertEquals(
  56. markdown_to_html_utils.choose_include_text(ABC, 'end-before=abl', 'fake.md'),
  57. '')
  58. self.assertEquals(
  59. markdown_to_html_utils.choose_include_text(ABC, 'end-before=xxx', 'fake.md'),
  60. '')
  61. self.assertEquals(
  62. markdown_to_html_utils.choose_include_text(ABC, 'end-before=bak', 'fake.md'),
  63. 'able')
  64. def test_include_start_at_end_at(self):
  65. self.assertEquals(
  66. markdown_to_html_utils.choose_include_text(ABC, 'start-at=abl&end-at=abl', 'fake.md'),
  67. 'able')
  68. self.assertEquals(
  69. markdown_to_html_utils.choose_include_text(ABC, 'start-at=cha&end-at=cha', 'fake.md'),
  70. 'charlie')
  71. self.assertEquals(
  72. markdown_to_html_utils.choose_include_text(ABC, 'start-at=abl&end-at=bak', 'fake.md'),
  73. '\n'.join(['able', 'baker']))
  74. self.assertEquals(
  75. markdown_to_html_utils.choose_include_text(ABC, 'start-at=bak&end-at=abl', 'fake.md'),
  76. '')
  77. class MarkdownToHtmlTest(TaskTestBase):
  78. @classmethod
  79. def task_type(cls):
  80. return MarkdownToHtml
  81. def test_rst_render_empty(self):
  82. self.create_file('empty.rst')
  83. empty_rst = self.make_target(':empty_rst', target_type=Page, source='empty.rst')
  84. task = self.create_task(self.context(target_roots=[empty_rst]))
  85. task.execute()
  86. def test_rst_render_failure_fail(self):
  87. self.create_file('bad.rst', contents=dedent("""
  88. A bad link:
  89. * `RB #2363 https://rbcommons.com/s/twitter/r/2363/>`_
  90. """))
  91. bad_rst = self.make_target(':bad_rst', target_type=Page, source='bad.rst')
  92. task = self.create_task(self.context(target_roots=[bad_rst]))
  93. with self.assertRaises(TaskError):
  94. task.execute()
  95. def get_rendered_page(self, context, page, rendered_basename):
  96. pages = context.products.get('markdown_html').get(page)
  97. self.assertIsNotNone(pages)
  98. pages_by_name = {os.path.basename(f): os.path.join(outdir, f)
  99. for outdir, files in pages.items()
  100. for f in files}
  101. self.assertIn(rendered_basename, pages_by_name)
  102. return pages_by_name.get(rendered_basename)
  103. def test_rst_render_failure_warn(self):
  104. self.create_file('bad.rst', contents=dedent("""
  105. A bad link:
  106. * `RB #2363 https://rbcommons.com/s/twitter/r/2363/>`_
  107. """))
  108. bad_rst = self.make_target(':bad_rst', target_type=Page, source='bad.rst')
  109. self.set_options(ignore_failure=True)
  110. context = self.context(target_roots=[bad_rst])
  111. context.log.warn = mock.Mock()
  112. task = self.create_task(context)
  113. task.execute()
  114. # The render error should have been logged.
  115. self.assertEqual(1, context.log.warn.call_count)
  116. args, kwargs = context.log.warn.call_args
  117. self.assertEqual(0, len(kwargs))
  118. self.assertEqual(1, len(args))
  119. self.assertIn('bad.rst', args[0])
  120. # But we still should have gotten (badly) rendered content.
  121. with open(self.get_rendered_page(context, bad_rst, 'bad.html')) as fp:
  122. html = fp.read()
  123. self.assertIn('A bad link:', html)
  124. def test_rst_render_success(self):
  125. self.create_file('good.rst', contents=dedent("""
  126. A good link:
  127. * `RB #2363 <https://rbcommons.com/s/twitter/r/2363/>`_
  128. """))
  129. good_rst = self.make_target(':good_rst', target_type=Page, source='good.rst')
  130. context = self.context(target_roots=[good_rst])
  131. task = self.create_task(context)
  132. task.execute()
  133. with open(self.get_rendered_page(context, good_rst, 'good.html')) as fp:
  134. html = fp.read()
  135. soup = bs4.BeautifulSoup(markup=html)
  136. self.assertIsNotNone(soup.find(text='A good link:'))
  137. unordered_list = soup.find(name='ul')
  138. self.assertIsNotNone(unordered_list)
  139. list_item = unordered_list.find('li')
  140. self.assertIsNotNone(list_item)
  141. anchor = list_item.find('a',
  142. attrs={'href': 'https://rbcommons.com/s/twitter/r/2363/'},
  143. text='RB #2363')
  144. self.assertIsNotNone(anchor)