示例工程 27-pyproject-dependency-management

27-pyproject-dependency-management/03-version-constraints/test_version_constraints.py

python · 0.9 KB

"""test_version_constraints.py"""
 
from __future__ import annotations
 
import pytest
 
pytest.importorskip("packaging")
 
from version_constraints import main, matches
 
 
def test_range_and_compatible() -> None:
    versions = ["1.1.0", "1.2.0", "1.9.9", "2.0.0"]
    assert matches(">=1.2,<2", versions) == {
        "1.1.0": False,
        "1.2.0": True,
        "1.9.9": True,
        "2.0.0": False,
    }
    assert matches("~=1.4", ["1.3.0", "1.4.0", "1.9.0", "2.0.0"]) == {
        "1.3.0": False,
        "1.4.0": True,
        "1.9.0": True,
        "2.0.0": False,
    }
 
 
def test_wildcard() -> None:
    assert matches("==1.5.*", ["1.5.0", "1.5.9", "1.6.0"]) == {
        "1.5.0": True,
        "1.5.9": True,
        "1.6.0": False,
    }
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert ">=1.2,<2:" in out
    assert "~=1.4:" in out