def test_render_static(self): """Test: Run render_templates("tests/template/static")""" template_dir = os.path.join(TEMPLATE_DIR, "static") try: context = {"first": "A", "second": "B"} self.assertEqual(render_templates(template_dir, context, "jinja", "utf-8"), 0) with open(os.path.join(template_dir, "counting"), encoding="utf-8") as counting_file: self.assertEqual(counting_file.read(), "Counting:\n* A\n* B\n* 3\n") finally: os.remove(os.path.join(template_dir, "counting"))
def test_render_invalid(self): """Test: Run render_templates("tests/template/invalid")""" template_dir = os.path.join(TEMPLATE_DIR, "invalid") with self.assertLogs("ionit", level="ERROR") as context_manager: self.assertEqual(render_templates(template_dir, {}, "jinja", "utf-8"), 1) self.assertFalse(os.path.exists(os.path.join(template_dir, "invalid"))) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], re.compile( r"ERROR:ionit:Failed to load template '\S*template/invalid/invalid.jinja':" "\n.*\njinja2.exceptions.TemplateSyntaxError: unexpected 'end of template'$", flags=re.DOTALL, ), )
def test_missing_context(self): """Test: Missing context for render_templates("tests/template/static")""" template_dir = os.path.join(TEMPLATE_DIR, "static") with self.assertLogs("ionit", level="ERROR") as context_manager: self.assertEqual(render_templates(template_dir, {"second": "B"}, "jinja", "utf-8"), 1) self.assertFalse(os.path.exists(os.path.join(template_dir, "counting"))) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], re.compile( r"^ERROR:ionit:Failed to render '\S*template/static/counting.jinja':\n.*\n" "jinja2.exceptions.UndefinedError: 'first' is undefined$", flags=re.DOTALL, ), )
def test_attribution_error(self): """Test: Run render_templates("tests/template/attribution-error")""" template_dir = os.path.join(TEMPLATE_DIR, "attribution-error") with self.assertLogs("ionit", level="ERROR") as context_manager: self.assertEqual(render_templates(template_dir, {}, "jinja", "utf-8"), 1) self.assertFalse(os.path.exists(os.path.join(template_dir, "error"))) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], re.compile( r"^ERROR:ionit:Failed to render '\S*template/attribution-error/error.jinja':" "\n.*\nAttributeError: 'dict_items' object has no attribute 'items'$", flags=re.DOTALL, ), )
def test_render_function(self): """Test: Run render_templates("tests/template/function")""" template_dir = os.path.join(TEMPLATE_DIR, "function") try: context = {"answer_to_all_questions": lambda: 42} self.assertEqual(render_templates(template_dir, context, "jinja", "utf-8"), 0) with open(os.path.join(template_dir, "Document"), encoding="utf-8") as document_file: self.assertEqual( document_file.read(), ( "The answer to the Ultimate Question of Life, The Universe, " "and Everything is 42.\n" ), ) finally: os.remove(os.path.join(template_dir, "Document"))
def test_render_write_protected(self): """Test: Run render_templates("tests/template/static"), but write protected""" template_dir = os.path.join(TEMPLATE_DIR, "static") context = {"first": "A", "second": "B"} with self.assertLogs("ionit", level="ERROR") as context_manager: counting_filename = os.path.join(template_dir, "counting") permission_error = PermissionError(13, "Permission denied") with mock_open(counting_filename, exception=permission_error, complain=False): self.assertEqual(render_templates(template_dir, context, "jinja", "utf-8"), 1) self.assertFalse(os.path.exists(os.path.join(template_dir, "counting"))) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], ( r"ERROR:ionit:Failed to write rendered template to " r"'\S*template/static/counting': \[Errno 13\] Permission denied" ), )