Feat: add test and pyPath to pathlib

This commit is contained in:
Bertrand Benjamin 2019-12-22 17:30:23 +01:00
parent ab850bcedb
commit 6a130d0086
3 changed files with 73 additions and 8 deletions

View File

@ -9,7 +9,8 @@ import logging
import math as m import math as m
import subprocess import subprocess
import random as rd import random as rd
from path import Path from pathlib import Path
import os
from .texenv import * from .texenv import *
from .latex_error_parser import generic_sink, filter_errors from .latex_error_parser import generic_sink, filter_errors
@ -67,7 +68,7 @@ def feed(template, data, output="", force=0):
logger.error(f"{output} exists. Use force=1 do override it") logger.error(f"{output} exists. Use force=1 do override it")
raise ValueError(f"{output} exists. Use force=1 do override it") raise ValueError(f"{output} exists. Use force=1 do override it")
output_dir = output_p.dirname() 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_p()
@ -84,11 +85,11 @@ def pdflatex(latex_file, output_dir=""):
If output_dir is not set, it produce it next to the latex file. If output_dir is not set, it produce it next to the latex file.
""" """
if not output_dir: if not output_dir:
output_dir = Path(latex_file).dirname().abspath() output_dir = Path(latex_file).parent.resolve()
logger.debug(f"output_dir for pdflatex is {output_dir}") logger.debug(f"output_dir for pdflatex is {output_dir}")
pwd = Path('./').abspath() prev_cwd = Path.cwd()
Path(output_dir).cd() os.chdir(output_dir)
compilation = subprocess.Popen( compilation = subprocess.Popen(
[ [
"pdflatex", "pdflatex",
@ -106,9 +107,11 @@ def pdflatex(latex_file, output_dir=""):
latex_error_logger = filter_errors(generic_sink(logger.error)) latex_error_logger = filter_errors(generic_sink(logger.error))
for line in compilation.stdout: for line in compilation.stdout:
latex_error_logger.send(line.decode("latin-1").rstrip('\r\n')) latex_error_logger.send(line.decode("latin-1").rstrip('\r\n'))
compilation_status = compilation.wait()
logger.debug(f"{latex_file.name} has been compiled in {output_dir}") logger.debug(f"{latex_file.name} has been compiled in {output_dir}")
pwd.cd()
os.chdir(prev_cwd)
def clean(dirname="", garbages=["*.aux", "*.log"]): def clean(dirname="", garbages=["*.aux", "*.log"]):
@ -116,10 +119,10 @@ def clean(dirname="", garbages=["*.aux", "*.log"]):
if not dirname: if not dirname:
dirname = Path("./") dirname = Path("./")
for g in garbages: for g in garbages:
g_files = Path(dirname).files(g) g_files = Path(dirname).glob(g)
logger.debug(f"Remove {g_files}") logger.debug(f"Remove {g_files}")
for g_file in g_files: for g_file in g_files:
g_file.remove() g_file.unlink()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

47
tests/test_pytex.py Normal file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env python
# encoding: utf-8
from pytex import feed, pdflatex, clean
import os
TESTDIR = "tests"
GOOD_TEMPLATE = TESTDIR + "/tpl_good.tex"
WRONG_TEMPLATE = TESTDIR + "/tpl_wrong.tex"
GOOD_DATA = {
"name": "Bob",
"repetitions": 4,
}
def test_feed_once():
output = feed(GOOD_TEMPLATE, GOOD_DATA)
os.system(f"rm {output}")
def test_feed_twice():
output1 = feed(GOOD_TEMPLATE, GOOD_DATA)
output2 = feed(GOOD_TEMPLATE, GOOD_DATA)
os.system(f"rm {output1}")
os.system(f"rm {output2}")
def test_feed_output():
dest = "./tests/special_name.tex"
output = feed(GOOD_TEMPLATE, GOOD_DATA, dest)
assert(output.samefile(dest))
os.system(f"rm {output}")
def test_feed_output_noforce():
pass
# output = feed(GOOD_TEMPLATE, GOOD_DATA, )
# os.system(f"rm {output}")
def test_feed_pdflatex():
latex_file = feed(GOOD_TEMPLATE, GOOD_DATA)
pdflatex(latex_file)
os.system(f"rm {latex_file}")
clean(TESTDIR, ["*.aux", "*.log", "*.pdf"])
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

15
tests/tpl_good.tex Normal file
View File

@ -0,0 +1,15 @@
\documentclass{article}
\usepackage[utf8]{inputenc} % Unicode support (Umlauts etc.)
\usepackage[french]{babel} % Change hyphenation rules
\begin{document}
Coucou comment allez vous?
Je m'appelle \Var{name}.
%- for j in range(repetitions)
Je peux faire \Var{j} \\
%- endfor
\end{document}