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.
 
 
 

270 lines
7.8 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.generator import Generator
  8. from hyde.model import Config
  9. from hyde.site import Site
  10. from pyquery import PyQuery
  11. from fswrap import File, Folder
  12. TEST_SITE = File(__file__).parent.child_folder('_test')
  13. class TestGenerator(object):
  14. def setUp(self):
  15. TEST_SITE.make()
  16. TEST_SITE.parent.child_folder(
  17. 'sites/test_jinja').copy_contents_to(TEST_SITE)
  18. def tearDown(self):
  19. TEST_SITE.delete()
  20. def test_generate_resource_from_path(self):
  21. site = Site(TEST_SITE)
  22. site.load()
  23. gen = Generator(site)
  24. gen.generate_resource_at_path(TEST_SITE.child('content/about.html'))
  25. about = File(Folder(site.config.deploy_root_path).child('about.html'))
  26. assert about.exists
  27. text = about.read_all()
  28. q = PyQuery(text)
  29. assert about.name in q("div#main").text()
  30. def test_generate_resource_from_path_with_is_processable_false(self):
  31. site = Site(TEST_SITE)
  32. site.load()
  33. resource = site.content.resource_from_path(
  34. TEST_SITE.child('content/about.html'))
  35. resource.is_processable = False
  36. gen = Generator(site)
  37. gen.generate_resource_at_path(TEST_SITE.child('content/about.html'))
  38. about = File(Folder(site.config.deploy_root_path).child('about.html'))
  39. assert not about.exists
  40. def test_generate_resource_from_path_with_uses_template_false(self):
  41. site = Site(TEST_SITE)
  42. site.load()
  43. resource = site.content.resource_from_path(
  44. TEST_SITE.child('content/about.html'))
  45. resource.uses_template = False
  46. gen = Generator(site)
  47. gen.generate_resource_at_path(TEST_SITE.child('content/about.html'))
  48. about = File(Folder(site.config.deploy_root_path).child('about.html'))
  49. assert about.exists
  50. text = about.read_all()
  51. expected = resource.source_file.read_all()
  52. assert text == expected
  53. def test_generate_resource_from_path_with_deploy_override(self):
  54. site = Site(TEST_SITE)
  55. site.load()
  56. resource = site.content.resource_from_path(
  57. TEST_SITE.child('content/about.html'))
  58. resource.relative_deploy_path = 'about/index.html'
  59. gen = Generator(site)
  60. gen.generate_resource_at_path(TEST_SITE.child('content/about.html'))
  61. about = File(
  62. Folder(site.config.deploy_root_path).child('about/index.html'))
  63. assert about.exists
  64. text = about.read_all()
  65. q = PyQuery(text)
  66. assert resource.name in q("div#main").text()
  67. def test_has_resource_changed(self):
  68. site = Site(TEST_SITE)
  69. site.load()
  70. resource = site.content.resource_from_path(
  71. TEST_SITE.child('content/about.html'))
  72. gen = Generator(site)
  73. gen.generate_all()
  74. import time
  75. time.sleep(1)
  76. assert not gen.has_resource_changed(resource)
  77. text = resource.source_file.read_all()
  78. resource.source_file.write(text)
  79. assert gen.has_resource_changed(resource)
  80. gen.generate_all()
  81. assert not gen.has_resource_changed(resource)
  82. time.sleep(1)
  83. l = File(TEST_SITE.child('layout/root.html'))
  84. l.write(l.read_all())
  85. assert gen.has_resource_changed(resource)
  86. def test_context(self):
  87. site = Site(TEST_SITE, Config(TEST_SITE, config_dict={
  88. "context": {
  89. "data": {
  90. "abc": "def"
  91. }
  92. }
  93. }))
  94. text = """
  95. {% extends "base.html" %}
  96. {% block main %}
  97. abc = {{ abc }}
  98. Hi!
  99. I am a test template to make sure jinja2 generation works well with hyde.
  100. {{resource.name}}
  101. {% endblock %}
  102. """
  103. site.load()
  104. resource = site.content.resource_from_path(
  105. TEST_SITE.child('content/about.html'))
  106. gen = Generator(site)
  107. resource.source_file.write(text)
  108. gen.generate_all()
  109. target = File(site.config.deploy_root_path.child(resource.name))
  110. assert "abc = def" in target.read_all()
  111. def test_context_providers(self):
  112. site = Site(TEST_SITE, Config(TEST_SITE, config_dict={
  113. "context": {
  114. "data": {
  115. "abc": "def"
  116. },
  117. "providers": {
  118. "nav": "nav.yaml"
  119. }
  120. }
  121. }))
  122. nav = """
  123. - home
  124. - articles
  125. - projects
  126. """
  127. text = """
  128. {% extends "base.html" %}
  129. {% block main %}
  130. {{nav}}
  131. {% for item in nav %}
  132. {{item}}
  133. {% endfor %}
  134. abc = {{ abc }}
  135. Hi!
  136. I am a test template to make sure jinja2 generation works well with hyde.
  137. {{resource.name}}
  138. {% endblock %}
  139. """
  140. File(TEST_SITE.child('nav.yaml')).write(nav)
  141. site.load()
  142. resource = site.content.resource_from_path(
  143. TEST_SITE.child('content/about.html'))
  144. gen = Generator(site)
  145. resource.source_file.write(text)
  146. gen.generate_all()
  147. target = File(site.config.deploy_root_path.child(resource.name))
  148. out = target.read_all()
  149. assert "abc = def" in out
  150. assert "home" in out
  151. assert "articles" in out
  152. assert "projects" in out
  153. def test_context_providers_no_data(self):
  154. site = Site(TEST_SITE, Config(TEST_SITE, config_dict={
  155. "context": {
  156. "providers": {
  157. "nav": "nav.yaml"
  158. }
  159. }
  160. }))
  161. nav = """
  162. main:
  163. - home
  164. - articles
  165. - projects
  166. """
  167. text = """
  168. {% extends "base.html" %}
  169. {% block main %}
  170. {{nav}}
  171. {% for item in nav.main %}
  172. {{item}}
  173. {% endfor %}
  174. abc = {{ abc }}
  175. Hi!
  176. I am a test template to make sure jinja2 generation works well with hyde.
  177. {{resource.name}}
  178. {% endblock %}
  179. """
  180. File(TEST_SITE.child('nav.yaml')).write(nav)
  181. site.load()
  182. resource = site.content.resource_from_path(
  183. TEST_SITE.child('content/about.html'))
  184. gen = Generator(site)
  185. resource.source_file.write(text)
  186. gen.generate_all()
  187. target = File(site.config.deploy_root_path.child(resource.name))
  188. out = target.read_all()
  189. assert "home" in out
  190. assert "articles" in out
  191. assert "projects" in out
  192. def test_context_providers_equivalence(self):
  193. import yaml
  194. events = """
  195. 2011:
  196. -
  197. title: "one event"
  198. location: "a city"
  199. -
  200. title: "one event"
  201. location: "a city"
  202. 2010:
  203. -
  204. title: "one event"
  205. location: "a city"
  206. -
  207. title: "one event"
  208. location: "a city"
  209. """
  210. events_dict = yaml.load(events)
  211. config_dict = dict(context=dict(
  212. data=dict(events1=events_dict),
  213. providers=dict(events2="events.yaml")
  214. ))
  215. text = """
  216. {%% extends "base.html" %%}
  217. {%% block main %%}
  218. <ul>
  219. {%% for year, eventlist in %s %%}
  220. <li>
  221. <h1>{{ year }}</h1>
  222. <ul>
  223. {%% for event in eventlist %%}
  224. <li>{{ event.title }}-{{ event.location }}</li>
  225. {%% endfor %%}
  226. </ul>
  227. </li>
  228. {%% endfor %%}
  229. </ul>
  230. {%% endblock %%}
  231. """
  232. File(TEST_SITE.child('events.yaml')).write(events)
  233. f1 = File(TEST_SITE.child('content/text1.html'))
  234. f2 = File(TEST_SITE.child('content/text2.html'))
  235. f1.write(text % "events1")
  236. f2.write(text % "events2")
  237. site = Site(TEST_SITE, Config(TEST_SITE, config_dict=config_dict))
  238. site.load()
  239. gen = Generator(site)
  240. gen.generate_all()
  241. left = File(site.config.deploy_root_path.child(f1.name)).read_all()
  242. right = File(site.config.deploy_root_path.child(f2.name)).read_all()
  243. assert left == right