Skip to content

Commit 76b8dbe

Browse files
committed
Add tests for sync tool
1 parent bef8a1f commit 76b8dbe

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
from typing import TYPE_CHECKING
5+
6+
from trio._tests.pytest_plugin import skip_if_optional_else_raise
7+
8+
# imports in gen_exports that are not in `install_requires` in requirements
9+
try:
10+
import yaml # noqa: F401
11+
except ImportError as error:
12+
skip_if_optional_else_raise(error)
13+
14+
from trio._tools.sync_requirements import (
15+
update_requirements,
16+
yield_pre_commit_version_data,
17+
)
18+
19+
if TYPE_CHECKING:
20+
from pathlib import Path
21+
22+
23+
def test_yield_pre_commit_version_data() -> None:
24+
text = """
25+
repos:
26+
- repo: https://github.com/astral-sh/ruff-pre-commit
27+
rev: v0.11.0
28+
- repo: https://github.com/psf/black-pre-commit-mirror
29+
rev: 25.1.0
30+
"""
31+
results = tuple(yield_pre_commit_version_data(text))
32+
assert results == (
33+
("ruff-pre-commit", "0.11.0"),
34+
("black-pre-commit-mirror", "25.1.0"),
35+
)
36+
37+
38+
def test_update_requirements(
39+
tmp_path: Path,
40+
) -> None:
41+
requirements_file = tmp_path / "requirements.txt"
42+
assert not requirements_file.exists()
43+
requirements_file.write_text(
44+
"""# comment
45+
# also comment but spaces line start
46+
waffles are delicious no equals
47+
black==3.1.4 ; specific version thingy
48+
mypy==1.15.0
49+
ruff==1.2.5
50+
# required by soupy cat""",
51+
encoding="utf-8",
52+
)
53+
assert update_requirements(requirements_file, {"black": "3.1.5", "ruff": "1.2.7"})
54+
assert (
55+
requirements_file.read_text(encoding="utf-8")
56+
== """# comment
57+
# also comment but spaces line start
58+
waffles are delicious no equals
59+
black==3.1.5 ; specific version thingy
60+
mypy==1.15.0
61+
ruff==1.2.7
62+
# required by soupy cat"""
63+
)
64+
65+
66+
def test_update_requirements_no_changes(
67+
tmp_path: Path,
68+
) -> None:
69+
requirements_file = tmp_path / "requirements.txt"
70+
assert not requirements_file.exists()
71+
original = """# comment
72+
# also comment but spaces line start
73+
waffles are delicious no equals
74+
black==3.1.4 ; specific version thingy
75+
mypy==1.15.0
76+
ruff==1.2.5
77+
# required by soupy cat"""
78+
requirements_file.write_text(original, encoding="utf-8")
79+
assert not update_requirements(
80+
requirements_file, {"black": "3.1.4", "ruff": "1.2.5"}
81+
)
82+
assert requirements_file.read_text(encoding="utf-8") == original

src/trio/_tools/sync_requirements.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626

2727
def yield_pre_commit_version_data(
28-
pre_commit: Path,
28+
pre_commit_text: str,
2929
) -> Generator[tuple[str, str], None, None]:
3030
"""Yield (name, rev) tuples from pre-commit config file."""
31-
pre_commit_config = load_yaml(pre_commit.read_text(encoding="utf-8"), Loader)
31+
pre_commit_config = load_yaml(pre_commit_text, Loader)
3232
for repo in pre_commit_config["repos"]:
3333
if "repo" not in repo or "rev" not in repo:
3434
continue
@@ -81,18 +81,19 @@ def main() -> int:
8181
"""Run program."""
8282

8383
source_root = Path.cwd().absolute()
84-
while not (source_root / "LICENSE").exists():
85-
source_root = source_root.parent
84+
8685
# Double-check we found the right directory
8786
assert (source_root / "LICENSE").exists()
8887
pre_commit = source_root / ".pre-commit-config.yaml"
8988
test_requirements = source_root / "test-requirements.txt"
9089

90+
pre_commit_text = pre_commit.read_text(encoding="utf-8")
91+
9192
# Get tool versions from pre-commit
9293
# Get correct names
9394
pre_commit_versions = {
9495
name.removesuffix("-mirror").removesuffix("-pre-commit"): version
95-
for name, version in yield_pre_commit_version_data(pre_commit)
96+
for name, version in yield_pre_commit_version_data(pre_commit_text)
9697
}
9798
changed = update_requirements(test_requirements, pre_commit_versions)
9899
return int(changed)

0 commit comments

Comments
 (0)