A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

308 lines
13 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.exceptions import HydeException
  8. from hyde.fs import File, Folder
  9. from hyde.generator import Generator
  10. from hyde.plugin import Plugin
  11. from hyde.site import Site
  12. from mock import patch
  13. from nose.tools import raises, nottest, with_setup
  14. TEST_SITE = File(__file__).parent.child_folder('_test')
  15. class PluginLoaderStub(Plugin):
  16. pass
  17. class TestPlugins(object):
  18. @classmethod
  19. def setup_class(cls):
  20. TEST_SITE.make()
  21. TEST_SITE.parent.child_folder('sites/test_jinja').copy_contents_to(TEST_SITE)
  22. folders = []
  23. text_files = []
  24. binary_files = []
  25. with TEST_SITE.child_folder('content').walker as walker:
  26. @walker.folder_visitor
  27. def visit_folder(folder):
  28. folders.append(folder.path)
  29. @walker.file_visitor
  30. def visit_file(afile):
  31. if not afile.is_text:
  32. binary_files.append(afile.path)
  33. else:
  34. text_files.append(afile.path)
  35. cls.content_nodes = sorted(folders)
  36. cls.content_text_resources = sorted(text_files)
  37. cls.content_binary_resources = sorted(binary_files)
  38. @classmethod
  39. def teardown_class(cls):
  40. TEST_SITE.delete()
  41. def setUp(self):
  42. self.site = Site(TEST_SITE)
  43. self.site.config.plugins = ['hyde.tests.test_plugin.PluginLoaderStub']
  44. def test_can_load_plugin_modules(self):
  45. assert not len(self.site.plugins)
  46. Plugin.load_all(self.site)
  47. assert len(self.site.plugins) == 1
  48. assert self.site.plugins[0].__class__.__name__ == 'PluginLoaderStub'
  49. def test_generator_loads_plugins(self):
  50. gen = Generator(self.site)
  51. assert len(self.site.plugins) == 1
  52. def test_generator_template_registered_called(self):
  53. with patch.object(PluginLoaderStub, 'template_loaded') as template_loaded_stub:
  54. gen = Generator(self.site)
  55. gen.generate_all()
  56. assert template_loaded_stub.call_count == 1
  57. def test_generator_template_begin_generation_called(self):
  58. with patch.object(PluginLoaderStub, 'begin_generation') as begin_generation_stub:
  59. gen = Generator(self.site)
  60. gen.generate_all()
  61. assert begin_generation_stub.call_count == 1
  62. def test_generator_template_begin_generation_called_for_single_resource(self):
  63. with patch.object(PluginLoaderStub, 'begin_generation') as begin_generation_stub:
  64. gen = Generator(self.site)
  65. path = self.site.content.source_folder.child('about.html')
  66. gen.generate_resource_at_path(path)
  67. assert begin_generation_stub.call_count == 1
  68. def test_generator_template_begin_generation_called_for_single_node(self):
  69. with patch.object(PluginLoaderStub, 'begin_generation') as begin_generation_stub:
  70. gen = Generator(self.site)
  71. path = self.site.content.source_folder
  72. gen.generate_node_at_path(path)
  73. assert begin_generation_stub.call_count == 1
  74. def test_generator_template_generation_complete_called(self):
  75. with patch.object(PluginLoaderStub, 'generation_complete') as generation_complete_stub:
  76. gen = Generator(self.site)
  77. gen.generate_all()
  78. assert generation_complete_stub.call_count == 1
  79. def test_generator_template_generation_complete_called_for_single_resource(self):
  80. with patch.object(PluginLoaderStub, 'generation_complete') as generation_complete_stub:
  81. gen = Generator(self.site)
  82. path = self.site.content.source_folder.child('about.html')
  83. gen.generate_resource_at_path(path)
  84. assert generation_complete_stub.call_count == 1
  85. def test_generator_template_generation_complete_called_for_single_node(self):
  86. with patch.object(PluginLoaderStub, 'generation_complete') as generation_complete_stub:
  87. gen = Generator(self.site)
  88. path = self.site.content.source_folder
  89. gen.generate_node_at_path(path)
  90. assert generation_complete_stub.call_count == 1
  91. def test_generator_template_begin_site_called(self):
  92. with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
  93. gen = Generator(self.site)
  94. gen.generate_all()
  95. assert begin_site_stub.call_count == 1
  96. def test_generator_template_begin_site_called_for_single_resource(self):
  97. with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
  98. gen = Generator(self.site)
  99. path = self.site.content.source_folder.child('about.html')
  100. gen.generate_resource_at_path(path)
  101. assert begin_site_stub.call_count == 1
  102. def test_generator_template_begin_site_not_called_for_single_resource_second_time(self):
  103. with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
  104. gen = Generator(self.site)
  105. gen.generate_all()
  106. assert begin_site_stub.call_count == 1
  107. path = self.site.content.source_folder.child('about.html')
  108. gen.generate_resource_at_path(path)
  109. assert begin_site_stub.call_count == 1
  110. def test_generator_template_begin_site_called_for_single_node(self):
  111. with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
  112. gen = Generator(self.site)
  113. path = self.site.content.source_folder
  114. gen.generate_node_at_path(path)
  115. assert begin_site_stub.call_count == 1
  116. def test_generator_template_begin_site_not_called_for_single_node_second_time(self):
  117. with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub:
  118. gen = Generator(self.site)
  119. gen.generate_all()
  120. assert begin_site_stub.call_count == 1
  121. path = self.site.content.source_folder
  122. gen.generate_node_at_path(path)
  123. assert begin_site_stub.call_count == 1
  124. def test_generator_template_site_complete_called(self):
  125. with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
  126. gen = Generator(self.site)
  127. gen.generate_all()
  128. assert site_complete_stub.call_count == 1
  129. def test_generator_template_site_complete_called_for_single_resource(self):
  130. with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
  131. gen = Generator(self.site)
  132. path = self.site.content.source_folder.child('about.html')
  133. gen.generate_resource_at_path(path)
  134. assert site_complete_stub.call_count == 1
  135. def test_generator_template_site_complete_not_called_for_single_resource_second_time(self):
  136. with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
  137. gen = Generator(self.site)
  138. gen.generate_all()
  139. assert site_complete_stub.call_count == 1
  140. path = self.site.content.source_folder.child('about.html')
  141. gen.generate_resource_at_path(path)
  142. assert site_complete_stub.call_count == 1
  143. def test_generator_template_site_complete_called_for_single_node(self):
  144. with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
  145. gen = Generator(self.site)
  146. path = self.site.content.source_folder
  147. gen.generate_node_at_path(path)
  148. assert site_complete_stub.call_count == 1
  149. def test_generator_template_site_complete_not_called_for_single_node_second_time(self):
  150. with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub:
  151. gen = Generator(self.site)
  152. gen.generate_all()
  153. path = self.site.content.source_folder
  154. gen.generate_node_at_path(path)
  155. assert site_complete_stub.call_count == 1
  156. def test_generator_template_begin_node_called(self):
  157. with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub:
  158. gen = Generator(self.site)
  159. gen.generate_all()
  160. assert begin_node_stub.call_count == len(self.content_nodes)
  161. called_with_nodes = sorted([arg[0][0].path for arg in begin_node_stub.call_args_list])
  162. assert called_with_nodes == self.content_nodes
  163. def test_generator_template_begin_node_called_for_single_resource(self):
  164. with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub:
  165. gen = Generator(self.site)
  166. gen.generate_resource_at_path(self.site.content.source_folder.child('about.html'))
  167. assert begin_node_stub.call_count == len(self.content_nodes)
  168. def test_generator_template_begin_node_not_called_for_single_resource_second_time(self):
  169. with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub:
  170. gen = Generator(self.site)
  171. gen.generate_all()
  172. assert begin_node_stub.call_count == len(self.content_nodes)
  173. gen.generate_resource_at_path(self.site.content.source_folder.child('about.html'))
  174. assert begin_node_stub.call_count == len(self.content_nodes) # No extra calls
  175. def test_generator_template_node_complete_called(self):
  176. with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub:
  177. gen = Generator(self.site)
  178. gen.generate_all()
  179. assert node_complete_stub.call_count == len(self.content_nodes)
  180. called_with_nodes = sorted([arg[0][0].path for arg in node_complete_stub.call_args_list])
  181. assert called_with_nodes == self.content_nodes
  182. def test_generator_template_node_complete_called_for_single_resource(self):
  183. with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub:
  184. gen = Generator(self.site)
  185. gen.generate_resource_at_path(self.site.content.source_folder.child('about.html'))
  186. assert node_complete_stub.call_count == len(self.content_nodes)
  187. def test_generator_template_node_complete_not_called_for_single_resource_second_time(self):
  188. with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub:
  189. gen = Generator(self.site)
  190. gen.generate_all()
  191. assert node_complete_stub.call_count == len(self.content_nodes)
  192. gen.generate_resource_at_path(self.site.content.source_folder.child('about.html'))
  193. assert node_complete_stub.call_count == len(self.content_nodes) # No extra calls
  194. def test_generator_template_begin_text_resource_called(self):
  195. with patch.object(PluginLoaderStub, 'begin_text_resource') as begin_text_resource_stub:
  196. begin_text_resource_stub.return_value = ''
  197. gen = Generator(self.site)
  198. gen.generate_all()
  199. called_with_resources = sorted([arg[0][0].path for arg in begin_text_resource_stub.call_args_list])
  200. assert begin_text_resource_stub.call_count == len(self.content_text_resources)
  201. assert called_with_resources == self.content_text_resources
  202. def test_generator_template_begin_text_resource_called_for_single_resource(self):
  203. with patch.object(PluginLoaderStub, 'begin_text_resource') as begin_text_resource_stub:
  204. begin_text_resource_stub.return_value = ''
  205. gen = Generator(self.site)
  206. gen.generate_all()
  207. begin_text_resource_stub.reset_mock()
  208. path = self.site.content.source_folder.child('about.html')
  209. gen = Generator(self.site)
  210. gen.generate_resource_at_path(path, incremental=True)
  211. called_with_resources = sorted([arg[0][0].path for arg in begin_text_resource_stub.call_args_list])
  212. assert begin_text_resource_stub.call_count == 1
  213. assert called_with_resources[0] == path
  214. def test_generator_template_begin_binary_resource_called(self):
  215. with patch.object(PluginLoaderStub, 'begin_binary_resource') as begin_binary_resource_stub:
  216. gen = Generator(self.site)
  217. gen.generate_all()
  218. called_with_resources = sorted([arg[0][0].path for arg in begin_binary_resource_stub.call_args_list])
  219. assert begin_binary_resource_stub.call_count == len(self.content_binary_resources)
  220. assert called_with_resources == self.content_binary_resources
  221. def test_generator_template_begin_binary_resource_called_for_single_resource(self):
  222. with patch.object(PluginLoaderStub, 'begin_binary_resource') as begin_binary_resource_stub:
  223. gen = Generator(self.site)
  224. gen.generate_all()
  225. begin_binary_resource_stub.reset_mock()
  226. path = self.site.content.source_folder.child('favicon.ico')
  227. gen.generate_resource_at_path(path)
  228. called_with_resources = sorted([arg[0][0].path for arg in begin_binary_resource_stub.call_args_list])
  229. assert begin_binary_resource_stub.call_count == 1
  230. assert called_with_resources[0] == path