Skip to content

Commit 8dc2d1e

Browse files
committed
Add some soundness checks to startup
1 parent 753b57e commit 8dc2d1e

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

conftest.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import pytest
2727

2828
import util
29-
from util import partdiff_params_tuple
29+
from util import ReferenceSource, partdiff_params_tuple
3030

3131

3232
def shlex_list_str(value: str) -> list[str]:
@@ -41,16 +41,16 @@ def shlex_list_str(value: str) -> list[str]:
4141
return shlex.split(value)
4242

4343

44-
def reference_source_param(value: str) -> util.ReferenceSource:
44+
def reference_source_param(value: str) -> ReferenceSource:
4545
"""Parse a ReferenceSource from str.
4646
4747
Args:
4848
value (str): The str to parse.
4949
5050
Returns:
51-
util.ReferenceSource: The parsed ReferenceSource.
51+
ReferenceSource: The parsed ReferenceSource.
5252
"""
53-
return util.ReferenceSource(value)
53+
return ReferenceSource(value)
5454

5555

5656
REGEX_NUM_LIST = re.compile(r"^(?:\d+(?:-\d+)?)(?:,\d+(?:-\d+)?)*$")
@@ -217,8 +217,8 @@ def pytest_addoption(parser: pytest.Parser) -> None:
217217
"auto == try cache and fall back to impl)."
218218
),
219219
type=reference_source_param,
220-
default=util.ReferenceSource.cache,
221-
choices=util.ReferenceSource,
220+
default=ReferenceSource.cache,
221+
choices=ReferenceSource,
222222
)
223223
custom_options.addoption(
224224
"--num-threads",
@@ -315,6 +315,16 @@ def pytest_configure(config: pytest.Config) -> None:
315315
"""
316316
See https://docs.pytest.org/en/7.1.x/reference/reference.html#pytest.hookspec.pytest_configure
317317
"""
318+
if config.getoption("reference_source") in (
319+
ReferenceSource.auto,
320+
ReferenceSource.impl,
321+
):
322+
util.ensure_reference_implementation_exists()
323+
324+
util.check_executable_exists(
325+
config.getoption("executable"), config.getoption("cwd")
326+
)
327+
318328
if config.getoption("valgrind"):
319329
if shutil.which("valgrind") is None:
320330
raise RuntimeError("Passed --valgrind, but valgrind could not be found.")

util.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ def get_from_cache():
252252
return reference_output_data[partdiff_params]
253253

254254
def get_from_impl():
255-
ensure_reference_implementation_exists()
256255
command_line = [REFERENCE_IMPLEMENTATION_EXEC] + list(partdiff_params)
257256
return subprocess.check_output(command_line).decode("utf-8")
258257

@@ -296,3 +295,18 @@ def get_actual_output(
296295
if use_valgrind:
297296
command_line = ["valgrind", "--leak-check=full"] + command_line
298297
return subprocess.check_output(command_line, cwd=cwd).decode("utf-8")
298+
299+
300+
def check_executable_exists(executable: list[str], cwd: Path | None) -> None:
301+
"""Check if the executable exists and can be executed by running it.
302+
303+
The executable is allowed to return a non-zero exit status.
304+
305+
Args:
306+
executable (list[str]): The executable to check
307+
cwd (Path | None): The working directory of the executable.
308+
"""
309+
try:
310+
subprocess.check_output(executable, cwd=cwd)
311+
except subprocess.CalledProcessError:
312+
pass

0 commit comments

Comments
 (0)