def get_version(name: str) -> VersionRepresentation: distro = _get_distro(name) if distro is None: raise DistributionNotFound( f"Expected {name} to be installed but pkg_resources could not find it.\n" f'Hint: Did you install "{name}" in the same Python environment as pysen?' ) return VersionRepresentation.from_str(distro.version)
def test__version() -> None: # version string MUST be in a format the VersionRepresentation understands VersionRepresentation.from_str(__version__)
def check_version(s: str, expected: VersionRepresentation) -> None: actual = VersionRepresentation.from_str(s) assert actual == expected assert s == str(expected)
def test_version_from_str() -> None: def check_version(s: str, expected: VersionRepresentation) -> None: actual = VersionRepresentation.from_str(s) assert actual == expected assert s == str(expected) cases = { ("0.601", VersionRepresentation(0, 601)), ("3.0.8", VersionRepresentation(3, 0, 8)), ("3.6.8a1", VersionRepresentation(3, 6, 8, "a1")), ("3.6a1", VersionRepresentation(3, 6, None, "a1")), ("3.6b0", VersionRepresentation(3, 6, None, "b0")), ("3.6rc993", VersionRepresentation(3, 6, None, "rc993")), } for case in cases: check_version(*case) with pytest.raises(ValueError): # prelease phase must be either a, b, or rc VersionRepresentation.from_str("3.6.8alpha1") with pytest.raises(ValueError): # MUST have a pre-release number VersionRepresentation.from_str("3.6.8a") with pytest.raises(ValueError): # MUST NOT start with zero followed by another number VersionRepresentation.from_str("03.1") with pytest.raises(ValueError): # MUST NOT start with zero followed by another number VersionRepresentation.from_str("00.1") with pytest.raises(ValueError): # MUST have minor VersionRepresentation.from_str("3") with pytest.raises(ValueError): # too many dots VersionRepresentation.from_str("3.0.100.1") with pytest.raises(ValueError): VersionRepresentation.from_str("3.")