def test_raise_extension(self): node = Node(mock.Mock(), None, meta={"templates": []}) node.is_leaf = False with self.assertRaises(TemplateRuntimeError) as excp: jinja_filter(node, ERROR_TEMPLATE) self.assertEqual(excp.exception.message, "This is an error")
def test_input_output(self): content = "hello, how are you?" node = Node(mock.Mock(), None, meta={"external_cmd": "cat {INPUT} | base64 > {OUTPUT}"}) node._content_start = 0 output = external_filter(node, content) expected_output = base64.b64encode(content.encode()).decode() + "\n" self.assertEqual(output, expected_output)
def test_full_url_with_index(self): parent_path = pathlib.Path(self.content_path.name) child_path = pathlib.Path(self.content_path.name, "index.html") child_path.touch() parent_node = Node(parent_path, None, meta=self.default_settings) child_node = Node(child_path, parent_node) self.assertEqual(child_node.full_url, "/")
def test_full_path_with_parent(self): parent_path = pathlib.Path(self.content_path.name) child_path = pathlib.Path(self.content_path.name, "page.html") child_path.touch() parent_node = Node(parent_path, None, meta=self.default_settings) child_node = Node(child_path, parent_node) self.assertEqual(child_node.full_path, self.deploy_path.name + "/page.html")
def test_extends(self): template_name = "bob.j2" with TemporaryDirectory() as tmp_dir: with pathlib.Path(tmp_dir, template_name).open("w") as tmpl: tmpl.write(BASE_TEMPLATE) node = Node(mock.Mock(), None, meta={"templates": [tmp_dir], "extends": "bob.j2"}) node.is_leaf = False result = jinja_filter(node, CONTENT_BLOCK) self.assertEqual(result, "<p>Title</p>\n<p>0</p><p>1</p><p>2</p>")
def test_context(self): path = mock.Mock() path.name = "thisfile.html" node = Node(path, None, meta={"templates": []}) node.is_leaf = False result = jinja_filter(node, "{{ node }}") self.assertEqual(result, "<Node: thisfile.html>") result = jinja_filter(node, "{{ undef_value }}") self.assertEqual(result, "")
def test_strip_exts_default(self): parent_path = pathlib.Path(self.content_path.name) child_path = pathlib.Path(self.content_path.name, "blog.html") child_path.touch() parent_node = Node(parent_path, None, meta=self.default_settings) child_node = Node(child_path, parent_node) self.assertEqual(child_node.strip_exts, [".html"]) self.assertEqual(child_node.full_url, "/blog")
def test_siblings(self): parent_path = pathlib.Path(self.content_path.name) child1_path = pathlib.Path(self.content_path.name, "page1.html") child2_path = pathlib.Path(self.content_path.name, "page2.html") parent_node = Node(parent_path, None, meta=self.default_settings) child1_node = Node(child1_path, parent_node) child2_node = Node(child2_path, parent_node) self.assertEqual(child1_node.siblings, {"page2.html": child2_node}) self.assertEqual(child2_node.siblings, {"page1.html": child1_node})
def test_render_file(self): parent_path = pathlib.Path(self.content_path.name) child_path = pathlib.Path(self.content_path.name, "blog") child_path.touch() parent_node = Node(parent_path, None, meta=self.default_settings) child_node = Node(child_path, parent_node) self.assertFalse(pathlib.Path(child_node.full_path).exists()) child_node.render() self.assertTrue(pathlib.Path(child_node.full_path).exists()) self.assertTrue(pathlib.Path(child_node.full_path).is_file())
def test_strip_exts_disabled(self): parent_path = pathlib.Path(self.content_path.name) child_path = pathlib.Path(self.content_path.name, "blog.html") child_path.touch() settings = {"strip_exts": []} settings.update(self.default_settings) parent_node = Node(parent_path, None, meta=settings) child_node = Node(child_path, parent_node) self.assertEqual(child_node.strip_exts, []) self.assertEqual(child_node.full_url, "/blog.html")
def test_index_file_default(self): parent_path = pathlib.Path(self.content_path.name) child1_path = pathlib.Path(self.content_path.name, "index.html") child1_path.touch() child2_path = pathlib.Path(self.content_path.name, "blog.html") child2_path.touch() parent_node = Node(parent_path, None, meta=self.default_settings) child1_node = Node(child1_path, parent_node) child2_node = Node(child2_path, parent_node) self.assertEqual(child1_node.index_file, "index.html") self.assertEqual(child1_node.full_url, "/") self.assertEqual(child2_node.index_file, "index.html") self.assertEqual(child2_node.full_url, "/blog")
def test_filter_not_matching(self): with TemporaryDirectory() as content_path, TemporaryDirectory() as deploy_path: path = pathlib.Path(content_path, "blog.htm") with path.open("w") as f: f.write(PLAIN_TEMPLATE) node = Node(path, Node(path.parent, None, {"content_path": content_path, "deploy_path": deploy_path, "filter": "exhibition.filters.jinja2", "templates": []})) node.render() with pathlib.Path(deploy_path, "blog.htm").open("r") as f: content = f.read() self.assertEqual(content, PLAIN_TEMPLATE)
def test_cache_bust_multi_glob(self): parent_path = pathlib.Path(self.content_path.name) child1_path = pathlib.Path(self.content_path.name, "bust-me.jpg") child1_path.touch() child2_path = pathlib.Path(self.content_path.name, "page.html") child2_path.touch() meta_path = pathlib.Path(self.content_path.name, "meta.yaml") with meta_path.open("w") as f: f.write("cache_bust_glob:\n - \"*.jpeg\"\n - \"*.jpg\"") parent_node = Node.from_path(parent_path) parent_node.meta.update(**self.default_settings) self.assertEqual(list(parent_node.children.keys()), ["bust-me.jpg", "page.html"]) child1_node = parent_node.children["bust-me.jpg"] child2_node = parent_node.children["page.html"] self.assertEqual( child1_node.full_path, str( pathlib.Path(self.deploy_path.name, "bust-me.{}.jpg".format(EMPTY_DIGEST)))) self.assertEqual(child2_node.full_path, str(pathlib.Path(self.deploy_path.name, "page.html"))) self.assertEqual(child1_node.full_url, "/bust-me.{}.jpg".format(EMPTY_DIGEST)) self.assertEqual(child2_node.full_url, "/page")
def test_dir_data(self): path = pathlib.Path(self.content_path.name, "blog.json") path.mkdir() node = Node(path, None) self.assertEqual(node.data, None)
def test_json_data(self): path = pathlib.Path(self.content_path.name, "blog.json") with path.open("w") as f: f.write(JSON_FILE) node = Node(path, None) self.assertEqual(node.data, {"thingy": 3, "bob": [1, 2]})
def test_render_binary_file(self): parent_path = pathlib.Path(self.content_path.name) child_path = pathlib.Path(self.content_path.name, "blog") with child_path.open("wb") as cf: cf.write(BINARY_FILE) parent_node = Node(parent_path, None, meta=self.default_settings) child_node = Node(child_path, parent_node) self.assertFalse(pathlib.Path(child_node.full_path).exists()) child_node.render() self.assertTrue(pathlib.Path(child_node.full_path).exists()) self.assertTrue(pathlib.Path(child_node.full_path).is_file()) with pathlib.Path(child_node.full_path).open("rb") as df: content = df.read() self.assertEqual(content, BINARY_FILE)
def test_full_url_base_url(self): path = pathlib.Path(self.content_path.name, "page.html") path.touch() for base in ["/base/", "/base", "base/"]: self.default_settings["base_url"] = base node = Node(path, None, meta=self.default_settings) self.assertEqual(node.full_url, "/base/")
def test_data_unsupported(self): path = pathlib.Path(self.content_path.name, "blog.xml") path.touch() node = Node(path, None) with self.assertRaises(KeyError): node.data
def test_empty_mark_extension(self): with TemporaryDirectory() as content_path, TemporaryDirectory() as deploy_path: path = pathlib.Path(content_path, "blog.html") with path.open("w") as f: f.write(EMPTY_MARK_TEMPLATE) node = Node(path, Node(path.parent, None, {"content_path": content_path, "deploy_path": deploy_path, "filter": "exhibition.filters.jinja2", "templates": []})) self.assertEqual(node.marks, {"thingy": Markup("")}) node.render() with pathlib.Path(deploy_path, "blog.html").open("r") as f: content = f.read() self.assertEqual(content, "\nBye")
def test_strip_exts_multiple(self): parent_path = pathlib.Path(self.content_path.name) child1_path = pathlib.Path(self.content_path.name, "blog.bluh") child1_path.touch() child2_path = pathlib.Path(self.content_path.name, "story.tw2") child2_path.touch() settings = {"strip_exts": [".tw2", ".bluh"]} settings.update(self.default_settings) parent_node = Node(parent_path, None, meta=settings) child1_node = Node(child1_path, parent_node) child2_node = Node(child2_path, parent_node) self.assertEqual(child1_node.strip_exts, [".tw2", ".bluh"]) self.assertEqual(child1_node.full_url, "/blog") self.assertEqual(child2_node.strip_exts, [".tw2", ".bluh"]) self.assertEqual(child2_node.full_url, "/story")
def test_from_path(self): parent_path = pathlib.Path(self.content_path.name) child1_path = pathlib.Path(self.content_path.name, "page1.html") child1_path.touch() child2_path = pathlib.Path(self.content_path.name, "page2.html") child2_path.touch() parent_node = Node.from_path(parent_path) self.assertEqual(list(parent_node.children.keys()), ["page1.html", "page2.html"])
def test_process_no_meta(self): path = pathlib.Path(self.content_path.name, "blog") with path.open("w") as f: f.write(NO_META) node = Node(path, None) self.assertEqual(list(node._meta.keys()), []) self.assertEqual(node._content_start, None) self.assertEqual(list(node.meta.keys()), []) self.assertEqual(node._content_start, 0)
def test_filter_glob(self): with TemporaryDirectory() as content_path, TemporaryDirectory() as deploy_path: # default glob is *.* for filename in ["blog.html", "mytext.txt"]: content = "hello {}, how are you?".format(filename) path = pathlib.Path(content_path, "blog.htm") with path.open("w") as f: f.write(content) node = Node(path, Node(path.parent, None, {"content_path": content_path, "deploy_path": deploy_path, "filter": "exhibition.filters.external", "external_cmd": "cat {INPUT} | base64 > {OUTPUT}"})) node.render() with pathlib.Path(deploy_path, "blog.htm").open("r") as f: output = f.read() expected_output = base64.b64encode(content.encode()).decode() + "\n" self.assertEqual(output, expected_output)
def test_from_path_and_ignore(self): parent_path = pathlib.Path(self.content_path.name) child1_path = pathlib.Path(self.content_path.name, "page1.html") child1_path.touch() child2_path = pathlib.Path(self.content_path.name, "page2.html") child2_path.touch() child3_path = pathlib.Path(self.content_path.name, "picture.jpg") child3_path.touch() parent_node = Node.from_path(parent_path, meta={"ignore": "*.html"}) self.assertEqual(list(parent_node.children.keys()), ["picture.jpg"])
def test_process_long_meta(self): path = pathlib.Path(self.content_path.name, "blog") with path.open("w") as f: f.write(LONG_META) node = Node(path, None) self.assertEqual(list(node._meta.keys()), []) self.assertEqual(node._content_start, None) self.assertCountEqual(list(node.meta.keys()), ["thing%s" % i for i in range(100)]) self.assertEqual(node._content_start, 1098)
def test_get_content(self): path = pathlib.Path(self.content_path.name, "blog") with path.open("w") as f: f.write("a" * 10) node = Node(path, None) self.assertEqual(node.get_content(), "a" * 10) self.assertEqual(node._content_start, 0) node._content = None node._content_start = 5 node.get_content() self.assertEqual(node.get_content(), "a" * 5) self.assertEqual(node._content_start, 5)
def test_cached_data(self): path = pathlib.Path(self.content_path.name, "blog.yaml") with path.open("w") as f: f.write(YAML_FILE) node = Node(path, None) self.assertEqual(node.data, {"thingy": 3, "bob": [1, 2]}) with path.open("w") as f: f.write("test: 1") self.assertEqual(node.data, {"thingy": 3, "bob": [1, 2]})
def test_walk(self): parent_path = pathlib.Path(self.content_path.name) child_path = pathlib.Path(self.content_path.name, "index.html") child_path.touch() parent_node = Node(parent_path, None, meta=self.default_settings) child_node = Node(child_path, parent_node) self.assertEqual(list(parent_node.walk(include_self=True)), [parent_node, child_node]) self.assertEqual(list(parent_node.walk()), [child_node]) self.assertEqual(list(child_node.walk(include_self=True)), [child_node]) self.assertEqual(list(child_node.walk()), [])
def test_metaselect(self): node = Node(mock.Mock(), None, meta={"templates": []}) node._content_start = 0 for i in [True, True, False, True, None]: new_node = Node(mock.Mock(), node, meta={"bob": i}) new_node._content_start = 0 result = jinja_filter(node, METASELECT_TEMPLATE) self.assertEqual(result, "TrueTrueTrue")
def test_metasort(self): node = Node(mock.Mock(), None, meta={"templates": []}) node._content_start = 0 for i in [3, 5, 27, 2, 1]: new_node = Node(mock.Mock(), node, meta={"bob": i}) new_node._content_start = 0 result = jinja_filter(node, METASORT_TEMPLATE) self.assertEqual(result, "123527")