77import tomllib
88from typing import TYPE_CHECKING
99
10+ import packaging .requirements
1011import pytest
1112
1213from tests import PROJECT_ROOT
1314
1415if TYPE_CHECKING :
16+ from typing import Any
1517 import pytest_subtests
1618
1719MAKE = 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
4159def test_files_that_should_be_same_are_same (subtests : pytest_subtests .plugin .SubTests ):
0 commit comments