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

/pants-plugins/tests/python/internal_backend_test/sitegen/test_sitegen.py

https://gitlab.com/Ivy001/pants
Python | 213 lines | 203 code | 7 blank | 3 comment | 0 complexity | 69ecd3c96e89a2b015e05e4c2b36beaa 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 json
  7. import unittest
  8. import bs4
  9. from internal_backend.sitegen.tasks import sitegen
  10. CONFIG_JSON = """
  11. {
  12. "sources": {
  13. "index": "fake0/README.html",
  14. "subdir/page1": "fake1/p1.html",
  15. "subdir/page2": "fake1/p2.html",
  16. "subdir/page2_no_toc": "fake1/p2.html"
  17. },
  18. "show_toc": {
  19. "subdir/page2_no_toc": false
  20. },
  21. "extras": {
  22. },
  23. "tree": [
  24. { "page": "index",
  25. "children": [
  26. { "page": "subdir/page1" },
  27. { "page": "subdir/page2" }
  28. ]
  29. }
  30. ],
  31. "template": "fake/fake.mustache"
  32. }
  33. """
  34. INDEX_HTML = """
  35. <h1 id="pants-build-system">Pants Build System</h1>
  36. <p>Pants is a build system.</p>
  37. <a pantsmark="pantsmark_index"></a>
  38. <p>See also:
  39. <a href="../fake1/p1.html">another page</a>.</p>
  40. """
  41. P1_HTML = """
  42. <h1>東京 is Tokyo</h1>
  43. <a id="an_pantsmark" pantsmark="pantsmark_p1"></a>
  44. <p>Fascinating description. <a pantsref="pantsmark_index">to index</a>
  45. """
  46. P2_HTML = """
  47. <head>
  48. <title>Page 2: Electric Boogaloo</title>
  49. </head>
  50. <body>
  51. <h1>Page 2</h1>
  52. <p>Some text <a pantsref="pantsmark_p1">to p1</a></p>
  53. <h2 id="one">Section One</h2>
  54. <p>Some more text</p>
  55. <h2 id="two">Section Two</h2>
  56. <p>Some more text</p>
  57. </body>
  58. """
  59. TEMPLATE_MUSTACHE = """
  60. {{{body_html}}}
  61. """
  62. class AllTheThingsTestCase(unittest.TestCase):
  63. def setUp(self):
  64. self.config = json.loads(CONFIG_JSON)
  65. self.soups = {
  66. 'index': bs4.BeautifulSoup(INDEX_HTML),
  67. 'subdir/page1': bs4.BeautifulSoup(P1_HTML),
  68. 'subdir/page2': bs4.BeautifulSoup(P2_HTML),
  69. 'subdir/page2_no_toc': bs4.BeautifulSoup(P2_HTML),
  70. }
  71. self.precomputed = sitegen.precompute(self.config, self.soups)
  72. def test_fixup_internal_links(self):
  73. sitegen.fixup_internal_links(self.config, self.soups)
  74. html = sitegen.render_html('index',
  75. self.config,
  76. self.soups,
  77. self.precomputed,
  78. TEMPLATE_MUSTACHE)
  79. self.assertIn('subdir/page1.html', html,
  80. 'p1.html link did not get fixed up to page1.html')
  81. def test_pantsrefs(self):
  82. sitegen.link_pantsrefs(self.soups, self.precomputed)
  83. p1_html = sitegen.render_html('subdir/page1',
  84. self.config,
  85. self.soups,
  86. self.precomputed,
  87. TEMPLATE_MUSTACHE)
  88. self.assertIn('href="../index.html#pantsmark_index"', p1_html,
  89. 'pantsref_index did not get linked')
  90. p2_html = sitegen.render_html('subdir/page2',
  91. self.config,
  92. self.soups,
  93. self.precomputed,
  94. TEMPLATE_MUSTACHE)
  95. self.assertIn('href="page1.html#an_pantsmark"', p2_html,
  96. 'pantsref_p1 did not get linked')
  97. def test_find_title(self):
  98. p2_html = sitegen.render_html('subdir/page2',
  99. self.config,
  100. self.soups,
  101. self.precomputed,
  102. '{{title}}')
  103. self.assertEqual(p2_html, 'Page 2: Electric Boogaloo',
  104. """Didn't find correct title""")
  105. # ascii worked? great, try non-ASCII
  106. p1_html = sitegen.render_html('subdir/page1',
  107. self.config,
  108. self.soups,
  109. self.precomputed,
  110. '{{title}}')
  111. self.assertEqual(p1_html, u'東京 is Tokyo',
  112. """Didn't find correct non-ASCII title""")
  113. def test_page_toc(self):
  114. # One of our "pages" has a couple of basic headings.
  115. # Do we get the correct info from that to generate
  116. # a page-level table of contents?
  117. sitegen.generate_page_tocs(self.soups, self.precomputed)
  118. rendered = sitegen.render_html('subdir/page2',
  119. self.config,
  120. self.soups,
  121. self.precomputed,
  122. """
  123. {{#page_toc}}
  124. DEPTH={{depth}} LINK={{link}} TEXT={{text}}
  125. {{/page_toc}}
  126. """)
  127. self.assertIn('DEPTH=1 LINK=one TEXT=Section One', rendered)
  128. self.assertIn('DEPTH=1 LINK=two TEXT=Section Two', rendered)
  129. def test_no_show_toc(self):
  130. sitegen.generate_page_tocs(self.soups, self.precomputed)
  131. rendered = sitegen.render_html('subdir/page2_no_toc',
  132. self.config,
  133. self.soups,
  134. self.precomputed,
  135. """
  136. {{#page_toc}}
  137. DEPTH={{depth}} LINK={{link}} TEXT={{text}}
  138. {{/page_toc}}
  139. """)
  140. self.assertNotIn('DEPTH=1 LINK=one TEXT=Section One', rendered)
  141. self.assertNotIn('DEPTH=1 LINK=two TEXT=Section Two', rendered)
  142. def test_transforms_not_discard_page_tocs(self):
  143. # We had a bug where one step of transform lost the info
  144. # we need to build page-tocs. Make sure that doesn't happen again.
  145. sitegen.transform_soups(self.config, self.soups, self.precomputed)
  146. rendered = sitegen.render_html('subdir/page2',
  147. self.config,
  148. self.soups,
  149. self.precomputed,
  150. """
  151. {{#page_toc}}
  152. DEPTH={{depth}} LINK={{link}} TEXT={{text}}
  153. {{/page_toc}}
  154. """)
  155. self.assertIn('DEPTH=1 LINK=one TEXT=Section One', rendered)
  156. self.assertIn('DEPTH=1 LINK=two TEXT=Section Two', rendered)
  157. def test_site_toc(self):
  158. # Our "site" has a simple outline.
  159. # Do we get the correct info from that to generate
  160. # a site-level table of contents?
  161. rendered = sitegen.render_html('index',
  162. self.config,
  163. self.soups,
  164. self.precomputed,
  165. """
  166. {{#site_toc}}
  167. DEPTH={{depth}} LINK={{link}} TEXT={{text}}
  168. {{/site_toc}}
  169. """)
  170. self.assertIn(u'DEPTH=1 LINK=subdir/page1.html TEXT=東京 is Tokyo', rendered)
  171. self.assertIn('DEPTH=1 LINK=subdir/page2.html TEXT=Page 2: Electric Boogaloo', rendered)
  172. def test_transform_fixes_up_internal_links(self):
  173. sitegen.transform_soups(self.config, self.soups, self.precomputed)
  174. html = sitegen.render_html('index',
  175. self.config,
  176. self.soups,
  177. self.precomputed,
  178. TEMPLATE_MUSTACHE)
  179. self.assertTrue('subdir/page1.html' in html,
  180. 'p1.html link did not get fixed up to page1.html')