Example #1
0
    def test_22_render__w_wrong_tpath(self):
        ng_t = pathlib.Path(self.workdir) / "ng.j2"
        ok_t = pathlib.Path(self.workdir) / "ok.j2"
        ok_t_content = "a: {{ a }}"
        ok_content = "a: aaa"
        ctx = dict(a="aaa", )

        ok_t.write_text(ok_t_content)

        with unittest.mock.patch("builtins.input") as mock_input:
            mock_input.return_value = str(ok_t)
            c_r = TT.render(str(ng_t), ctx, ask=True)
            self.assertEqual(c_r, ok_content)
        try:
            TT.render(str(ng_t), ctx, ask=False)
            assert False  # force raising an exception.
        except TT.jinja2.TemplateNotFound:
            pass
Example #2
0
    def test_24_render__wo_paths(self):
        fname = self.templates[0][0]
        workdir = pathlib.Path(self.workdir)

        assert workdir / fname

        subdir = workdir / "a/b/c"
        subdir.mkdir(parents=True)

        tmpl = subdir / fname
        tmpl.write_text("{{ a|default('aaa') }}")

        c_r = TT.render(str(tmpl))
        self.assertEqual(c_r, "aaa")
Example #3
0
    def test_25_render__w_prefer_paths(self):
        workdir = pathlib.Path(self.workdir / 'a' / 'b' / 'c')
        workdir.mkdir(parents=True)

        assert workdir.exists()

        tmpl_ref = pathlib.Path(self.workdir / 'ref_25_0.j2')
        tmpl_ref.write_text("{{ a | d('A') }}\n")

        tmpl = pathlib.Path(workdir / 'd.j2')
        tmpl.write_text("{{ a | d('xyz') }}\n")

        c_r = TT.render(tmpl, paths=[self.workdir])
        self.assertNotEqual(c_r, 'A')
        self.assertEqual(c_r, 'xyz')
Example #4
0
 def test_20_render__wo_paths(self):
     for fname, _str, ctx in self.templates:
         fpath = pathlib.Path(self.workdir) / fname
         c_r = TT.render(str(fpath))
         self.assertEqual(c_r, ctx)
Example #5
0
 def test_40_render__w_filter(self):
     fname, _, ctx = TMPL_WITH_FILTER
     fpath = pathlib.Path(self.workdir) / fname
     c_r = TT.render(str(fpath), filters={"negate": negate})
     self.assertEqual(c_r, ctx)