Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/python/test_pdfium_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,46 @@


class PdfiumRendererTests(unittest.TestCase):
def test_prepare_resolves_the_npx_platform_shim(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
pdfium = root / "pdfium"
pdfium.mkdir()
cache = root / "cache"
resources = [root / "font.otf", root / "OFL.txt"]
for resource in resources:
resource.write_bytes(b"fixture")

arguments = mock.Mock(
pdfium_dir=pdfium,
font_cache=cache,
download=True,
build=True,
)
windows_npx = r"C:\hostedtoolcache\windows\node\npx.CMD"

with (
mock.patch.object(
prepare_renderer,
"verified_resources",
return_value=resources,
),
mock.patch.object(prepare_renderer, "patch_source"),
mock.patch.object(
prepare_renderer,
"required_tool",
return_value=windows_npx,
) as resolve_tool,
mock.patch.object(
prepare_renderer.subprocess,
"run",
) as run,
):
prepare_renderer.prepare(arguments)

resolve_tool.assert_called_once_with("npx")
self.assertEqual(run.call_args_list[1].args[0][0], windows_npx)

def test_patch_source_is_pinned_and_idempotent(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
pdfium = Path(temporary)
Expand Down
9 changes: 8 additions & 1 deletion tools/pdfium/prepare_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
ALLOWED_FONT_ORIGIN = "https://raw.githubusercontent.com"


def required_tool(name: str) -> str:
resolved = shutil.which(name)
if resolved is None:
raise RuntimeError(f"required build tool is missing: {name}")
return resolved


def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as source:
Expand Down Expand Up @@ -119,7 +126,7 @@ def prepare(arguments: argparse.Namespace) -> None:
["node", "scripts/download-pdfium.mjs"], cwd=pdfium_dir, check=True
)
subprocess.run(
["npx", "--yes", "node-gyp@11.4.2", "rebuild"],
[required_tool("npx"), "--yes", "node-gyp@11.4.2", "rebuild"],
cwd=pdfium_dir,
check=True,
)
Expand Down
Loading