Skip to content

Commit

Permalink
Update komodo matrix functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-el committed Jan 16, 2025
1 parent 35f899d commit 7d26590
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
46 changes: 32 additions & 14 deletions komodo/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,53 @@

import itertools
import re
from typing import Iterator, Sequence, Tuple
from typing import Iterator, Sequence, Tuple, Optional, Dict


def get_matrix(
rhel_versions: Sequence[str],
py_versions: Sequence[str],
) -> Iterator[Tuple[str, str]]:
other_components: Optional[Dict[str, Sequence[str]]] = None,
) -> Iterator[Tuple[str, str, str]]:
"""Return tuples of rhel version and Python version, representing the
current release matrix.
"""
for product in itertools.product(rhel_versions, py_versions):
rh_ver, py_ver = product
yield (f"rhel{rh_ver}", f"py{str(py_ver).replace('.', '')}")

if other_components:
((component_name, component_seq),) = (
other_components.items()
) # only pick a single component

def format_release(base: str, rhel_ver: str, py_ver: str) -> str:
for product in itertools.product(rhel_versions, py_versions, component_seq):
rh_ver, py_ver, other_ver = product
yield (
f"rhel{rh_ver}",
f"py{str(py_ver).replace('.', '')}",
f"{component_name}{other_ver}",
)
else:
for product in itertools.product(rhel_versions, py_versions):
rh_ver, py_ver = product
yield f"rhel{rh_ver}", f"py{str(py_ver).replace('.', '')}", None


def format_release(
base: str, rhel_ver: str, py_ver: str, other_component: Optional[str] = None
) -> str:
"""Format a base (e.g. a matrix file without the .yml suffix) such that it
looks like a concrete release.
"""
return f"{base}-{py_ver}-{rhel_ver}"
return (
f"{base}-{py_ver}-{rhel_ver}-{other_component}"
if other_component
else f"{base}-{py_ver}-{rhel_ver}"
)


def get_matrix_base(release_name: str) -> str:
"""Return the base (e.g. matrix part of a concrete release). Should be the
inverse of format_release for actual, concrete matrix releases.
Hard-coded the suffix pattern '-py..-rhel.' or '-py...-rhel.'.
"""Return the base (e.g. matrix part of a concrete release).
Match release name on -py[nno]-rhel[n] and delimit using that
"""
suffix = format_release("", "rhel[0-9]", r"py\d{2,3}")
if re.search(suffix, release_name):
return re.split(suffix, release_name)[0]
# no matrix suffix at all
if re.search(r"-py\d{2,3}-rhel[0-9]", release_name):
return release_name.split("-py")[0]
return release_name
42 changes: 40 additions & 2 deletions tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
from komodo import matrix


def test_format_matrix():
assert matrix.format_release("base", "rhel6", "py27") == "base-py27-rhel6"
@pytest.mark.parametrize(
("base", "rhel", "python", "other", "expected"),
[
("base", "rhel6", "py27", None, "base-py27-rhel6"),
("base", "rhel6", "py27", "numpy1", "base-py27-rhel6-numpy1"),
],
)
def test_format_matrix(base, rhel, python, other, expected):
assert matrix.format_release(base, rhel, python, other) == expected


@pytest.mark.parametrize(
Expand All @@ -14,8 +21,39 @@ def test_format_matrix():
("1970.12.rc0-foo-py38-rhel7", "1970.12.rc0-foo"),
("1970.12.03", "1970.12.03"),
(matrix.format_release("1970.12.04", "rhel7", "py38"), "1970.12.04"),
(matrix.format_release("1990.06.04", "rhel9", "py38", "numpy2"), "1990.06.04"),
("1970.12.05-rhel8-py27", "1970.12.05-rhel8-py27"), # outside matrix
("2025.02.00-py311-rhel9-numpy2", "2025.02.00"),
],
)
def test_get_matrix_base(test_input, expected):
assert matrix.get_matrix_base(test_input) == expected


@pytest.mark.parametrize(
("rhel_ver", "py_ver", "other_ver", "expected_yield"),
[
(
["8"],
["38", "311"],
None,
[("rhel8", "py38", None), ("rhel8", "py311", None)],
),
(
["8", "9"],
["311"],
{"numpy": ["1", "2"]},
[
("rhel8", "py311", "numpy1"),
("rhel8", "py311", "numpy2"),
("rhel9", "py311", "numpy1"),
("rhel9", "py311", "numpy2"),
],
),
],
)
def test_get_matrix(rhel_ver, py_ver, other_ver, expected_yield):
yielded = []
for mat in matrix.get_matrix(rhel_ver, py_ver, other_ver):
yielded.append(mat)
assert yielded == expected_yield

0 comments on commit 7d26590

Please sign in to comment.