7
7
import tomllib
8
8
from typing import TYPE_CHECKING
9
9
10
+ import packaging .requirements
10
11
import pytest
11
12
12
13
from tests import PROJECT_ROOT
13
14
14
15
if TYPE_CHECKING :
16
+ from typing import Any
15
17
import pytest_subtests
16
18
17
19
MAKE = shutil .which ("gmake" ) or shutil .which ("make" )
@@ -27,15 +29,31 @@ def test_image_pyprojects(subtests: pytest_subtests.plugin.SubTests):
27
29
except ValueError :
28
30
pytest .skip (f"skipping { directory .name } /pyproject.toml as it is not an image directory" )
29
31
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
+ )
39
57
40
58
41
59
def test_files_that_should_be_same_are_same (subtests : pytest_subtests .plugin .SubTests ):
0 commit comments