fix tests

This commit is contained in:
Sina Atalay 2023-11-18 17:51:13 +01:00
parent 05930e0d80
commit fdd95d9c88
2 changed files with 57 additions and 53 deletions

View File

@ -444,35 +444,35 @@ def run_latex(latex_file_path: str) -> str:
) )
# Run TinyTeX: # Run TinyTeX:
with subprocess.Popen( def run():
[ with subprocess.Popen(
executable, [
f"{latex_file_name}", executable,
"&&", f"{latex_file_name}",
executable, ],
f"{latex_file_name}", cwd=os.path.dirname(latex_file_path),
], stdout=subprocess.PIPE,
cwd=os.path.dirname(latex_file_path), stdin=subprocess.DEVNULL, # don't allow TinyTeX to ask for user input
stdout=subprocess.PIPE, text=True,
stdin=subprocess.DEVNULL, # don't allow TinyTeX to ask for user input ) as latex_process:
text=True, output, error = latex_process.communicate()
shell=True,
) as latex_process:
output, error = latex_process.communicate()
if latex_process.returncode != 0: if latex_process.returncode != 0:
# Find the error line: # Find the error line:
for line in output.split("\n"): for line in output.split("\n"):
if line.startswith("! "): if line.startswith("! "):
error_line = line.replace("! ", "") error_line = line.replace("! ", "")
break break
raise RuntimeError( raise RuntimeError(
"Running TinyTeX has failed with the following error:", "Running TinyTeX has failed with the following error:",
f"{error_line}", f"{error_line}",
"If you can't solve the problem, please try to re-install RenderCV," "If you can't solve the problem, please try to re-install RenderCV,"
" or open an issue on GitHub.", " or open an issue on GitHub.",
) )
run()
run() # run twice for cross-references
# check if the PDF file is generated: # check if the PDF file is generated:
if not os.path.exists(output_file_path): if not os.path.exists(output_file_path):

View File

@ -12,35 +12,39 @@ class TestCLI(unittest.TestCase):
# Change the working directory to the root of the project: # Change the working directory to the root of the project:
workspace_path = os.path.dirname(os.path.dirname(__file__)) workspace_path = os.path.dirname(os.path.dirname(__file__))
test_input_file_path = os.path.join( with self.subTest(msg="Correct input"):
workspace_path, test_input_file_path = os.path.join(
"tests", workspace_path,
"reference_files", "tests",
"John_Doe_CV_yaml_reference.yaml", "reference_files",
) "John_Doe_CV_yaml_reference.yaml",
subprocess.run( )
[sys.executable, "-m", "rendercv", "render", test_input_file_path], subprocess.run(
check=True, [sys.executable, "-m", "rendercv", "render", test_input_file_path],
) check=True,
)
# Read the necessary information and remove the output directory: # Read the necessary information and remove the output directory:
output_file_path = os.path.join(workspace_path, "output", "John_Doe_CV.pdf") output_file_path = os.path.join(workspace_path, "output", "John_Doe_CV.pdf")
pdf_file_size = os.path.getsize(output_file_path) pdf_file_size = os.path.getsize(output_file_path)
file_exists = os.path.exists(output_file_path) file_exists = os.path.exists(output_file_path)
shutil.rmtree(os.path.join(workspace_path, "output")) shutil.rmtree(os.path.join(workspace_path, "output"))
# Check if the output file exists: # Check if the output file exists:
self.assertTrue(file_exists, msg="PDF file couldn't be generated.") self.assertTrue(file_exists, msg="PDF file couldn't be generated.")
# Compare the pdf file with the reference pdf file: # Compare the pdf file with the reference pdf file:
reference_pdf_file = os.path.join( reference_pdf_file = os.path.join(
workspace_path, "tests", "reference_files", "John_Doe_CV_pdf_reference.pdf" workspace_path,
) "tests",
reference_pdf_file_size = os.path.getsize(reference_pdf_file) "reference_files",
ratio = min(reference_pdf_file_size, pdf_file_size) / max( "John_Doe_CV_pdf_reference.pdf",
reference_pdf_file_size, pdf_file_size )
) reference_pdf_file_size = os.path.getsize(reference_pdf_file)
self.assertTrue(ratio > 0.98, msg="PDF file didn't match the reference.") ratio = min(reference_pdf_file_size, pdf_file_size) / max(
reference_pdf_file_size, pdf_file_size
)
self.assertTrue(ratio > 0.98, msg="PDF file didn't match the reference.")
# Wrong input: # Wrong input:
with self.subTest(msg="Wrong input"): with self.subTest(msg="Wrong input"):