Skip to content

Commit

Permalink
preflight: more unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
norbusan committed Sep 1, 2024
1 parent 271d6a8 commit da50ba3
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 74 deletions.
53 changes: 0 additions & 53 deletions preflight_parser/tests/DISABLED_test_tex_inspction.py

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions preflight_parser/tests/fixture/include_1/NOBEL_PRIZE_WINNER.TEX
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%! Author = ntai
%! Date = 2/14/24

% Preamble
\documentclass[11pt]{article}

% Packages
\usepackage{amsmath}
\usepackage{graphicx}

% Document
\begin{document}

Hello World
\includegraphics{ImageOfNobelPrize.jpg}



\end{document}
79 changes: 58 additions & 21 deletions preflight_parser/tests/test_base_models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import os
import unittest

from tex_inspection import ZeroZeroReadMe, find_primary_tex
from preflight_parser import generate_preflight_response, PreflightResponse, PreflightStatus, PreflightStatusValues, \
IncludeSpec, FileType, TEX_EXTENSIONS, IndexProcessSpec, IndexCompiler, BibProcessSpec, BibCompiler, \
CompilerSpec
from preflight_parser import (
TEX_EXTENSIONS,
BibCompiler,
BibProcessSpec,
CompilerSpec,
FileType,
IncludeSpec,
IndexCompiler,
IndexProcessSpec,
PreflightStatus,
PreflightStatusValues,
)


class TestBaseModels(unittest.TestCase):
def test_preflightstatus(self):
pfs = PreflightStatus(key=PreflightStatusValues.success, info="Something")
ret = pfs.model_dump_json(indent=4, exclude_none=True, exclude_defaults=True)
exp = \
"""{
exp = """{
"key": "success",
"info": "Something"
}"""
Expand All @@ -20,8 +27,7 @@ def test_preflightstatus(self):
def test_includespec_1(self):
inc = IncludeSpec(cmd="input", source="core", type=FileType.tex, extensions=TEX_EXTENSIONS, take_options=False)
ret = inc.model_dump_json(indent=4, exclude_none=True, exclude_defaults=True)
exp = \
"""{
exp = """{
"cmd": "input",
"source": "core",
"type": "tex",
Expand All @@ -35,8 +41,7 @@ def test_includespec_2(self):
cmd="usepackage", source="core", type=FileType.tex, extensions="sty", take_options=True, multi_args=True
)
ret = inc.model_dump_json(indent=4, exclude_none=True, exclude_defaults=True)
exp = \
"""{
exp = """{
"cmd": "usepackage",
"source": "core",
"type": "tex",
Expand All @@ -48,8 +53,7 @@ def test_includespec_2(self):
def test_indexprocessorspec(self):
ips = IndexProcessSpec(processor=IndexCompiler.makeindex, pre_generated=True)
ret = ips.model_dump_json(indent=4, exclude_none=True, exclude_defaults=True)
exp = \
"""{
exp = """{
"processor": "makeindex",
"pre_generated": true
}"""
Expand All @@ -58,31 +62,64 @@ def test_indexprocessorspec(self):
def test_bibprocessorspec(self):
ips = BibProcessSpec(processor=BibCompiler.biber, pre_generated=False)
ret = ips.model_dump_json(indent=4, exclude_none=True, exclude_defaults=True)
exp = \
"""{
exp = """{
"processor": "biber",
"pre_generated": false
}"""
self.assertEqual(exp, ret)

def test_compilerspec(self):
def test_compilerspec_1(self):
"""Test CompilerSpec initialized from compiler string."""
self.assertEqual(
CompilerSpec(compiler="etex+dvipdfmx").model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"tex","output":"dvi","postp":"dvipdfmx"}"""
"""{"engine":"tex","lang":"tex","output":"dvi","postp":"dvipdfmx"}""",
)
self.assertEqual(
CompilerSpec(compiler="pdflatex").model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"latex","output":"pdf","postp":"none"}"""
"""{"engine":"tex","lang":"latex","output":"pdf","postp":"none"}""",
)
self.assertEqual(
CompilerSpec(compiler="etex+dvips_ps2pdf").model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"tex","output":"dvi","postp":"dvips_ps2pdf"}"""
"""{"engine":"tex","lang":"tex","output":"dvi","postp":"dvips_ps2pdf"}""",
)
self.assertEqual(
CompilerSpec(compiler="latex+dvips_ps2pdf").model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"latex","output":"dvi","postp":"dvips_ps2pdf"}"""
"""{"engine":"tex","lang":"latex","output":"dvi","postp":"dvips_ps2pdf"}""",
)
self.assertEqual(
CompilerSpec(compiler="pdf_submission").model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"unknown","lang":"pdf","output":"unknown","postp":"none"}"""
"""{"engine":"unknown","lang":"pdf","output":"unknown","postp":"none"}""",
)

def test_compilerspec_2(self):
"""Test CompilerSpec initialized from dict of data."""
self.assertEqual(
CompilerSpec(engine="tex", lang="tex", output="dvi", postp="dvipdfmx").model_dump_json(
exclude_none=True, exclude_defaults=True
),
"""{"engine":"tex","lang":"tex","output":"dvi","postp":"dvipdfmx"}""",
)
self.assertEqual(
CompilerSpec(engine="tex", lang="latex", output="pdf", postp="none").model_dump_json(
exclude_none=True, exclude_defaults=True
),
"""{"engine":"tex","lang":"latex","output":"pdf","postp":"none"}""",
)
self.assertEqual(
CompilerSpec(engine="tex", lang="tex", output="dvi", postp="dvips_ps2pdf").model_dump_json(
exclude_none=True, exclude_defaults=True
),
"""{"engine":"tex","lang":"tex","output":"dvi","postp":"dvips_ps2pdf"}""",
)
self.assertEqual(
CompilerSpec(engine="tex", lang="latex", output="dvi", postp="dvips_ps2pdf").model_dump_json(
exclude_none=True, exclude_defaults=True
),
"""{"engine":"tex","lang":"latex","output":"dvi","postp":"dvips_ps2pdf"}""",
)
self.assertEqual(
CompilerSpec(engine="unknown", lang="pdf", output="unknown", postp="none").model_dump_json(
exclude_none=True, exclude_defaults=True
),
"""{"engine":"unknown","lang":"pdf","output":"unknown","postp":"none"}""",
)
83 changes: 83 additions & 0 deletions preflight_parser/tests/test_preflight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
import unittest

from preflight_parser import PreflightResponse, generate_preflight_response


class TestPreflight(unittest.TestCase):
def setUp(self):
self.fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "fixture"))

def test_preflight_0(self):
"""Test for empty/missing directory response."""
dir_path = "/no/such/directory/should/exist"
pf: PreflightResponse = generate_preflight_response(dir_path)
self.assertEqual(pf.status.key.value, "error")
self.assertEqual(pf.status.info, "No TeX files found")
self.assertEqual(len(pf.detected_toplevel_files), 0)
self.assertEqual(len(pf.tex_files), 0)

def test_preflight_1(self):
dir_path = os.path.join(self.fixture_dir, "include_1")
pf: PreflightResponse = generate_preflight_response(dir_path)
self.assertEqual(pf.status.key.value, "success")
self.assertEqual(len(pf.detected_toplevel_files), 1)
self.assertEqual(pf.tex_files[0].used_other_files, ["ImageOfNobelPrize.jpg"])
# check for correct detection of output==pdf when a jpg is included
self.assertEqual(
pf.detected_toplevel_files[0].process.compiler.model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"latex","output":"pdf","postp":"none"}""",
)

def test_preflight_single_tex_1(self):
dir_path = os.path.join(self.fixture_dir, "single_tex_1")
pf: PreflightResponse = generate_preflight_response(dir_path)
self.assertEqual(pf.status.key.value, "success")
self.assertEqual(len(pf.detected_toplevel_files), 1)
self.assertEqual(
pf.detected_toplevel_files[0].process.compiler.model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"latex","output":"pdf","postp":"none"}""",
)

def test_preflight_single_tex_2(self):
dir_path = os.path.join(self.fixture_dir, "single_tex_2")
pf: PreflightResponse = generate_preflight_response(dir_path)
self.assertEqual(pf.status.key.value, "success")
# we do NOT check for 00README entries, so nothing is ignored
self.assertEqual(len(pf.detected_toplevel_files), 3)
self.assertEqual(
pf.detected_toplevel_files[0].process.compiler.model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"latex","output":"pdf","postp":"none"}""",
)

def test_preflight_single_tex_3(self):
"""Test recursive inclusion."""
dir_path = os.path.join(self.fixture_dir, "single_tex_3")
pf: PreflightResponse = generate_preflight_response(dir_path)
self.assertEqual(pf.status.key.value, "success")
self.assertEqual(len(pf.detected_toplevel_files), 1)
self.assertEqual(
pf.detected_toplevel_files[0].process.compiler.model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"latex","output":"pdf","postp":"none"}""",
)
for tf in pf.tex_files:
if tf.filename == "fake-file-1.tex":
self.assertEqual(tf.used_tex_files, ["fake-file-2.TEX", "fake-file-3.tex"])
self.assertEqual(tf.language.value, "latex")

def test_preflight_single_tex_4(self):
"""Test recursive inclusion."""
dir_path = os.path.join(self.fixture_dir, "single_tex_4")
pf: PreflightResponse = generate_preflight_response(dir_path)
self.assertEqual(pf.status.key.value, "success")
self.assertEqual(len(pf.detected_toplevel_files), 2)
self.assertEqual(
pf.detected_toplevel_files[0].process.compiler.model_dump_json(exclude_none=True, exclude_defaults=True),
"""{"engine":"tex","lang":"latex","output":"pdf","postp":"none"}""",
)
for tf in pf.tex_files:
if tf.filename == "fake-file-1.tex":
self.assertEqual(tf.used_tex_files, ["fake-file-2.TEX", "fake-file-3.tex"])
self.assertEqual(tf.issues[0].info, "fake-package.sty")
self.assertEqual(tf.issues[0].key.value, "file_not_found")
self.assertEqual(tf.language.value, "latex")

0 comments on commit da50ba3

Please sign in to comment.