Commit 5d77c76b by Jörg Stucke

review changes + refactoring

parent 9ac7f0ee
...@@ -20,8 +20,9 @@ ...@@ -20,8 +20,9 @@
import json import json
import shutil import shutil
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory
from sys import exit as sys_exit from sys import exit as sys_exit
from tempfile import TemporaryDirectory
from pdf_generator.generator import compile_pdf, create_templates from pdf_generator.generator import compile_pdf, create_templates
......
import os import os
import shutil import shutil
import sys
from pathlib import Path 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 ( 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 PDF_NAME = Path(MAIN_TEMPLATE).with_suffix('.pdf').name
...@@ -13,7 +15,10 @@ 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): def execute_latex(tmp_dir):
current_dir = os.getcwd() current_dir = os.getcwd()
os.chdir(tmp_dir) 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) os.chdir(current_dir)
......
from pathlib import Path
import shutil
import PyPDF2 import PyPDF2
from docker_entry import main as main_docker_entry from docker_entry import main as main_docker_entry
import pathlib
import shutil
# pylint: disable=redefined-outer-name # 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'): def test_docker_entry():
pathlib.Path("/tmp/interface/data").mkdir(parents=True, exist_ok=True) (INTERFACE_DIR / 'data').mkdir(parents=True, exist_ok=True)
pathlib.Path("/tmp/interface/pdf").mkdir(parents=True, exist_ok=True) (INTERFACE_DIR / 'pdf').mkdir(parents=True, exist_ok=True)
shutil.copyfile('data/analysis.json', '/tmp/interface/data/analysis.json') shutil.copyfile(TEST_ANALYSIS_DATA, INTERFACE_DIR / 'data' / 'analysis.json')
shutil.copyfile('data/meta.json', '/tmp/interface/data/meta.json') shutil.copyfile(TEST_META_DATA, INTERFACE_DIR / 'data' / 'meta.json')
output = main_docker_entry() output = main_docker_entry()
assert output == 0, 'LaTeX PDF build unsuccessful'
assert OUTPUT_FILE.is_file()
try: 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: except PyPDF2.utils.PdfReadError:
assert False assert False, 'PDF could not be read'
assert pathlib.Path('/tmp/interface/pdf/A_devices_name_analysis_report.pdf').exists()
assert output == 0
...@@ -2,11 +2,11 @@ import json ...@@ -2,11 +2,11 @@ import json
from pathlib import Path from pathlib import Path
import pytest import pytest
from pdf_generator.generator import ( from pdf_generator.generator import (
LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE, copy_fact_image, create_report_filename, copy_fact_image, create_report_filename, create_templates, execute_latex, LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE
create_templates, execute_latex
) )
from test.data.test_dict import TEST_DICT, META_DICT from test.data.test_dict import META_DICT, TEST_DICT
class MockEngine: class MockEngine:
...@@ -15,7 +15,7 @@ class MockEngine: ...@@ -15,7 +15,7 @@ class MockEngine:
@staticmethod @staticmethod
def render_main_template(analysis): def render_main_template(analysis):
return '{}'.format(json.dumps(analysis)) return json.dumps(analysis)
@staticmethod @staticmethod
def render_meta_template(meta_data): def render_meta_template(meta_data):
...@@ -32,10 +32,11 @@ class MockEngine: ...@@ -32,10 +32,11 @@ class MockEngine:
def exec_mock(*_, **__): def exec_mock(*_, **__):
Path('test').write_text('works') Path('test').write_text('works')
return '', 0
def test_execute_latex(monkeypatch, tmpdir): 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)) execute_latex(str(tmpdir))
assert Path(str(tmpdir), 'test').exists() 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