Skip to content

Commit c216b74

Browse files
authored
Centralize METADATA.toml parsing in the test suite (#9534)
1 parent 7f986bd commit c216b74

11 files changed

+296
-193
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
cache: pip
3434
cache-dependency-path: requirements-tests.txt
3535
- run: pip install -r requirements-tests.txt
36-
- run: ./tests/check_consistent.py
36+
- run: python ./tests/check_consistent.py
3737

3838
new-syntax:
3939
name: Ensure new syntax usage

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extra_standard_library = [
5555
"opcode",
5656
"pyexpat",
5757
]
58-
known_first_party = ["utils"]
58+
known_first_party = ["utils", "parse_metadata"]
5959

6060
[tool.pycln]
6161
all = true

tests/check_consistent.py

100755100644
Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,14 @@
1111
import urllib.parse
1212
from pathlib import Path
1313

14-
import tomli
1514
import yaml
1615
from packaging.requirements import Requirement
1716
from packaging.specifiers import SpecifierSet
18-
from packaging.version import Version
19-
20-
from utils import (
21-
METADATA_MAPPING,
22-
VERSIONS_RE,
23-
get_all_testcase_directories,
24-
get_gitignore_spec,
25-
spec_matches_path,
26-
strip_comments,
27-
)
28-
29-
metadata_keys = {
30-
"version",
31-
"requires",
32-
"extra_description",
33-
"stub_distribution",
34-
"obsolete_since",
35-
"no_longer_updated",
36-
"upload",
37-
"tool",
38-
}
39-
tool_keys = {
40-
"stubtest": {
41-
"skip",
42-
"apt_dependencies",
43-
"brew_dependencies",
44-
"choco_dependencies",
45-
"extras",
46-
"ignore_missing_stub",
47-
"platforms",
48-
}
49-
}
50-
extension_descriptions = {".pyi": "stub", ".py": ".py"}
51-
supported_stubtest_platforms = {"win32", "darwin", "linux"}
5217

53-
dist_name_re = re.compile(r"^[a-z0-9]([a-z0-9._-]*[a-z0-9])?$", re.IGNORECASE)
18+
from parse_metadata import read_metadata
19+
from utils import VERSIONS_RE, get_all_testcase_directories, get_gitignore_spec, spec_matches_path, strip_comments
20+
21+
extension_descriptions = {".pyi": "stub", ".py": ".py"}
5422

5523

5624
def assert_consistent_filetypes(
@@ -163,46 +131,8 @@ def _find_stdlib_modules() -> set[str]:
163131

164132
def check_metadata() -> None:
165133
for distribution in os.listdir("stubs"):
166-
with open(os.path.join("stubs", distribution, "METADATA.toml"), encoding="UTF-8") as f:
167-
data = tomli.loads(f.read())
168-
assert "version" in data, f"Missing version for {distribution}"
169-
version = data["version"]
170-
msg = f"Unsupported version {repr(version)}"
171-
assert isinstance(version, str), msg
172-
# Check that the version parses
173-
Version(version.removesuffix(".*"))
174-
for key in data:
175-
assert key in metadata_keys, f"Unexpected key {key} for {distribution}"
176-
assert isinstance(data.get("requires", []), list), f"Invalid requires value for {distribution}"
177-
for dep in data.get("requires", []):
178-
assert isinstance(dep, str), f"Invalid requirement {repr(dep)} for {distribution}"
179-
for space in " \t\n":
180-
assert space not in dep, f"For consistency, requirement should not have whitespace: {dep}"
181-
# Check that the requirement parses
182-
Requirement(dep)
183-
184-
if "stub_distribution" in data:
185-
assert dist_name_re.fullmatch(data["stub_distribution"]), f"Invalid 'stub_distribution' value for {distribution!r}"
186-
187-
assert isinstance(data.get("upload", True), bool), f"Invalid 'upload' value for {distribution!r}"
188-
189-
assert set(data.get("tool", [])).issubset(tool_keys.keys()), f"Unrecognised tool for {distribution}"
190-
for tool, tk in tool_keys.items():
191-
for key in data.get("tool", {}).get(tool, {}):
192-
assert key in tk, f"Unrecognised {tool} key {key} for {distribution}"
193-
194-
tool_stubtest = data.get("tool", {}).get("stubtest", {})
195-
specified_stubtest_platforms = set(tool_stubtest.get("platforms", ["linux"]))
196-
assert (
197-
specified_stubtest_platforms <= supported_stubtest_platforms
198-
), f"Unrecognised platforms specified: {supported_stubtest_platforms - specified_stubtest_platforms} for {distribution}"
199-
200-
# Check that only specified platforms install packages:
201-
for supported_plat in supported_stubtest_platforms:
202-
if supported_plat not in specified_stubtest_platforms:
203-
assert (
204-
METADATA_MAPPING[supported_plat] not in tool_stubtest
205-
), f"Installing system deps for unspecified platform {supported_plat} for {distribution}"
134+
# This function does various sanity checks for METADATA.toml files
135+
read_metadata(distribution)
206136

207137

208138
def get_txt_requirements() -> dict[str, SpecifierSet]:

tests/get_external_stub_requirements.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import sys
66

7-
from utils import read_dependencies
7+
from parse_metadata import read_dependencies
88

99
distributions = sys.argv[1:]
1010
if not distributions:

tests/get_stubtest_system_requirements.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
import os
33
import sys
44

5-
import tomli
6-
7-
from utils import METADATA_MAPPING
5+
from parse_metadata import read_stubtest_settings
86

97
platform = sys.platform
108
distributions = sys.argv[1:]
119
if not distributions:
1210
distributions = os.listdir("stubs")
1311

14-
if platform in METADATA_MAPPING:
15-
for distribution in distributions:
16-
with open(f"stubs/{distribution}/METADATA.toml", "rb") as file:
17-
for package in tomli.load(file).get("tool", {}).get("stubtest", {}).get(METADATA_MAPPING[platform], []):
18-
print(package)
12+
for distribution in distributions:
13+
stubtest_settings = read_stubtest_settings(distribution)
14+
for package in stubtest_settings.system_requirements_for_platform(platform):
15+
print(package)

tests/mypy_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626

2727
import tomli
2828

29+
from parse_metadata import PackageDependencies, get_recursive_requirements
2930
from utils import (
3031
VERSIONS_RE as VERSION_LINE_RE,
31-
PackageDependencies,
3232
VenvInfo,
3333
colored,
3434
get_gitignore_spec,
3535
get_mypy_req,
36-
get_recursive_requirements,
3736
make_venv,
3837
print_error,
3938
print_success_msg,

0 commit comments

Comments
 (0)