import os import jinja2 from pathlib import Path from bopytex.worker.generate import tpl2tex, generate import pytest def test_tpl2tex(): tpl = "Plop {{a}}" jinja2_tpl = jinja2.Template(tpl) fed = tpl2tex(jinja2_tpl, metas={"a": 1}) assert fed == "Plop 1" @pytest.fixture def template_path(tmp_path): template = tmp_path / "template.j2" with open(template, "w") as tpl: tpl.write("Plop {{ a }}") return template @pytest.fixture def jinja2_env(tmp_path): templateEnv = jinja2.Environment(loader=jinja2.FileSystemLoader(tmp_path)) return templateEnv def test_generate(template_path, jinja2_env): tmp_path = template_path.parent os.chdir(tmp_path) assert template_path.exists template = str(template_path.name) output = "output" message = generate( args={"a": 2, "jinja2": {"environment": jinja2_env}}, deps=[template], output=output, ) print(message.err) assert message.status == 0 assert message.out == ["GENERATE - template.j2 to output"] with open(output, "r") as out: lines = out.readlines() assert lines == ["Plop 2"]