Skip to main content

tests/utils.py

1import pathlib
2import subprocess
3import typing
6class CommandOutput(typing.TypedDict):
7 returncode: int
8 stdout: str | None
9 stderr: str | None
12def save_safari_webarchive(argv: list[str | pathlib.Path]) -> CommandOutput:
13 """
14 Run the ``save_safari_webarchive.swift`` script and return the result.
15 """
16 cmd = ["swift", "save_safari_webarchive.swift"] + [str(av) for av in argv]
18 proc = subprocess.Popen(
19 cmd,
20 stdout=subprocess.PIPE,
21 stderr=subprocess.PIPE,
22 )
23 stdout, stderr = proc.communicate()
25 if stdout is not None:
26 stdout = stdout.decode("utf8")
28 if stderr is not None:
29 stderr = stderr.decode("utf8")
31 return CommandOutput(
32 returncode=proc.returncode,
33 stdout=stdout or None,
34 stderr=stderr or None,
35 )