Feat: test in tmp dir and few bug correction

This commit is contained in:
Bertrand Benjamin 2019-12-23 14:29:15 +01:00
parent 86d523d222
commit 737d64e0a8
3 changed files with 35 additions and 22 deletions

View File

@ -71,7 +71,7 @@ def feed(template, data, output="", force=0):
output_dir = output_p.parent output_dir = output_p.parent
if output_dir and not output_dir.exists(): if output_dir and not output_dir.exists():
logger.debug(f"Creating output dir {output_dir}") logger.debug(f"Creating output dir {output_dir}")
output_dir.mkdir_p() output_dir.mkdir(exist_ok=True)
with open(output_p, "w") as output_f: with open(output_p, "w") as output_f:
output_f.write(tpl.render(**EXPORT_DICT, **data)) output_f.write(tpl.render(**EXPORT_DICT, **data))

View File

@ -24,7 +24,6 @@ texenv = jinja2.Environment(
line_statement_prefix='%-', line_statement_prefix='%-',
line_comment_prefix='%#', line_comment_prefix='%#',
loader=jinja2.ChoiceLoader([ loader=jinja2.ChoiceLoader([
# jinja2.PackageLoader("notes_tools.reports", "templates"),
jinja2.FileSystemLoader(['./']), jinja2.FileSystemLoader(['./']),
]), ]),
extensions=['jinja2.ext.do'] extensions=['jinja2.ext.do']

View File

@ -3,42 +3,56 @@
from pytex import feed, pdflatex, clean from pytex import feed, pdflatex, clean
import os import os
from shutil import copyfile
from pathlib import Path
import pytest
TESTDIR = "tests" TESTDIR = Path("tests")
GOOD_TEMPLATE = TESTDIR + "/tpl_good.tex" GOOD_TEMPLATE = TESTDIR / "tpl_good.tex"
WRONG_TEMPLATE = TESTDIR + "/tpl_wrong.tex"
GOOD_DATA = { GOOD_DATA = {
"name": "Bob", "name": "Bob",
"repetitions": 4, "repetitions": 4,
} }
def test_feed_once():
output = feed(GOOD_TEMPLATE, GOOD_DATA)
os.system(f"rm {output}")
def test_feed_twice(): @pytest.fixture()
output1 = feed(GOOD_TEMPLATE, GOOD_DATA) def prepare_templates(tmpdir):
output2 = feed(GOOD_TEMPLATE, GOOD_DATA) wdir = tmpdir
os.system(f"rm {output1}") good_tpl = Path(GOOD_TEMPLATE)
os.system(f"rm {output2}") copyfile(good_tpl, wdir / good_tpl.name)
prev_cwd = Path.cwd()
def test_feed_output(): os.chdir(wdir)
yield wdir
os.chdir(prev_cwd)
def test_feed_once(prepare_templates):
output = feed(GOOD_TEMPLATE.name, GOOD_DATA)
def test_feed_twice(prepare_templates):
output1 = feed(GOOD_TEMPLATE.name, GOOD_DATA)
output2 = feed(GOOD_TEMPLATE.name, GOOD_DATA)
def test_feed_output(prepare_templates):
dest = "./tests/special_name.tex" dest = "./tests/special_name.tex"
output = feed(GOOD_TEMPLATE, GOOD_DATA, dest) output = feed(GOOD_TEMPLATE.name, GOOD_DATA, dest)
assert(output.samefile(dest)) assert output.samefile(dest)
os.system(f"rm {output}")
def test_feed_output_noforce(): def test_feed_output_noforce():
pass pass
# output = feed(GOOD_TEMPLATE, GOOD_DATA, ) # output = feed(GOOD_TEMPLATE, GOOD_DATA, )
# os.system(f"rm {output}") # os.system(f"rm {output}")
def test_feed_pdflatex():
latex_file = feed(GOOD_TEMPLATE, GOOD_DATA) def test_feed_pdflatex(prepare_templates):
latex_file = feed(GOOD_TEMPLATE.name, GOOD_DATA)
pdflatex(latex_file) pdflatex(latex_file)
os.system(f"rm {latex_file}") clean(garbages=["*.aux", "*.log", "*.pdf"])
clean(TESTDIR, ["*.aux", "*.log", "*.pdf"])
# ----------------------------- # -----------------------------