| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from trio._tests.pytest_plugin import skip_if_optional_else_raise |
| 6 |
|
| 7 |
# imports in gen_exports that are not in `install_requires` in requirements |
| 8 |
try: |
| 9 |
import yaml # noqa: F401 |
| 10 |
except ImportError as error: |
| 11 |
skip_if_optional_else_raise(error) |
| 12 |
|
| 13 |
from trio._tools.sync_requirements import ( |
| 14 |
update_requirements, |
| 15 |
yield_pre_commit_version_data, |
| 16 |
) |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from pathlib import Path |
| 20 |
|
| 21 |
|
| 22 |
def test_yield_pre_commit_version_data() -> None: |
| 23 |
text = """ |
| 24 |
repos: |
| 25 |
- repo: https://github.com/astral-sh/ruff-pre-commit |
| 26 |
rev: v0.11.0 |
| 27 |
- repo: https://github.com/psf/black-pre-commit-mirror |
| 28 |
rev: 25.1.0 |
| 29 |
- bad: data |
| 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 requirements_file.read_text(encoding="utf-8") == """# comment |
| 55 |
# also comment but spaces line start |
| 56 |
waffles are delicious no equals |
| 57 |
black==3.1.5 ; specific version thingy |
| 58 |
mypy==1.15.0 |
| 59 |
ruff==1.2.7 |
| 60 |
# required by soupy cat""" |
| 61 |
|
| 62 |
|
| 63 |
def test_update_requirements_no_changes( |
| 64 |
tmp_path: Path, |
| 65 |
) -> None: |
| 66 |
requirements_file = tmp_path / "requirements.txt" |
| 67 |
assert not requirements_file.exists() |
| 68 |
original = """# comment |
| 69 |
# also comment but spaces line start |
| 70 |
waffles are delicious no equals |
| 71 |
black==3.1.4 ; specific version thingy |
| 72 |
mypy==1.15.0 |
| 73 |
ruff==1.2.5 |
| 74 |
# required by soupy cat""" |
| 75 |
requirements_file.write_text(original, encoding="utf-8") |
| 76 |
assert not update_requirements( |
| 77 |
requirements_file, {"black": "3.1.4", "ruff": "1.2.5"} |
| 78 |
) |
| 79 |
assert requirements_file.read_text(encoding="utf-8") == original |