def _check_flake8_version() -> None: version = get_version("flake8") minimum_supported = VersionRepresentation(3, 7) if version < minimum_supported: raise IncompatibleVersionError( f"pysen only supports flake8 >= {minimum_supported}, " f"version {version} is not supported.")
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 _check_black_version() -> None: version = get_version("black") minimum_supported = VersionRepresentation(19, 10) if version < minimum_supported: raise IncompatibleVersionError( f"pysen only supports black >= {minimum_supported}, " f"version {version} is not supported." )
def test__get_isort_version() -> None: def get_version() -> VersionRepresentation: _get_isort_version.cache_clear() return _get_isort_version() distro = "pkg_resources.get_distribution" # pass case with mock.patch(distro, return_value=mock.Mock(version="4.3.21")): assert get_version() == VersionRepresentation(4, 3, 21) with mock.patch(distro, return_value=mock.Mock(version="5.1.2")): assert get_version() == VersionRepresentation(5, 1, 2) # incompatible version with pytest.raises(IncompatibleVersionError) as e: with mock.patch(distro, return_value=mock.Mock(version="3.0.0")): get_version() assert "version 3.0.0 is not supported" in str(e) # isort cannot be imported with pytest.raises(DistributionNotFound) as e: with mock.patch( distro, side_effect=pkg_resources.DistributionNotFound("req", "requires") ): get_version() assert "Expected isort to be installed" in str(e)
def test__check_version_compatibility() -> None: with pytest.raises(IncompatibleVersionError): _check_version_compatibility(True, VersionRepresentation(4, 0)) with pytest.raises(IncompatibleVersionError): _check_version_compatibility(False, VersionRepresentation(4, 0)) _check_version_compatibility(None, VersionRepresentation(4, 0)) _check_version_compatibility(True, VersionRepresentation(5, 0)) _check_version_compatibility(False, VersionRepresentation(5, 0)) _check_version_compatibility(None, VersionRepresentation(5, 0))
def test_to_black_compatible() -> None: setting = IsortSetting( force_single_line=False, include_trailing_comma=False, multi_line_output=1, ensure_newline_before_comments=False, force_grid_wrap=1, use_parentheses=False, ) with mock.patch( "pysen.ext.isort_wrapper._get_isort_version", return_value=VersionRepresentation(5, 0, 0), ): black_compat = setting.to_black_compatible() assert black_compat.multi_line_output == 3 assert black_compat.include_trailing_comma assert black_compat.force_grid_wrap == 0 assert black_compat.use_parentheses assert black_compat.ensure_newline_before_comments
def test__version() -> None: # version string MUST be in a format the VersionRepresentation understands VersionRepresentation.from_str(__version__)
def test___load_version() -> None: assert _load_version({}) is None assert _load_version({"version": "0.1.2a1"}) == VersionRepresentation(0, 1, 2, "a1") with pytest.raises(InvalidConfigurationError): _load_version({"version": "none"})
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.")
def test_is_compatible() -> None: assert VersionRepresentation(0, 5).is_compatible(VersionRepresentation(0, 5)) assert VersionRepresentation(0, 5).is_compatible( VersionRepresentation(0, 5, 7)) assert VersionRepresentation(0, 5).is_compatible( VersionRepresentation(0, 5, 1)) assert VersionRepresentation(0, 5, 5).is_compatible( VersionRepresentation(0, 5, 4)) assert VersionRepresentation(0, 5, 5).is_compatible( VersionRepresentation(0, 5, 6)) assert VersionRepresentation(3, 6).is_compatible(VersionRepresentation(3, 6)) assert not VersionRepresentation(3, 6).is_compatible( VersionRepresentation(3, 5)) assert not VersionRepresentation(3, 6, 8).is_compatible( VersionRepresentation(3, 5, 32)) assert not VersionRepresentation(2, 6).is_compatible( VersionRepresentation(3, 0)) assert not VersionRepresentation(4, 6).is_compatible( VersionRepresentation(3, 0))
def test_version_comp() -> None: def assert_rhs_is_larger(lhs: VersionRepresentation, rhs: VersionRepresentation) -> None: assert lhs < rhs assert not rhs < lhs assert_rhs_is_larger(VersionRepresentation(0, 4, 5), VersionRepresentation(1, 2, 3)) assert_rhs_is_larger(VersionRepresentation(1, 0), VersionRepresentation(2, 0)) assert_rhs_is_larger(VersionRepresentation(0, 5), VersionRepresentation(0, 6)) assert_rhs_is_larger(VersionRepresentation(0, 5), VersionRepresentation(0, 5, 1)) assert not VersionRepresentation(0, 5, 1, "b0") < VersionRepresentation( 0, 5, 1, "b1") assert not VersionRepresentation(0, 5, 1, "b1") < VersionRepresentation( 0, 5, 1, "b0")
def test_version_ops() -> None: assert VersionRepresentation(3, 6) == VersionRepresentation(3, 6, None, None) with pytest.raises(NotImplementedError): assert VersionRepresentation(3, 6) == "3"