/hyde/tests/test_simple_copy.py

http://github.com/hyde/hyde · Python · 139 lines · 107 code · 13 blank · 19 comment · 1 complexity · 1bf3f282858686f380541365e855831a MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests the simple copy feature.
  4. In order to mark some files to simply be copied to the
  5. destination without any processing what so ever add this
  6. to the config (site.yaml for example):
  7. simple_copy:
  8. - media/css/*.css
  9. - media/js/*.js
  10. - **/*.js
  11. Matching is done with `fnmatch` module. So any `glob` that fnmatch
  12. can process is a valid pattern.
  13. Use nose
  14. `$ pip install nose`
  15. `$ nosetests`
  16. """
  17. import yaml
  18. from hyde.model import Config
  19. from hyde.site import Site
  20. from hyde.generator import Generator
  21. from fswrap import File
  22. from nose.tools import nottest
  23. TEST_SITE_ROOT = File(__file__).parent.child_folder('sites/test_jinja')
  24. class TestSimpleCopy(object):
  25. @classmethod
  26. def setup_class(cls):
  27. cls.SITE_PATH = File(__file__).parent.child_folder('sites/test_jinja_with_config')
  28. cls.SITE_PATH.make()
  29. TEST_SITE_ROOT.copy_contents_to(cls.SITE_PATH)
  30. @classmethod
  31. def teardown_class(cls):
  32. cls.SITE_PATH.delete()
  33. @nottest
  34. def setup_config(self, passthru):
  35. self.config_file = File(self.SITE_PATH.child('site.yaml'))
  36. with open(self.config_file.path) as config:
  37. conf = yaml.load(config)
  38. conf['simple_copy'] = passthru
  39. self.config = Config(sitepath=self.SITE_PATH, config_dict=conf)
  40. def test_simple_copy_basic(self):
  41. self.setup_config([
  42. 'about.html'
  43. ])
  44. s = Site(self.SITE_PATH, config=self.config)
  45. s.load()
  46. res = s.content.resource_from_relative_path('about.html')
  47. assert res
  48. assert res.simple_copy
  49. def test_simple_copy_directory(self):
  50. self.setup_config([
  51. '**/*.html'
  52. ])
  53. s = Site(self.SITE_PATH, config=self.config)
  54. s.load()
  55. res = s.content.resource_from_relative_path('about.html')
  56. assert res
  57. assert not res.simple_copy
  58. res = s.content.resource_from_relative_path('blog/2010/december/merry-christmas.html')
  59. assert res
  60. assert res.simple_copy
  61. def test_simple_copy_multiple(self):
  62. self.setup_config([
  63. '**/*.html',
  64. 'media/css/*.css'
  65. ])
  66. s = Site(self.SITE_PATH, config=self.config)
  67. s.load()
  68. res = s.content.resource_from_relative_path('about.html')
  69. assert res
  70. assert not res.simple_copy
  71. res = s.content.resource_from_relative_path('blog/2010/december/merry-christmas.html')
  72. assert res
  73. assert res.simple_copy
  74. res = s.content.resource_from_relative_path('media/css/site.css')
  75. assert res
  76. assert res.simple_copy
  77. def test_generator(self):
  78. self.setup_config([
  79. '**/*.html',
  80. 'media/css/*.css'
  81. ])
  82. s = Site(self.SITE_PATH, self.config)
  83. g = Generator(s)
  84. g.generate_all()
  85. source = s.content.resource_from_relative_path('blog/2010/december/merry-christmas.html')
  86. target = File(s.config.deploy_root_path.child(source.relative_deploy_path))
  87. left = source.source_file.read_all()
  88. right = target.read_all()
  89. assert left == right
  90. def test_plugins(self):
  91. text = """
  92. ---
  93. title: Hey
  94. author: Me
  95. twitter: @me
  96. ---
  97. {%% extends "base.html" %%}
  98. {%% block main %%}
  99. Hi!
  100. I am a test template to make sure jinja2 generation works well with hyde.
  101. <span class="title">{{resource.meta.title}}</span>
  102. <span class="author">{{resource.meta.author}}</span>
  103. <span class="twitter">{{resource.meta.twitter}}</span>
  104. {%% endblock %%}
  105. """
  106. index = File(self.SITE_PATH.child('content/blog/index.html'))
  107. index.write(text)
  108. self.setup_config([
  109. '**/*.html',
  110. 'media/css/*.css'
  111. ])
  112. conf = {'plugins': ['hyde.ext.plugins.meta.MetaPlugin']}
  113. conf.update(self.config.to_dict())
  114. s = Site(self.SITE_PATH, Config(sitepath=self.SITE_PATH, config_dict=conf))
  115. g = Generator(s)
  116. g.generate_all()
  117. source = s.content.resource_from_relative_path('blog/index.html')
  118. target = File(s.config.deploy_root_path.child(source.relative_deploy_path))
  119. left = source.source_file.read_all()
  120. right = target.read_all()
  121. assert left == right