Commit 5d77c76b by Jörg Stucke

review changes + refactoring

parent 9ac7f0ee
......@@ -20,8 +20,9 @@
import json
import shutil
from pathlib import Path
from tempfile import TemporaryDirectory
from sys import exit as sys_exit
from tempfile import TemporaryDirectory
from pdf_generator.generator import compile_pdf, create_templates
......
import os
import shutil
import sys
from pathlib import Path
from common_helper_process import execute_shell_command
from common_helper_process import execute_shell_command_get_return_code
from pdf_generator.tex_generation.template_engine import (
LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE, CUSTOM_TEMPLATE_CLASS, TemplateEngine
CUSTOM_TEMPLATE_CLASS, LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE, TemplateEngine
)
PDF_NAME = Path(MAIN_TEMPLATE).with_suffix('.pdf').name
......@@ -13,7 +15,10 @@ PDF_NAME = Path(MAIN_TEMPLATE).with_suffix('.pdf').name
def execute_latex(tmp_dir):
current_dir = os.getcwd()
os.chdir(tmp_dir)
execute_shell_command('env buf_size=1000000 pdflatex {}'.format(MAIN_TEMPLATE))
output, return_code = execute_shell_command_get_return_code('env buf_size=1000000 pdflatex {}'.format(MAIN_TEMPLATE))
if return_code != 0:
print(f'Error when trying to build PDF:\n{output}')
sys.exit(1)
os.chdir(current_dir)
......
from pathlib import Path
import shutil
import PyPDF2
from docker_entry import main as main_docker_entry
import pathlib
import shutil
# pylint: disable=redefined-outer-name
INTERFACE_DIR = Path('/tmp/interface')
OUTPUT_FILE = INTERFACE_DIR / 'pdf' / 'A_devices_name_analysis_report.pdf'
TEST_META_DATA = Path(__file__).parent / 'data' / 'meta.json'
TEST_ANALYSIS_DATA = Path(__file__).parent / 'data' / 'analysis.json'
def test_docker_entry(template_style='default'):
pathlib.Path("/tmp/interface/data").mkdir(parents=True, exist_ok=True)
pathlib.Path("/tmp/interface/pdf").mkdir(parents=True, exist_ok=True)
shutil.copyfile('data/analysis.json', '/tmp/interface/data/analysis.json')
shutil.copyfile('data/meta.json', '/tmp/interface/data/meta.json')
def test_docker_entry():
(INTERFACE_DIR / 'data').mkdir(parents=True, exist_ok=True)
(INTERFACE_DIR / 'pdf').mkdir(parents=True, exist_ok=True)
shutil.copyfile(TEST_ANALYSIS_DATA, INTERFACE_DIR / 'data' / 'analysis.json')
shutil.copyfile(TEST_META_DATA, INTERFACE_DIR / 'data' / 'meta.json')
output = main_docker_entry()
assert output == 0, 'LaTeX PDF build unsuccessful'
assert OUTPUT_FILE.is_file()
try:
PyPDF2.PdfFileReader(open('/tmp/interface/pdf/A_devices_name_analysis_report.pdf', "rb"))
PyPDF2.PdfFileReader(open(str(OUTPUT_FILE), 'rb'))
except PyPDF2.utils.PdfReadError:
assert False
assert pathlib.Path('/tmp/interface/pdf/A_devices_name_analysis_report.pdf').exists()
assert output == 0
assert False, 'PDF could not be read'
......@@ -2,11 +2,11 @@ import json
from pathlib import Path
import pytest
from pdf_generator.generator import (
LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE, copy_fact_image, create_report_filename,
create_templates, execute_latex
copy_fact_image, create_report_filename, create_templates, execute_latex, LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE
)
from test.data.test_dict import TEST_DICT, META_DICT
from test.data.test_dict import META_DICT, TEST_DICT
class MockEngine:
......@@ -15,7 +15,7 @@ class MockEngine:
@staticmethod
def render_main_template(analysis):
return '{}'.format(json.dumps(analysis))
return json.dumps(analysis)
@staticmethod
def render_meta_template(meta_data):
......@@ -32,10 +32,11 @@ class MockEngine:
def exec_mock(*_, **__):
Path('test').write_text('works')
return '', 0
def test_execute_latex(monkeypatch, tmpdir):
monkeypatch.setattr('pdf_generator.generator.execute_shell_command', exec_mock)
monkeypatch.setattr('pdf_generator.generator.execute_shell_command_get_return_code', exec_mock)
execute_latex(str(tmpdir))
assert Path(str(tmpdir), 'test').exists()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment