2022-04-10 13:50:58 +00:00
|
|
|
import os
|
|
|
|
import jinja2
|
2022-05-09 07:09:38 +00:00
|
|
|
from bopytex.worker.generate import generate
|
2022-04-10 13:50:58 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2022-05-08 07:45:45 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def jinja2_env(tmp_path):
|
|
|
|
templateEnv = jinja2.Environment(loader=jinja2.FileSystemLoader(tmp_path))
|
|
|
|
return templateEnv
|
|
|
|
|
|
|
|
|
2022-04-10 13:50:58 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def template_path(tmp_path):
|
|
|
|
template = tmp_path / "template.j2"
|
|
|
|
with open(template, "w") as tpl:
|
|
|
|
tpl.write("Plop {{ a }}")
|
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2022-04-13 10:26:04 +00:00
|
|
|
message = generate(
|
2022-04-10 13:50:58 +00:00
|
|
|
args={"a": 2, "jinja2": {"environment": jinja2_env}},
|
|
|
|
deps=[template],
|
|
|
|
output=output,
|
2022-04-13 10:26:04 +00:00
|
|
|
)
|
|
|
|
print(message.err)
|
|
|
|
assert message.status == 0
|
|
|
|
assert message.out == ["GENERATE - template.j2 to output"]
|
|
|
|
|
2022-04-10 13:50:58 +00:00
|
|
|
with open(output, "r") as out:
|
|
|
|
lines = out.readlines()
|
|
|
|
assert lines == ["Plop 2"]
|
2022-05-08 07:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def template_path_with_random(tmp_path):
|
|
|
|
template = tmp_path / "template.j2"
|
|
|
|
with open(template, "w") as tpl:
|
|
|
|
tpl.write("Plop {{ random.randint(0, 10) }}")
|
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
|
|
def test_generate_with_random(template_path_with_random, jinja2_env):
|
|
|
|
tmp_path = template_path_with_random.parent
|
|
|
|
os.chdir(tmp_path)
|
|
|
|
|
|
|
|
assert template_path_with_random.exists
|
|
|
|
template = str(template_path_with_random.name)
|
|
|
|
output = "output"
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
message = generate(
|
|
|
|
args={"random": random, "jinja2": {"environment": jinja2_env}},
|
|
|
|
deps=[template],
|
|
|
|
output=output,
|
|
|
|
)
|
|
|
|
print(message.err)
|
|
|
|
assert message.status == 0
|
|
|
|
|
|
|
|
with open(output, "r") as out:
|
|
|
|
lines = out.readlines()
|
2022-05-09 07:11:56 +00:00
|
|
|
assert int(lines[0][-1]) >= 0
|
2022-05-08 07:45:45 +00:00
|
|
|
assert int(lines[0][-1]) < 10
|