From 9d5c231c9cb88646efcb2c79cdb3a59cee0d5c25 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 23 Dec 2019 15:05:50 +0100 Subject: [PATCH] Feat: Add test! snippet compilation failed thow... --- bopytex/__init__.py | 3 +- bopytex/bopytex.py | 20 +++--- test/templates/tpl_test.tex | 22 +++++++ test/test_bopytex.py | 124 ++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 test/templates/tpl_test.tex create mode 100644 test/test_bopytex.py diff --git a/bopytex/__init__.py b/bopytex/__init__.py index 9721eec..15567b7 100644 --- a/bopytex/__init__.py +++ b/bopytex/__init__.py @@ -2,7 +2,8 @@ # encoding: utf-8 -__version__ = "0.1" +from .bopytex import produce_and_compile + # ----------------------------- # Reglages pour 'vim' diff --git a/bopytex/bopytex.py b/bopytex/bopytex.py index 9542558..dcb0fdd 100755 --- a/bopytex/bopytex.py +++ b/bopytex/bopytex.py @@ -100,7 +100,11 @@ def pdfjoin(pdf_files, destname, working_dir=".", rm_pdfs=1): def extract_student_csv(csv_filename): - """ Extract student list from csv_filename """ + """ Extract student list from csv_filename + + Student identifier is got in the column "Élève". + + """ with open(csv_filename, "r") as csvfile: reader = csv.DictReader(csvfile) return [r["Élève"] for r in reader] @@ -113,7 +117,7 @@ def produce_and_compile(options): if options["only_corr"]: options["corr"] = True - tex_files = working_dir.files("[0-9]*_*.tex") + tex_files = working_dir.glop("[0-9]*_*.tex") else: template = Path(options["template"]).name logger.debug(f"Template will be {template}") @@ -152,16 +156,16 @@ def produce_and_compile(options): ) logger.debug(f"{dest} fed") - if not options["no_compil"]: + if not options["no_compile"]: pdf_files = [] for texfile in tex_files: logger.debug(f"Start compiling {texfile}") pytex.pdflatex(texfile) logger.debug(f"End compiling {texfile}") - pdf_files.append(str(texfile[:-4] + ".pdf")) + pdf_files.append(str(texfile).split('.')[0] + ".pdf") logger.debug(f"Compiled files : {pdf_files}") - if not options["no_join"] and not options["no_compil"]: + if not options["no_join"] and not options["no_compile"]: pdfjoin( pdf_files, template.replace("tpl", "all").replace(".tex", ".pdf"), @@ -173,14 +177,14 @@ def produce_and_compile(options): pdf_files = [] for texfile in tex_files: corr_fname = activate_printanswers(texfile) - if not options["no_compil"]: + if not options["no_compile"]: logger.debug(f"Start compiling {texfile}") pytex.pdflatex(corr_fname) logger.debug(f"End compiling {texfile}") - pdf_files.append(str(corr_fname[:-4] + ".pdf")) + pdf_files.append(str(corr_fname).split(".")[0] + ".pdf") deactivate_printanswers(corr_fname) - if not options["no_join"] and not options["no_compil"]: + if not options["no_join"] and not options["no_compile"]: pdfjoin( pdf_files, template.replace("tpl", "corr").replace(".tex", ".pdf"), diff --git a/test/templates/tpl_test.tex b/test/templates/tpl_test.tex new file mode 100644 index 0000000..dc3183b --- /dev/null +++ b/test/templates/tpl_test.tex @@ -0,0 +1,22 @@ +\documentclass[12pt]{article} +\usepackage[utf8x]{inputenc} +\usepackage[francais]{babel} +\usepackage[T1]{fontenc} +\usepackage{amssymb} +\usepackage{amsmath} +\usepackage{amsfonts} + + +\title{ + Tests +} +\author{ + Benjamin Bertrand +} + +\begin{document} +\maketitle + + + +\end{document} diff --git a/test/test_bopytex.py b/test/test_bopytex.py new file mode 100644 index 0000000..151c270 --- /dev/null +++ b/test/test_bopytex.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +import pytest +import os +from pathlib import Path +from shutil import copyfile +from bopytex import produce_and_compile + +SNIPPETS_PATH = Path("snippets/") +TEST_TEMPLATE_PATH = Path("test/templates/") + + +@pytest.fixture +def prepare_test_template(tmp_path): + """ Create a tmp directory, copy snippets inside + + return tmp directory name + """ + tmp = tmp_path + snippets = TEST_TEMPLATE_PATH.glob("tpl_*.tex") + for s in snippets: + copyfile(s, tmp / s.name) + + prev_dir = Path.cwd() + os.chdir(tmp) + yield tmp + os.chdir(prev_dir) + + +@pytest.fixture +def prepare_snippets(tmp_path): + """ Create a tmp directory, copy snippets inside + + return tmp directory name + """ + tmp = tmp_path + snippets = SNIPPETS_PATH.glob("tpl_*.tex") + for s in snippets: + copyfile(s, tmp / s.name) + + prev_dir = Path.cwd() + os.chdir(tmp) + yield tmp + os.chdir(prev_dir) + + +def test_produce_and_compile_base(prepare_test_template): + test_tpl = list(Path(".").glob("tpl_*.tex")) + assert [tpl.name for tpl in test_tpl] == ["tpl_test.tex"] + for tpl in test_tpl: + produce_and_compile( + { + "template": tpl, + "working_dir": None, + "only_corr": False, + "students_csv": None, + "number_subjects": 1, + "dirty": False, + "no_compile": False, + "no_join": False, + "corr": False, + "crazy": False, + } + ) + + +def test_pdfjoin_current_directory(prepare_test_template): + wdir = prepare_test_template + pass + + +def test_pdfjoin_deep_directory(): + pass + + +def test_pdfjoin_dont_remove(): + pass + + +def test_subject_names(): + pass + + +def test_feed_texfiles(): + pass + + +def test_tex2pdf_current_directory(): + pass + + +def test_tex2pdf_deep_directory(): + pass + + +def test_activate_solution(): + pass + + +def test_snippets(prepare_snippets): + snippets = list(Path(".").glob("tpl_*.tex")) + for tpl in snippets: + produce_and_compile( + { + "template": tpl, + "working_dir": None, + "only_corr": False, + "students_csv": None, + "number_subjects": 1, + "dirty": False, + "no_compile": False, + "no_join": False, + "corr": False, + "crazy": False, + } + ) + + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del