Skip to content

Commit

Permalink
Enable ruff for tests, fix failures, format
Browse files Browse the repository at this point in the history
  • Loading branch information
norbusan committed Dec 3, 2024
1 parent e54f420 commit 66bc48d
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 69 deletions.
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pre-commit = "*"
[tool.ruff]
line-length = 120
extend-exclude = [
"tests",
"pdf_profile",
"bin",
"service-various",
Expand Down Expand Up @@ -78,13 +77,16 @@ lint.ignore = [
"D107", # no docstring in __init__
]


output-format = "grouped"

# Assume Python 3.11
# Note: helps prevent breaking autofixes from, e.g., pyupgrade
target-version = "py311"

[tool.ruff.lint.per-file-ignores]
# ignore all the "missing docstring" checks in the test directory
"tests/*" = ["D100", "D101", "D102", "D103", "D104"]

[tool.ruff.lint.pydocstyle]
convention = "pep257"

Expand Down
5 changes: 1 addition & 4 deletions tests/preflight_parser/test_preflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,4 @@ def test_preflight_pdf_only_submission(self):
pf.detected_toplevel_files[0].process.compiler.json(exclude_none=True, exclude_defaults=True),
"""{"engine": "unknown", "lang": "pdf", "output": "unknown", "postp": "none"}""",
)
self.assertEqual(
pf.detected_toplevel_files[0].process.compiler.compiler_string,
"pdf_submission"
)
self.assertEqual(pf.detected_toplevel_files[0].process.compiler.compiler_string, "pdf_submission")
83 changes: 54 additions & 29 deletions tests/service/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@
import os
import shutil
import subprocess
import tempfile
import time
import urllib.parse

import pytest
import requests

from bin.compile_submissions import get_outcome_meta_and_files_info
from tex2pdf.service.converter_driver import RemoteConverterDriver

PORT = 33031
SELF_DIR = os.path.abspath(os.path.dirname(__file__))

def submit_tarball(service: str, tarball: str, outcome_file: str, tex2pdf_timeout: int = 30, post_timeout: int = 10, json_response: bool = False, api_args: dict= {}) -> None | dict:

def submit_tarball(
service: str,
tarball: str,
outcome_file: str,
tex2pdf_timeout: int = 30,
post_timeout: int = 10,
json_response: bool = False,
api_args: dict = {},
) -> None | dict:
meta = None
params_dict = { "timeout": tex2pdf_timeout }
params_dict = {"timeout": tex2pdf_timeout}
params_dict.update(api_args)
params = urllib.parse.urlencode(params_dict)
url = f"{service}/?{params}"
with open(tarball, "rb") as data_fd:
uploading = {'incoming': (os.path.basename(tarball), data_fd, 'application/gzip')}
uploading = {"incoming": (os.path.basename(tarball), data_fd, "application/gzip")}
while True:
try:
res = requests.post(url, files=uploading,
timeout=post_timeout, allow_redirects=False)
res = requests.post(url, files=uploading, timeout=post_timeout, allow_redirects=False)
status_code = res.status_code
if status_code == 504:
logging.warning("Got 504 for %s", service)
Expand All @@ -43,7 +51,7 @@ def submit_tarball(service: str, tarball: str, outcome_file: str, tex2pdf_timeou
out.write(res.content)
meta, lines, clsfiles, styfiles, pdfchecksum = get_outcome_meta_and_files_info(outcome_file)
else:
logging.warning(f"%s: status code %d", url, status_code)
logging.warning("%s: status code %d", url, status_code)

except TimeoutError:
logging.warning("%s: Connection timed out", tarball)
Expand All @@ -54,13 +62,14 @@ def submit_tarball(service: str, tarball: str, outcome_file: str, tex2pdf_timeou

return meta


@pytest.fixture(scope="module")
def docker_container(request):
global PORT
PORT = request.config.getoption('--docker-port')
global PORT # noqa: PLW0603
PORT = request.config.getoption("--docker-port")
url = f"http://localhost:{PORT}"

if not request.config.getoption('--no-docker-setup'):
if not request.config.getoption("--no-docker-setup"):
image_name = "public-tex2pdf-app-2024-2024-07-21"
container_name = "test-arxiv-tex2pdf"
dockerport = "8080"
Expand All @@ -69,20 +78,31 @@ def docker_container(request):

# Make sure the container is the latest
args = ["make", "app.docker"]
make = subprocess.run(args, encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
make = subprocess.run(args, encoding="utf-8", capture_output=True, check=False)
if make.returncode != 0:
print(make.stdout)
print(make.stderr)
pass

# Start the container
args = ["docker", "run", '--security-opt', "no-new-privileges=true", "--cpus", "1", "--rm",
"-d",
"-p", f"{PORT}:{dockerport}",
"-e", f"PORT={dockerport}",
"--name", container_name,
image_name]
docker = subprocess.run(args, encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
args = [
"docker",
"run",
"--security-opt",
"no-new-privileges=true",
"--cpus",
"1",
"--rm",
"-d",
"-p",
f"{PORT}:{dockerport}",
"-e",
f"PORT={dockerport}",
"--name",
container_name,
image_name,
]
docker = subprocess.run(args, encoding="utf-8", capture_output=True, check=False)
if docker.returncode != 0:
logging.error("tex2pdf container did not start")
pass
Expand All @@ -101,12 +121,13 @@ def docker_container(request):

yield url

if not request.config.getoption('--no-docker-setup') and not request.config.getoption('--keep-docker-running'):
if not request.config.getoption("--no-docker-setup") and not request.config.getoption("--keep-docker-running"):
# Stop the container after tests
with open("tex2pdf.log", "w", encoding="utf-8") as log:
subprocess.call(["docker", "logs", container_name], stdout=log, stderr=log)
subprocess.call(["docker", "kill", container_name])


@pytest.mark.integration
def test_api_hello(docker_container):
url = docker_container
Expand Down Expand Up @@ -136,9 +157,10 @@ def test_api_test2(docker_container):
meta = submit_tarball(url, tarball, outcome, api_args={"auto_detect": "true"})
assert meta is not None
assert meta.get("pdf_file") == "test2.pdf"
assert meta.get("tex_files") == ['fake-file-2.tex']
assert meta.get("tex_files") == ["fake-file-2.tex"]
# autotex says that the documents are combined alphabetically
assert meta.get("documents") == ['out/fake-file-2.pdf']
assert meta.get("documents") == ["out/fake-file-2.pdf"]


@pytest.mark.integration
def test_api_test3(docker_container):
Expand All @@ -148,10 +170,10 @@ def test_api_test3(docker_container):
meta = submit_tarball(url, tarball, outcome, api_args={"auto_detect": "true"})
assert meta is not None
assert meta.get("pdf_file") == "test3.pdf"
assert meta.get("tex_files") == ['fake-file-2.tex', 'fake-file-1.tex', 'fake-file-3.tex']
assert meta.get("pdf_files") == ['fake-file-2.pdf', 'fake-file-1.pdf', 'fake-file-3.pdf']
assert meta.get("tex_files") == ["fake-file-2.tex", "fake-file-1.tex", "fake-file-3.tex"]
assert meta.get("pdf_files") == ["fake-file-2.pdf", "fake-file-1.pdf", "fake-file-3.pdf"]
# v2 keeps the order which is what we'd expect
assert meta.get("documents") == ['out/fake-file-2.pdf', 'out/fake-file-1.pdf', 'out/fake-file-3.pdf']
assert meta.get("documents") == ["out/fake-file-2.pdf", "out/fake-file-1.pdf", "out/fake-file-3.pdf"]


@pytest.mark.integration
Expand All @@ -162,10 +184,11 @@ def test_api_test4(docker_container):
meta = submit_tarball(url, tarball, outcome, api_args={"auto_detect": "true"})
assert meta is not None
assert meta.get("pdf_file") == "test4.pdf"
assert meta.get("tex_files") == ['main.tex', 'gdp.tex']
assert meta.get("tex_files") == ["main.tex", "gdp.tex"]
assert len(meta.get("converters", [])) == 2
assert len(meta["converters"][0]["runs"]) == 4 # latex, latex, dvi2ps, ps2pdf


@pytest.mark.integration
def test_api_preflight(docker_container):
url = docker_container + "/convert"
Expand All @@ -176,7 +199,11 @@ def test_api_preflight(docker_container):
print(meta)
assert meta.get("status").get("key") == "success"
assert len(meta.get("detected_toplevel_files")) == 3
assert [f["filename"] for f in meta.get("detected_toplevel_files")] == ['fake-file-1.tex', 'fake-file-2.tex', 'fake-file-3.tex']
assert [f["filename"] for f in meta.get("detected_toplevel_files")] == [
"fake-file-1.tex",
"fake-file-2.tex",
"fake-file-3.tex",
]


@pytest.mark.integration
Expand All @@ -190,9 +217,7 @@ def test_remote2023(docker_container) -> None:

shutil.rmtree(out_dir, ignore_errors=True)

converter = RemoteConverterDriver(url, 600, out_dir, tarball,
use_addon_tree=False, tag=tag,
auto_detect=True)
converter = RemoteConverterDriver(url, 600, out_dir, tarball, use_addon_tree=False, tag=tag, auto_detect=True)
logging.debug("Calling generate_pdf")
pdf = converter.generate_pdf()
assert os.path.isfile(f"{out_dir}/outcome-test1.json")
Expand Down
8 changes: 5 additions & 3 deletions tests/service/test_patch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest

from tex2pdf.service.tex_patching import fix_tex_sources

TEST_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "output/test-patch")
Expand All @@ -22,10 +23,12 @@
\graphicspath{{foo}{foo/}{bar/}{baz}{baz/}}
"""


def read_file(filename) -> str:
with open(filename, "r", encoding="utf-8") as fd:
with open(filename, encoding="utf-8") as fd:
return fd.read()


class TestTexPatch(unittest.TestCase):
def setUp(self):
os.makedirs(TEST_DIR, exist_ok=True)
Expand All @@ -39,6 +42,5 @@ def test_fixer(self):
self.assertEqual(first_expected, result)



if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
10 changes: 5 additions & 5 deletions tests/service/test_typings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests using mypy."""

import os
import shutil
import subprocess
Expand All @@ -13,12 +14,11 @@ def test_run_mypy_module(self) -> None:
"""Run mypy on all module sources."""
mypy = shutil.which("mypy")
if mypy is None:
raise EnvironmentError("mypy not found in PATH")
raise OSError("mypy not found in PATH")
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
result: int = subprocess.call([mypy, "tex2pdf/service"],
env=os.environ, cwd=root_dir)
self.assertEqual(result, 0, 'Expect 0 type errors when running mypy')
result: int = subprocess.call([mypy, "tex2pdf/service"], env=os.environ, cwd=root_dir)
self.assertEqual(result, 0, "Expect 0 type errors when running mypy")


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
20 changes: 10 additions & 10 deletions tests/service/test_watermark.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import os
import unittest

from tex2pdf.service.pdf_watermark import add_watermark_text_to_pdf, Watermark
from tex2pdf.service.pdf_watermark import Watermark, add_watermark_text_to_pdf

SELF_DIR = os.path.abspath(os.path.dirname(__file__))

watermark_pdf = os.path.join(SELF_DIR, "output/watermark.pdf")
in_pdf = os.path.join(SELF_DIR, "fixture/smoke/Test.pdf")

class MyTestCase(unittest.TestCase):

class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
os.makedirs(os.path.dirname(watermark_pdf), exist_ok=True)

def test_watermarking(self):
add_watermark_text_to_pdf(Watermark(
"Water World is in Orlando, FL.",
"https://en.wikipedia.org/wiki/Waterworld"
),
in_pdf, os.path.join(SELF_DIR, "output/Test.pdf")
)

if __name__ == '__main__':
add_watermark_text_to_pdf(
Watermark("Water World is in Orlando, FL.", "https://en.wikipedia.org/wiki/Waterworld"),
in_pdf,
os.path.join(SELF_DIR, "output/Test.pdf"),
)


if __name__ == "__main__":
unittest.main()
19 changes: 12 additions & 7 deletions tests/tex_inspection/test_ban.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest
from tex2pdf.tex_inspection import maybe_banned_tex_file, is_banned_tex

from tex2pdf.tex_inspection import is_banned_tex, maybe_banned_tex_file

class BanTest(unittest.TestCase):

class BanTest(unittest.TestCase):
def test_ban_1(self):
self.assertTrue(maybe_banned_tex_file("sample-foo.tex"))
self.assertFalse(maybe_banned_tex_file("main.tex"))
Expand All @@ -16,9 +16,14 @@ def test_ban_2(self):
pass

def test_ban_3(self):
self.assertTrue(is_banned_tex("docsimart.tex",
"\\title{Guide to Using SIAM's \\LaTeX\\ Style\\thanks{Submitted to the editors DATE."))
self.assertFalse(is_banned_tex("docsimart.text",
"\\title{Guide to Using SIAM's \\LaTeX\\ Style\\thanks{Submitted to the editors DATE."))
self.assertTrue(
is_banned_tex(
"docsimart.tex", "\\title{Guide to Using SIAM's \\LaTeX\\ Style\\thanks{Submitted to the editors DATE."
)
)
self.assertFalse(
is_banned_tex(
"docsimart.text", "\\title{Guide to Using SIAM's \\LaTeX\\ Style\\thanks{Submitted to the editors DATE."
)
)
pass

5 changes: 3 additions & 2 deletions tests/tex_inspection/test_conveter_opts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import unittest

from tex2pdf.tex_inspection import find_pdfoutput_1

class TestConverterSelection(unittest.TestCase):

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

Expand All @@ -20,5 +21,5 @@ def test_no(self):
pass


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
10 changes: 5 additions & 5 deletions tests/tex_inspection/test_typings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests using mypy."""

import os
import shutil
import subprocess
Expand All @@ -13,12 +14,11 @@ def test_run_mypy_module(self) -> None:
"""Run mypy on all module sources."""
mypy = shutil.which("mypy")
if mypy is None:
raise EnvironmentError("mypy not found in PATH")
raise OSError("mypy not found in PATH")
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
result: int = subprocess.call([mypy, "tex2pdf/tex_inspection"],
env=os.environ, cwd=root_dir)
self.assertEqual(result, 0, 'Expect 0 type errors when running mypy')
result: int = subprocess.call([mypy, "tex2pdf/tex_inspection"], env=os.environ, cwd=root_dir)
self.assertEqual(result, 0, "Expect 0 type errors when running mypy")


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit 66bc48d

Please sign in to comment.