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