| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
"""A set of Hypothesis-based correctness tests.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import contextlib |
| 8 |
import subprocess # noqa: S404 |
| 9 |
import typing |
| 10 |
from unittest import mock |
| 11 |
|
| 12 |
import hypothesis as hyp |
| 13 |
import hypothesis.strategies as strat |
| 14 |
|
| 15 |
from feature_check import defs as fdefs |
| 16 |
from feature_check import obtain as fobtain |
| 17 |
from feature_check import parser as fparser |
| 18 |
|
| 19 |
|
| 20 |
if typing.TYPE_CHECKING: |
| 21 |
from typing import Final |
| 22 |
|
| 23 |
|
| 24 |
ALPHABET_NAME: Final = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" |
| 25 |
ALPHABET_VER_FIRST: Final = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 26 |
ALPHABET_VER_REST: Final = f"0{ALPHABET_VER_FIRST}" |
| 27 |
|
| 28 |
STRAT_COMPONENT: Final = strat.tuples( |
| 29 |
strat.text(ALPHABET_VER_FIRST, min_size=1, max_size=1), |
| 30 |
strat.text(ALPHABET_VER_REST), |
| 31 |
) |
| 32 |
|
| 33 |
STRAT_FEATURES: Final = strat.lists( |
| 34 |
strat.tuples(strat.text(ALPHABET_NAME, min_size=1), strat.lists(STRAT_COMPONENT, min_size=1)), |
| 35 |
min_size=1, |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def build_features(pairs: list[tuple[str, list[tuple[str, str]]]]) -> list[tuple[str, str]]: |
| 40 |
"""Build a list of (name, version) pairs out of the feature components.""" |
| 41 |
return [(name, ".".join(f"{first}{rest}" for first, rest in comps)) for name, comps in pairs] |
| 42 |
|
| 43 |
|
| 44 |
@hyp.given(line=strat.text()) |
| 45 |
@hyp.example("") |
| 46 |
def test_parse_exception(line: str) -> None: |
| 47 |
"""Make sure the parser returns either success or our own exception.""" |
| 48 |
with contextlib.suppress(fdefs.FCError): |
| 49 |
fparser.parse_features_line(line) |
| 50 |
|
| 51 |
|
| 52 |
@hyp.given(pairs=STRAT_FEATURES) |
| 53 |
def test_parse_pairs(pairs: list[tuple[str, list[tuple[str, str]]]]) -> None: |
| 54 |
"""Make sure the parser returns the expected dictionary of feature names and versions.""" |
| 55 |
paired: Final = build_features(pairs) |
| 56 |
exp: Final = dict(paired) |
| 57 |
line: Final = " ".join(f"{name}={ver}" for name, ver in paired) |
| 58 |
features: Final = fparser.parse_features_line(line) |
| 59 |
res: Final = {name: ver.value for name, ver in features.items()} |
| 60 |
assert res == exp |
| 61 |
|
| 62 |
|
| 63 |
@hyp.given( |
| 64 |
program=strat.text(min_size=1), |
| 65 |
option=strat.text(min_size=1), |
| 66 |
prefix=strat.text(ALPHABET_NAME), |
| 67 |
pairs=STRAT_FEATURES, |
| 68 |
) |
| 69 |
@hyp.example("0", "0", "", [("A", [("1", "")])]) |
| 70 |
def test_parse_features_line( |
| 71 |
program: str, |
| 72 |
option: str, |
| 73 |
prefix: str, |
| 74 |
pairs: list[tuple[str, list[tuple[str, str]]]], |
| 75 |
) -> None: |
| 76 |
"""Make sure a full line can be parsed.""" |
| 77 |
paired: Final = build_features(pairs) |
| 78 |
exp: Final = dict(paired) |
| 79 |
line: Final = prefix + " ".join(f"{name}={ver}" for name, ver in paired) |
| 80 |
|
| 81 |
def mock_subprocess_run( |
| 82 |
args: list[str], |
| 83 |
*, |
| 84 |
capture_output: bool = False, |
| 85 |
check: bool = True, |
| 86 |
stdin: str | None = "oof?", |
| 87 |
) -> subprocess.CompletedProcess[bytes]: |
| 88 |
"""Mock the invocation of an external program.""" |
| 89 |
assert args == [program, option] |
| 90 |
assert capture_output |
| 91 |
assert not check |
| 92 |
assert stdin is None |
| 93 |
return subprocess.CompletedProcess( |
| 94 |
args=args, |
| 95 |
returncode=0, |
| 96 |
stdout=f"{line}\n".encode(), |
| 97 |
stderr=b"", |
| 98 |
) |
| 99 |
|
| 100 |
with mock.patch("subprocess.run", new=mock_subprocess_run): |
| 101 |
features: Final = fobtain.obtain_features(program, option, prefix) |
| 102 |
|
| 103 |
res: Final = {name: ver.value for name, ver in features.items()} |
| 104 |
assert res == exp |