|
| 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 |
0 commit comments