PageRenderTime 29ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/tests/ext/test_combine.py

http://github.com/hyde/hyde
Python | 148 lines | 133 code | 9 blank | 6 comment | 0 complexity | 2484cf9ed2c7de957415b97a57a5fcbb MD5 | raw file
Possible License(s): MIT
  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.generator import Generator
  8. from hyde.site import Site
  9. from fswrap import File, Folder
  10. COMBINE_SOURCE = File(__file__).parent.child_folder('combine')
  11. TEST_SITE = File(__file__).parent.parent.child_folder('_test')
  12. class CombineTester(object):
  13. def _test_combine(self, content):
  14. s = Site(TEST_SITE)
  15. s.config.plugins = [
  16. 'hyde.ext.plugins.meta.MetaPlugin',
  17. 'hyde.ext.plugins.structure.CombinePlugin']
  18. source = TEST_SITE.child('content/media/js/script.js')
  19. target = File(Folder(s.config.deploy_root_path).child('media/js/script.js'))
  20. File(source).write(content)
  21. gen = Generator(s)
  22. gen.generate_resource_at_path(source)
  23. assert target.exists
  24. text = target.read_all()
  25. return text, s
  26. class TestCombine(CombineTester):
  27. def setUp(self):
  28. TEST_SITE.make()
  29. TEST_SITE.parent.child_folder(
  30. 'sites/test_jinja').copy_contents_to(TEST_SITE)
  31. TEST_SITE.child_folder('content/media/js').make()
  32. COMBINE_SOURCE.copy_contents_to(TEST_SITE.child('content/media/js'))
  33. def tearDown(self):
  34. TEST_SITE.delete()
  35. def test_combine_top(self):
  36. text, _ = self._test_combine("""
  37. ---
  38. combine:
  39. files: script.*.js
  40. where: top
  41. ---
  42. Last line""")
  43. assert text == """var a = 1 + 2;
  44. var b = a + 3;
  45. var c = a + 5;
  46. Last line"""
  47. return
  48. def test_combine_bottom(self):
  49. text, _ = self._test_combine("""
  50. ---
  51. combine:
  52. files: script.*.js
  53. where: bottom
  54. ---
  55. First line
  56. """)
  57. expected = """First line
  58. var a = 1 + 2;
  59. var b = a + 3;
  60. var c = a + 5;
  61. """
  62. assert text.strip() == expected.strip()
  63. return
  64. def test_combine_bottom_unsorted(self):
  65. text, _ = self._test_combine("""
  66. ---
  67. combine:
  68. sort: false
  69. files:
  70. - script.3.js
  71. - script.1.js
  72. - script.2.js
  73. where: bottom
  74. ---
  75. First line
  76. """)
  77. expected = """First line
  78. var c = a + 5;
  79. var a = 1 + 2;
  80. var b = a + 3;
  81. """
  82. assert text.strip() == expected.strip()
  83. return
  84. def test_combine_remove(self):
  85. _, s = self._test_combine("""
  86. ---
  87. combine:
  88. files: script.*.js
  89. remove: yes
  90. ---
  91. First line""")
  92. for i in range(1,4):
  93. assert not File(Folder(s.config.deploy_root_path).
  94. child('media/js/script.%d.js' % i)).exists
  95. class TestCombinePaths(CombineTester):
  96. def setUp(self):
  97. TEST_SITE.make()
  98. TEST_SITE.parent.child_folder(
  99. 'sites/test_jinja').copy_contents_to(TEST_SITE)
  100. TEST_SITE.child_folder('content/media/js').make()
  101. JS = TEST_SITE.child_folder('content/scripts').make()
  102. S1 = JS.child_folder('s1').make()
  103. S2 = JS.child_folder('s2').make()
  104. S3 = JS.child_folder('s3').make()
  105. File(COMBINE_SOURCE.child('script.1.js')).copy_to(S1)
  106. File(COMBINE_SOURCE.child('script.2.js')).copy_to(S2)
  107. File(COMBINE_SOURCE.child('script.3.js')).copy_to(S3)
  108. def tearDown(self):
  109. TEST_SITE.delete()
  110. def test_combine_top(self):
  111. text, _ = self._test_combine("""
  112. ---
  113. combine:
  114. root: scripts
  115. recurse: true
  116. files: script.*.js
  117. where: top
  118. ---
  119. Last line""")
  120. assert text == """var a = 1 + 2;
  121. var b = a + 3;
  122. var c = a + 5;
  123. Last line"""
  124. return