def test_generator_template_begin_text_resource_called_for_single_resource(self): with patch.object(PluginLoaderStub, 'begin_text_resource') as begin_text_resource_stub: begin_text_resource_stub.return_value = '' gen = Generator(self.site) gen.generate_all() begin_text_resource_stub.reset_mock() path = self.site.content.source_folder.child('about.html') gen = Generator(self.site) gen.generate_resource_at_path(path, incremental=True) called_with_resources = sorted([arg[0][0].path for arg in begin_text_resource_stub.call_args_list]) assert begin_text_resource_stub.call_count == 1 assert called_with_resources[0] == path
def test_context(self): site = Site(TEST_SITE, Config(TEST_SITE, config_dict={ "context": { "data": { "abc": "def" } } })) text = """ {% extends "base.html" %} {% block main %} abc = {{ abc }} Hi! I am a test template to make sure jinja2 generation works well with hyde. {{resource.name}} {% endblock %} """ site.load() resource = site.content.resource_from_path( TEST_SITE.child('content/about.html')) gen = Generator(site) resource.source_file.write(text) gen.generate_all() target = File(site.config.deploy_root_path.child(resource.name)) assert "abc = def" in target.read_all()
def test_plugins(self): text = """ --- title: Hey author: Me twitter: @me --- {%% extends "base.html" %%} {%% block main %%} Hi! I am a test template to make sure jinja2 generation works well with hyde. <span class="title">{{resource.meta.title}}</span> <span class="author">{{resource.meta.author}}</span> <span class="twitter">{{resource.meta.twitter}}</span> {%% endblock %%} """ index = File(self.SITE_PATH.child('content/blog/index.html')) index.write(text) self.setup_config(['**/*.html', 'media/css/*.css']) conf = {'plugins': ['hyde.ext.plugins.meta.MetaPlugin']} conf.update(self.config.to_dict()) s = Site(self.SITE_PATH, Config(sitepath=self.SITE_PATH, config_dict=conf)) g = Generator(s) g.generate_all() source = s.content.resource_from_relative_path('blog/index.html') target = File( s.config.deploy_root_path.child(source.relative_deploy_path)) left = source.source_file.read_all() right = target.read_all() assert left == right
def test_generator_template_begin_generation_called_for_single_resource(self): with patch.object(PluginLoaderStub, 'begin_generation') as begin_generation_stub: gen = Generator(self.site) path = self.site.content.source_folder.child('about.html') gen.generate_resource_at_path(path) assert begin_generation_stub.call_count == 1
def test_ignores_pattern_in_content(self): text = """ {% markdown %} Heading 1 === Heading 2 === {% endmarkdown %} """ about2 = File(TEST_SITE.child('content/about2.html')) about2.write(text) s = Site(TEST_SITE) s.load() res = s.content.resource_from_path(about2.path) assert res.is_processable s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin'] gen = Generator(s) gen.generate_all() target = File(Folder(s.config.deploy_root_path).child('about2.html')) text = target.read_all() q = PyQuery(text) assert q("h1").length == 2 assert q("h1:eq(0)").text().strip() == "Heading 1" assert q("h1:eq(1)").text().strip() == "Heading 2"
def test_textlinks(self): d = { 'objects': 'template/variables', 'plugins': 'plugins/metadata', 'sorter': 'plugins/sorter' } text = """ {%% markdown %%} [[!!img/hyde-logo.png]] * [Rich object model][hyde objects] and [overridable hierarchical metadata]([[ %(plugins)s ]]) thats available for use in templates. * Configurable [sorting][], filtering and grouping support. [hyde objects]: [[ %(objects)s ]] [sorting]: [[%(sorter)s]] {%% endmarkdown %%} """ site = Site(TEST_SITE) site.config.plugins = ['hyde.ext.plugins.text.TextlinksPlugin'] site.config.base_url = 'http://example.com/' site.config.media_url = '/media' tlink = File(site.content.source_folder.child('tlink.html')) tlink.write(text % d) print((tlink.read_all())) gen = Generator(site) gen.generate_all() f = File(site.config.deploy_root_path.child(tlink.name)) assert f.exists html = f.read_all() assert html for name, path in list(d.items()): assert site.config.base_url + quote(path) in html assert '/media/img/hyde-logo.png' in html
def test_generator_template_begin_site_called_for_single_node(self): with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub: gen = Generator(self.site) path = self.site.content.source_folder gen.generate_node_at_path(path) assert begin_site_stub.call_count == 1
def test_plugin_node_filters_begin_text_resource(self): def empty_return(*args, **kwargs): return None with patch.object(ConstantReturnPlugin, 'begin_text_resource', new=Mock(wraps=empty_return)) as mock1: with patch.object(NoReturnPlugin, 'begin_text_resource', new=Mock(wraps=empty_return)) as mock2: self.site.config.plugins = [ 'hyde.tests.test_plugin.ConstantReturnPlugin', 'hyde.tests.test_plugin.NoReturnPlugin' ] self.site.config.constantreturn = Expando( dict(include_paths="media")) self.site.config.noreturn = Expando( dict(include_file_pattern="*.html", include_paths=["blog"])) gen = Generator(self.site) gen.generate_all() mock1_args = sorted( set([arg[0][0].name for arg in mock1.call_args_list])) mock2_args = sorted( set([arg[0][0].name for arg in mock2.call_args_list])) assert len(mock1_args) == 1 assert len(mock2_args) == 1 assert mock1_args == ["site.css"] assert mock2_args == ["merry-christmas.html"]
def test_can_compress_with_stylus(self): s = Site(TEST_SITE) s.config.mode = "production" s.config.plugins = ['hyde.ext.plugins.stylus.StylusPlugin'] paths = [ '/usr/local/share/npm/bin/stylus', '~/local/bin/stylus', '~/bin/stylus' ] stylus = [path for path in paths if File(path).exists] if not stylus: assert False, "Cannot find the stylus executable" stylus = stylus[0] s.config.stylus = Expando(dict(app=stylus)) source = TEST_SITE.child('content/media/css/site.styl') target = File( Folder(s.config.deploy_root_path).child('media/css/site.css')) gen = Generator(s) gen.generate_resource_at_path(source) assert target.exists text = target.read_all() expected_text = File( STYLUS_SOURCE.child('expected-site-compressed.css')).read_all() assert text.strip() == expected_text.strip()
def test_plugin_filters_begin_text_resource(self): def empty_return(self, resource, text=''): return text with patch.object(ConstantReturnPlugin, 'begin_text_resource', new=Mock(wraps=empty_return)) as mock1: with patch.object(NoReturnPlugin, 'begin_text_resource', new=Mock(wraps=empty_return)) as mock2: self.site.config.plugins = [ 'hyde.tests.test_plugin.ConstantReturnPlugin', 'hyde.tests.test_plugin.NoReturnPlugin' ] self.site.config.constantreturn = Expando( dict(include_file_pattern="*.css")) self.site.config.noreturn = Expando( dict(include_file_pattern=["*.html", "*.txt"])) gen = Generator(self.site) gen.generate_all() mock1_args = sorted( set([arg[0][0].name for arg in mock1.call_args_list])) mock2_args = sorted( set([arg[0][0].name for arg in mock2.call_args_list])) assert len(mock1_args) == 1 assert len(mock2_args) == 4 assert mock1_args == ["site.css"] assert mock2_args == [ "404.html", "about.html", "merry-christmas.html", "robots.txt" ]
def test_depends(self): s = Site(TEST_SITE) s.config.plugins = [ 'hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.depends.DependsPlugin' ] text = """ === depends: index.html === """ inc = File(TEST_SITE.child('content/inc.md')) inc.write(text) gen = Generator(s) gen.load_site_if_needed() gen.load_template_if_needed() def dateformat(x): return x.strftime('%Y-%m-%d') gen.template.env.filters['dateformat'] = dateformat gen.generate_resource_at_path(inc.name) res = s.content.resource_from_relative_path(inc.name) assert len(res.depends) == 1 assert 'index.html' in res.depends deps = list(gen.get_dependencies(res)) assert len(deps) == 3 assert 'helpers.html' in deps assert 'layout.html' in deps assert 'index.html' in deps
def test_url_cleaner(self): s = Site(TEST_SITE) cfg = """ plugins: - hyde.ext.plugins.urls.UrlCleanerPlugin urlcleaner: index_file_names: - about.html strip_extensions: - html append_slash: true """ s.config = Config(TEST_SITE, config_dict=yaml.load(cfg, Loader=yaml.FullLoader)) text = """ {% extends "base.html" %} {% block main %} <a id="index" href="{{ content_url('about.html') }}"></a> <a id="blog" href= "{{ content_url('blog/2010/december/merry-christmas.html') }}"></a> {% endblock %} """ about2 = File(TEST_SITE.child('content/test.html')) about2.write(text) gen = Generator(s) gen.generate_all() from pyquery import PyQuery target = File(Folder(s.config.deploy_root_path).child('test.html')) text = target.read_all() q = PyQuery(text) assert q('a#index').attr("href") == '/' assert q('a#blog').attr( "href") == '/blog/2010/december/merry-christmas'
def test_generator_template_node_complete_not_called_for_single_resource_second_time(self): with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub: gen = Generator(self.site) gen.generate_all() assert node_complete_stub.call_count == len(self.content_nodes) gen.generate_resource_at_path(self.site.content.source_folder.child('about.html')) assert node_complete_stub.call_count == len(self.content_nodes) # No extra calls
def test_multiple_levels(self): page_d = {'title': 'An even nicer title'} blog_d = {'author': 'Laks'} content_d = {'title': 'A nice title', 'author': 'Lakshmi Vyas'} site_d = { 'author': 'Lakshmi', 'twitter': 'lakshmivyas', 'nodemeta': 'meta.yaml' } text = """ --- title: %(title)s --- {%% extends "base.html" %%} {%% block main %%} Hi! I am a test template to make sure jinja2 generation works well with hyde. <span class="title">{{resource.meta.title}}</span> <span class="author">{{resource.meta.author}}</span> <span class="twitter">{{resource.meta.twitter}}</span> {%% endblock %%} """ about2 = File(TEST_SITE.child('content/blog/about2.html')) about2.write(text % page_d) content_meta = File(TEST_SITE.child('content/nodemeta.yaml')) content_meta.write(yaml.dump(content_d)) content_meta = File(TEST_SITE.child('content/blog/meta.yaml')) content_meta.write(yaml.dump(blog_d)) s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin'] s.config.meta = site_d gen = Generator(s) gen.generate_all() expected = {} expected.update(site_d) expected.update(content_d) expected.update(blog_d) expected.update(page_d) res = s.content.resource_from_path(about2.path) assert hasattr(res, 'meta') for k, v in list(expected.items()): assert hasattr(res.meta, k) assert getattr(res.meta, k) == v target = File( Folder(s.config.deploy_root_path).child('blog/about2.html')) text = target.read_all() q = PyQuery(text) for k, v in list(expected.items()): if k != 'nodemeta': assert v in q("span." + k).text()
def _generate_site_with_meta(self, meta): self.site.config.mode = "production" self.site.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin', 'hyde.ext.plugins.images.ImageThumbnailsPlugin'] mlink = File(self.image_folder.child('meta.yaml')) meta_text = yaml.dump(meta, default_flow_style=False) mlink.write(meta_text) gen = Generator(self.site) gen.generate_all()
def test_generator_template_site_complete_not_called_for_single_node_second_time(self): with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: gen = Generator(self.site) gen.generate_all() path = self.site.content.source_folder gen.generate_node_at_path(path) assert site_complete_stub.call_count == 1
def __init__(self, site, address, port): self.site = site self.site.load() self.generator = Generator(self.site) self.request_time = datetime.strptime('1-1-1999', '%m-%d-%Y') self.regeneration_time = datetime.strptime('1-1-1998', '%m-%d-%Y') self.__is_shut_down = threading.Event() self.__shutdown_request = False HTTPServer.__init__(self, (address, port), HydeRequestHandler)
def test_generator_template_node_complete_called(self): with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub: gen = Generator(self.site) gen.generate_all() assert node_complete_stub.call_count == len(self.content_nodes) called_with_nodes = sorted([arg[0][0].path for arg in node_complete_stub.call_args_list]) assert called_with_nodes == self.content_nodes
def test_generator_template_begin_binary_resource_called(self): with patch.object(PluginLoaderStub, 'begin_binary_resource') as begin_binary_resource_stub: gen = Generator(self.site) gen.generate_all() called_with_resources = sorted([arg[0][0].path for arg in begin_binary_resource_stub.call_args_list]) assert begin_binary_resource_stub.call_count == len(self.content_binary_resources) assert called_with_resources == self.content_binary_resources
def test_generator_template_begin_text_resource_called(self): with patch.object(PluginLoaderStub, 'begin_text_resource') as begin_text_resource_stub: begin_text_resource_stub.reset_mock() begin_text_resource_stub.return_value = '' gen = Generator(self.site) gen.generate_all() called_with_resources = sorted([arg[0][0].path for arg in begin_text_resource_stub.call_args_list]) assert set(called_with_resources) == set(self.content_text_resources)
def test_generator_template_begin_node_not_called_4_sngl_res_scnd_tm(self): with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub: gen = Generator(self.site) gen.generate_all() assert begin_node_stub.call_count == len(self.content_nodes) gen.generate_resource_at_path( self.site.content.source_folder.child('about.html')) assert begin_node_stub.call_count == len( self.content_nodes) # No extra calls
def test_generate_resource_from_path_with_is_processable_false(self): site = Site(TEST_SITE) site.load() resource = site.content.resource_from_path( TEST_SITE.child('content/about.html')) resource.is_processable = False gen = Generator(site) gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) about = File(Folder(site.config.deploy_root_path).child('about.html')) assert not about.exists
def test_plugin_chaining(self): self.site.config.plugins = [ 'test_plugin.ConstantReturnPlugin', 'test_plugin.NoReturnPlugin' ] path = self.site.content.source_folder.child('about.html') gen = Generator(self.site) gen.generate_resource_at_path(path) about = File( Folder(self.site.config.deploy_root_path).child('about.html')) assert about.read_all() == "Jam"
def test_generate_resource_from_path(self): site = Site(TEST_SITE) site.load() gen = Generator(site) gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) about = File(Folder(site.config.deploy_root_path).child('about.html')) assert about.exists text = about.read_all() q = PyQuery(text) assert about.name in q("div#main").text()
def test_context_providers_equivalence(self): import yaml events = """ 2011: - title: "one event" location: "a city" - title: "one event" location: "a city" 2010: - title: "one event" location: "a city" - title: "one event" location: "a city" """ events_dict = yaml.load(events, Loader=yaml.FullLoader) config_dict = dict(context=dict( data=dict(events1=events_dict), providers=dict(events2="events.yaml") )) text = """ {%% extends "base.html" %%} {%% block main %%} <ul> {%% for year, eventlist in %s %%} <li> <h1>{{ year }}</h1> <ul> {%% for event in eventlist %%} <li>{{ event.title }}-{{ event.location }}</li> {%% endfor %%} </ul> </li> {%% endfor %%} </ul> {%% endblock %%} """ File(TEST_SITE.child('events.yaml')).write(events) f1 = File(TEST_SITE.child('content/text1.html')) f2 = File(TEST_SITE.child('content/text2.html')) f1.write(text % "events1") f2.write(text % "events2") site = Site(TEST_SITE, Config(TEST_SITE, config_dict=config_dict)) site.load() gen = Generator(site) gen.generate_all() left = File(site.config.deploy_root_path.child(f1.name)).read_all() right = File(site.config.deploy_root_path.child(f2.name)).read_all() assert left == right
def test_nav_with_grouper(self): text =""" {% for group, resources in site.content.walk_section_groups() %} <ul> <li> <h2>{{ group.name|title }}</h2> <h3>{{ group.description }}</h3> <ul class="links"> {% for resource in resources %} <li>{{resource.name}}</li> {% endfor %} </ul> </li> </ul> {% endfor %} """ expected = """ <ul> <li> <h2>Section</h2> <h3>Sections in the site</h3> <ul class="links"></ul> </li> </ul> <ul> <li> <h2>Start</h2> <h3>Getting Started</h3> <ul class="links"> <li>installation.html</li> <li>overview.html</li> <li>templating.html</li> </ul> </li> </ul> <ul> <li> <h2>Plugins</h2> <h3>Plugins</h3> <ul class="links"> <li>plugins.html</li> <li>tags.html</li> </ul> </li> </ul> """ gen = Generator(self.s) gen.load_site_if_needed() gen.load_template_if_needed() out = gen.template.render(text, {'site':self.s}) assert_html_equals(out, expected)
def test_can_execute_optipng(self): s = Site(TEST_SITE) s.config.mode = "production" s.config.plugins = ['hyde.ext.plugins.optipng.OptiPNGPlugin'] s.config.optipng = Expando(dict(args=dict(quiet=""))) source =File(TEST_SITE.child('content/media/images/hyde-lt-b.png')) target = File(Folder(s.config.deploy_root_path).child('media/images/hyde-lt-b.png')) gen = Generator(s) gen.generate_resource_at_path(source) assert target.exists assert target.size < source.size
def setUp(self): TEST_SITE.make() TEST_SITE.parent.child_folder( 'sites/test_paginator').copy_contents_to(TEST_SITE) self.s = Site(TEST_SITE) self.deploy = TEST_SITE.child_folder('deploy') self.gen = Generator(self.s) self.gen.load_site_if_needed() self.gen.load_template_if_needed() self.gen.generate_all()
def test_generator_template_site_complt_not_call_4_sngl_res_scnd_tm(self): with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: gen = Generator(self.site) gen.generate_all() assert site_complete_stub.call_count == 1 path = self.site.content.source_folder.child('about.html') gen.generate_resource_at_path(path) assert site_complete_stub.call_count == 1
def _generic_test_image(self, text): self.site.config.mode = "production" self.site.config.plugins = ['hyde.ext.plugins.images.ImageSizerPlugin'] tlink = File(self.site.content.source_folder.child('timg.html')) tlink.write(text) gen = Generator(self.site) gen.generate_all() f = File(self.site.config.deploy_root_path.child(tlink.name)) assert f.exists html = f.read_all() assert html return html