Skip to content

Commit d4c77bd

Browse files
authored
RHAIENG-304: tests(pytests): add check for the pylock.toml integrity with respect to pyproject.toml (#2233)
1 parent 5977b60 commit d4c77bd

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

tests/test_main.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import tomllib
88
from typing import TYPE_CHECKING
99

10+
import packaging.requirements
1011
import pytest
1112

1213
from tests import PROJECT_ROOT
1314

1415
if TYPE_CHECKING:
16+
from typing import Any
1517
import pytest_subtests
1618

1719
MAKE = shutil.which("gmake") or shutil.which("make")
@@ -27,15 +29,31 @@ def test_image_pyprojects(subtests: pytest_subtests.plugin.SubTests):
2729
except ValueError:
2830
pytest.skip(f"skipping {directory.name}/pyproject.toml as it is not an image directory")
2931

30-
with open(file, "rb") as fp:
31-
pyproject = tomllib.load(fp)
32-
assert "project" in pyproject, "pyproject.toml is missing a [project] section"
33-
assert "requires-python" in pyproject["project"], (
34-
"pyproject.toml is missing a [project.requires-python] section"
35-
)
36-
assert pyproject["project"]["requires-python"] == f"=={python}.*", (
37-
"pyproject.toml does not declare the expected Python version"
38-
)
32+
pyproject = tomllib.loads(file.read_text())
33+
with subtests.test(msg="checking pyproject.toml", pyproject=file):
34+
assert "project" in pyproject, "pyproject.toml is missing a [project] section"
35+
assert "requires-python" in pyproject["project"], (
36+
"pyproject.toml is missing a [project.requires-python] section"
37+
)
38+
assert pyproject["project"]["requires-python"] == f"=={python}.*", (
39+
"pyproject.toml does not declare the expected Python version"
40+
)
41+
42+
assert "dependencies" in pyproject["project"], (
43+
"pyproject.toml is missing a [project.dependencies] section"
44+
)
45+
46+
pylock = tomllib.loads(file.with_name("pylock.toml").read_text())
47+
pylock_packages: dict[str, dict[str, Any]] = {p["name"]: p for p in pylock["packages"]}
48+
with subtests.test(msg="checking pylock.toml consistency with pyproject.toml", pyproject=file):
49+
for d in pyproject["project"]["dependencies"]:
50+
requirement = packaging.requirements.Requirement(d)
51+
52+
assert requirement.name in pylock_packages, f"Dependency {d} is not in pylock.toml"
53+
version = pylock_packages[requirement.name]["version"]
54+
assert requirement.specifier.contains(version), (
55+
f"Version of {d} in pyproject.toml does not match {version=} in pylock.toml"
56+
)
3957

4058

4159
def test_files_that_should_be_same_are_same(subtests: pytest_subtests.plugin.SubTests):

0 commit comments

Comments
 (0)