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.
 
 
 

317 lines
10 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. import yaml
  8. from hyde._compat import quote
  9. from hyde.model import Config
  10. from hyde.site import Node, RootNode, Site
  11. from fswrap import File, Folder
  12. TEST_SITE_ROOT = File(__file__).parent.child_folder('sites/test_jinja')
  13. def test_node_site():
  14. s = Site(TEST_SITE_ROOT)
  15. r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
  16. assert r.site == s
  17. n = Node(r.source_folder.child_folder('blog'), r)
  18. assert n.site == s
  19. def test_node_root():
  20. s = Site(TEST_SITE_ROOT)
  21. r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
  22. assert r.root == r
  23. n = Node(r.source_folder.child_folder('blog'), r)
  24. assert n.root == r
  25. def test_node_parent():
  26. s = Site(TEST_SITE_ROOT)
  27. r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
  28. c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
  29. assert c.parent == r.node_from_relative_path('blog/2010')
  30. def test_node_module():
  31. s = Site(TEST_SITE_ROOT)
  32. r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
  33. assert not r.module
  34. n = r.add_node(TEST_SITE_ROOT.child_folder('content/blog'))
  35. assert n.module == n
  36. c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
  37. assert c.module == n
  38. def test_node_url():
  39. s = Site(TEST_SITE_ROOT)
  40. r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
  41. assert not r.module
  42. n = r.add_node(TEST_SITE_ROOT.child_folder('content/blog'))
  43. assert n.url == '/' + n.relative_path
  44. assert n.url == '/blog'
  45. c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
  46. assert c.url == '/' + c.relative_path
  47. assert c.url == '/blog/2010/december'
  48. def test_node_full_url():
  49. s = Site(TEST_SITE_ROOT)
  50. s.config.base_url = 'http://localhost'
  51. r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
  52. assert not r.module
  53. n = r.add_node(TEST_SITE_ROOT.child_folder('content/blog'))
  54. assert n.full_url == 'http://localhost/blog'
  55. c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
  56. assert c.full_url == 'http://localhost/blog/2010/december'
  57. def test_node_full_url_quoted():
  58. s = Site(TEST_SITE_ROOT)
  59. s.config.base_url = 'http://localhost'
  60. r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
  61. assert not r.module
  62. n = r.add_node(TEST_SITE_ROOT.child_folder('content/blo~g'))
  63. assert n.full_url == 'http://localhost/' + quote('blo~g')
  64. c = r.add_node(TEST_SITE_ROOT.child_folder('content/blo~g/2010/december'))
  65. assert c.full_url == 'http://localhost/' + quote('blo~g/2010/december')
  66. def test_node_relative_path():
  67. s = Site(TEST_SITE_ROOT)
  68. r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
  69. assert not r.module
  70. n = r.add_node(TEST_SITE_ROOT.child_folder('content/blog'))
  71. assert n.relative_path == 'blog'
  72. c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
  73. assert c.relative_path == 'blog/2010/december'
  74. def test_load():
  75. s = Site(TEST_SITE_ROOT)
  76. s.load()
  77. path = 'blog/2010/december'
  78. node = s.content.node_from_relative_path(path)
  79. assert node
  80. assert Folder(node.relative_path) == Folder(path)
  81. path += '/merry-christmas.html'
  82. resource = s.content.resource_from_relative_path(path)
  83. assert resource
  84. assert resource.relative_path == path
  85. assert not s.content.resource_from_relative_path('/happy-festivus.html')
  86. def test_walk_resources():
  87. s = Site(TEST_SITE_ROOT)
  88. s.load()
  89. pages = [page.name for page in s.content.walk_resources()]
  90. expected = ["404.html",
  91. "about.html",
  92. "apple-touch-icon.png",
  93. "merry-christmas.html",
  94. "crossdomain.xml",
  95. "favicon.ico",
  96. "robots.txt",
  97. "site.css"
  98. ]
  99. pages.sort()
  100. expected.sort()
  101. assert pages == expected
  102. def test_contains_resource():
  103. s = Site(TEST_SITE_ROOT)
  104. s.load()
  105. path = 'blog/2010/december'
  106. node = s.content.node_from_relative_path(path)
  107. assert node.contains_resource('merry-christmas.html')
  108. def test_get_resource():
  109. s = Site(TEST_SITE_ROOT)
  110. s.load()
  111. path = 'blog/2010/december'
  112. node = s.content.node_from_relative_path(path)
  113. resource = node.get_resource('merry-christmas.html')
  114. assert resource == s.content.resource_from_relative_path(
  115. Folder(path).child('merry-christmas.html'))
  116. def test_resource_slug():
  117. s = Site(TEST_SITE_ROOT)
  118. s.load()
  119. path = 'blog/2010/december'
  120. node = s.content.node_from_relative_path(path)
  121. resource = node.get_resource('merry-christmas.html')
  122. assert resource.slug == 'merry-christmas'
  123. def test_get_resource_from_relative_deploy_path():
  124. s = Site(TEST_SITE_ROOT)
  125. s.load()
  126. path = 'blog/2010/december'
  127. node = s.content.node_from_relative_path(path)
  128. resource = node.get_resource('merry-christmas.html')
  129. assert resource == s.content.resource_from_relative_deploy_path(
  130. Folder(path).child('merry-christmas.html'))
  131. resource.relative_deploy_path = Folder(path).child('merry-christmas.php')
  132. assert resource == s.content.resource_from_relative_deploy_path(
  133. Folder(path).child('merry-christmas.php'))
  134. def test_is_processable_default_true():
  135. s = Site(TEST_SITE_ROOT)
  136. s.load()
  137. for page in s.content.walk_resources():
  138. assert page.is_processable
  139. def test_relative_deploy_path():
  140. s = Site(TEST_SITE_ROOT)
  141. s.load()
  142. for page in s.content.walk_resources():
  143. assert page.relative_deploy_path == Folder(page.relative_path)
  144. assert page.url == '/' + page.relative_deploy_path
  145. def test_relative_deploy_path_override():
  146. s = Site(TEST_SITE_ROOT)
  147. s.load()
  148. res = s.content.resource_from_relative_path(
  149. 'blog/2010/december/merry-christmas.html')
  150. res.relative_deploy_path = 'blog/2010/december/happy-holidays.html'
  151. for page in s.content.walk_resources():
  152. if res.source_file == page.source_file:
  153. assert (page.relative_deploy_path ==
  154. 'blog/2010/december/happy-holidays.html')
  155. else:
  156. assert page.relative_deploy_path == Folder(page.relative_path)
  157. class TestSiteWithConfig(object):
  158. @classmethod
  159. def setup_class(cls):
  160. cls.SITE_PATH = File(__file__).parent.child_folder(
  161. 'sites/test_jinja_with_config')
  162. cls.SITE_PATH.make()
  163. TEST_SITE_ROOT.copy_contents_to(cls.SITE_PATH)
  164. cls.config_file = File(cls.SITE_PATH.child('alternate.yaml'))
  165. with open(cls.config_file.path) as config:
  166. cls.config = Config(
  167. sitepath=cls.SITE_PATH, config_dict=yaml.load(config))
  168. cls.SITE_PATH.child_folder('content').rename_to(
  169. cls.config.content_root)
  170. @classmethod
  171. def teardown_class(cls):
  172. cls.SITE_PATH.delete()
  173. def test_load_with_config(self):
  174. s = Site(self.SITE_PATH, config=self.config)
  175. s.load()
  176. path = 'blog/2010/december'
  177. node = s.content.node_from_relative_path(path)
  178. assert node
  179. assert Folder(node.relative_path) == Folder(path)
  180. path += '/merry-christmas.html'
  181. resource = s.content.resource_from_relative_path(path)
  182. assert resource
  183. assert resource.relative_path == path
  184. assert not s.content.resource_from_relative_path(
  185. '/happy-festivus.html')
  186. def test_content_url(self):
  187. s = Site(self.SITE_PATH, config=self.config)
  188. s.load()
  189. path = 'blog/2010/december'
  190. assert s.content_url(path) == "/" + path
  191. def test_content_url_encoding(self):
  192. s = Site(self.SITE_PATH, config=self.config)
  193. s.load()
  194. path = '".jpg'
  195. assert s.content_url(path) == "/" + quote(path)
  196. def test_content_url_encoding_safe(self):
  197. s = Site(self.SITE_PATH, config=self.config)
  198. s.load()
  199. path = '".jpg/abc'
  200. print(s.content_url(path, ""))
  201. print("/" + quote(path, ""))
  202. assert s.content_url(path, "") == "/" + quote(path, "")
  203. def test_media_url(self):
  204. s = Site(self.SITE_PATH, config=self.config)
  205. s.load()
  206. path = 'css/site.css'
  207. assert s.media_url(path) == "/media/" + path
  208. def test_is_media(self):
  209. s = Site(self.SITE_PATH, config=self.config)
  210. s.load()
  211. assert s.is_media('media/css/site.css')
  212. s.config.media_root = 'monkey'
  213. assert not s.is_media('media/css/site.css')
  214. assert s.is_media('monkey/css/site.css')
  215. def test_full_url_for_content(self):
  216. s = Site(self.SITE_PATH, config=self.config)
  217. s.load()
  218. path = 'blog/2010/december'
  219. assert s.full_url(path) == "/" + path
  220. def test_full_url_for_media(self):
  221. s = Site(self.SITE_PATH, config=self.config)
  222. s.load()
  223. path = 'media/css/site.css'
  224. assert s.is_media(path)
  225. full_url = s.full_url(path)
  226. assert full_url == "/" + path
  227. def test_media_url_from_resource(self):
  228. s = Site(self.SITE_PATH, config=self.config)
  229. s.load()
  230. path = 'css/site.css'
  231. resource = s.content.resource_from_relative_path(
  232. Folder("media").child(path))
  233. assert resource
  234. assert resource.full_url == "/media/" + path
  235. def test_config_ignore(self):
  236. c = Config(self.SITE_PATH, config_dict=self.config.to_dict())
  237. s = Site(self.SITE_PATH, config=c)
  238. s.load()
  239. path = 'apple-touch-icon.png'
  240. resource = s.content.resource_from_relative_path(path)
  241. assert resource
  242. assert resource.full_url == "/" + path
  243. s = Site(self.SITE_PATH, config=c)
  244. s.config.ignore.append('*.png')
  245. resource = s.content.resource_from_relative_path(path)
  246. assert not resource
  247. def test_config_ignore_nodes(self):
  248. c = Config(self.SITE_PATH, config_dict=self.config.to_dict())
  249. git = self.SITE_PATH.child_folder('.git')
  250. git.make()
  251. s = Site(self.SITE_PATH, config=c)
  252. s.load()
  253. git_node = s.content.node_from_relative_path('.git')
  254. assert not git_node
  255. blog_node = s.content.node_from_relative_path('blog')
  256. assert blog_node
  257. assert blog_node.full_url == "/blog"
  258. s = Site(self.SITE_PATH, config=c)
  259. s.config.ignore.append('blog')
  260. blog_node = s.content.node_from_relative_path('blog')
  261. assert not blog_node
  262. git_node = s.content.node_from_relative_path('.git')
  263. assert not git_node