Skip to content

Commit 0afdfd5

Browse files
committed
Ghostscript fixes and tests
1 parent a6be656 commit 0afdfd5

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

problemtools/problem2pdf.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,27 @@ def latex2pdf(options: argparse.Namespace) -> bool:
133133
if status:
134134
return False
135135

136-
try:
137-
with tempfile.NamedTemporaryFile(suffix='.pdf') as f:
138-
command = [
139-
'gs',
140-
'-q',
141-
'-dBATCH',
142-
'-sDEVICE=pdfwrite',
143-
'-dNOPAUSE',
144-
'-dCompatibilityLevel=1.7',
145-
f'-sOutputFile={f.name}',
146-
destfile,
147-
]
148-
gs_status = subprocess.run(command, capture_output=True, text=True, shell=False, check=True)
149-
if gs_status:
150-
return False
151-
shutil.copy(f.name, destfile)
152-
except subprocess.CalledProcessError as e:
153-
print(f'Error sanitizing PDF: {e} {e.stderr}')
154-
raise
136+
# We only sanitize if a PDF was created
137+
if not options.nopdf:
138+
try:
139+
with tempfile.NamedTemporaryFile(suffix='.pdf') as f:
140+
command = [
141+
'gs',
142+
'-q',
143+
'-dBATCH',
144+
'-sDEVICE=pdfwrite',
145+
'-dNOPAUSE',
146+
'-dCompatibilityLevel=1.7',
147+
f'-sOutputFile={f.name}',
148+
destfile,
149+
]
150+
gs_status = subprocess.run(command, capture_output=True, text=True, shell=False, check=True)
151+
if gs_status.returncode != 0:
152+
return False
153+
shutil.copy(f.name, destfile)
154+
except subprocess.CalledProcessError as e:
155+
print(f'Error sanitizing PDF: {e} {e.stderr}')
156+
raise
155157

156158
return True
157159

tests/test_latex.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from pathlib import Path
2+
import tempfile
3+
4+
from problemtools import problem2pdf
5+
6+
7+
def test_pdf_render_verifyproblem():
8+
# Same options as in verifyproblem
9+
options = problem2pdf.get_parser().parse_args([''])
10+
problem_path = Path(__file__).parent / '..' / 'examples' / 'guess'
11+
options.problem = str(problem_path.resolve())
12+
options.language = 'en'
13+
options.nopdf = True
14+
options.quiet = True
15+
if not problem2pdf.convert(options):
16+
assert False, 'PDF conversion failed'
17+
18+
19+
def test_pdf_render_problem2pdf():
20+
# Same options as typical problem2pdf usage
21+
with tempfile.TemporaryDirectory() as temp_dir:
22+
options = problem2pdf.get_parser().parse_args([''])
23+
problem_path = Path(__file__).parent / '..' / 'examples' / 'guess'
24+
options.problem = str(problem_path.resolve())
25+
options.language = 'en'
26+
options.quiet = True
27+
options.dest_dir = str(temp_dir)
28+
if not problem2pdf.convert(options):
29+
assert False, 'PDF conversion failed'

tests/test_markdown.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
from pathlib import Path
2+
3+
import pytest
24
from tests.test_xss import render, renderpdf
35
from problemtools.statement_util import find_footnotes
4-
import pytest
6+
7+
# TODO: add when guess is updated to 2023-07
8+
# def test_pdf_render():
9+
# with tempfile.TemporaryDirectory() as temp_dir:
10+
# problem_path = Path(__file__).parent / '..' / 'examples' / 'guess'
11+
# args, _unknown = problem2pdf.get_parser().parse_known_args(
12+
# ['--problem', str(problem_path.resolve()), '-l', 'sv', '--dest-dir', str(temp_dir)]
13+
# )
14+
# problem2pdf.convert(args)
515

616

717
def test_sample_escaping():

0 commit comments

Comments
 (0)