Exemple #1
0
    def test_empty_specifier(self, version):
        spec = PythonSpecifierSet(prereleases=True)

        assert version in spec
        assert spec.contains(version)
        assert PythonVersion(version) in spec
        assert spec.contains(PythonVersion(version))
Exemple #2
0
    def test_specifiers(self, version, spec, expected):
        spec = PythonSpecifier(spec, prereleases=True)

        if expected:
            # Test that the plain string form works
            assert version in spec
            assert spec.contains(version)

            # Test that the version instance form works
            assert PythonVersion(version) in spec
            assert spec.contains(PythonVersion(version))
        else:
            # Test that the plain string form works
            assert version not in spec
            assert not spec.contains(version)

            # Test that the version instance form works
            assert PythonVersion(version) not in spec
            assert not spec.contains(PythonVersion(version))
Exemple #3
0
 def test_major_version(self):
     assert PythonVersion("2.1.0").major == 2
Exemple #4
0
 def test_dunder_op_returns_notimplemented(self, op):
     method = getattr(PythonVersion, "__{0}__".format(op))
     assert method(PythonVersion("1"), 1) is NotImplemented
Exemple #5
0
    def test_compare_other(self, op, expected):
        other = pretend.stub(**{
            "__{0}__".format(op): lambda other: NotImplemented
        })

        assert getattr(operator, op)(PythonVersion("1"), other) is expected
Exemple #6
0
 def test_version_rc_and_c_equals(self):
     assert PythonVersion("1.0rc1") == PythonVersion("1.0c1")
Exemple #7
0
 def test_normalized_versions(self, version, normalized):
     assert str(PythonVersion(version)) == normalized
Exemple #8
0
 def test_valid_versions(self, version):
     PythonVersion(version)
Exemple #9
0
from itertools import chain
from verspec.loose import Version, Specifier, SpecifierSet, InvalidSpecifier
from verspec.python import PythonVersion, PythonSpecifier, PythonSpecifierSet

from .app_version import version as bfg_version
from .exceptions import VersionError
from .iterutils import iterate

__all__ = [
    'bfg_version', 'check_version', 'detect_version', 'InvalidSpecifier',
    'python_version', 'PythonSpecifier', 'PythonSpecifierSet', 'PythonVersion',
    'simplify_specifiers', 'Specifier', 'SpecifierSet', 'Version',
    'VersionError'
]

bfg_version = PythonVersion(bfg_version)
# Strip trailing "+" from Python version. Some versions in distros have this...
python_version = PythonVersion(re.sub(r'\+$', '', platform.python_version()))


def simplify_specifiers(spec):
    """Try to simplify a SpecifierSet by combining redundant specifiers."""
    def key(s):
        return (s.version, 1 if s.operator in ['>=', '<'] else 2)

    def in_bounds(v, lo, hi):
        if lo and v not in lo:
            return False
        if hi and v not in hi:
            return False
        return True
Exemple #10
0
 def test_version_local(self, version, local):
     assert PythonVersion(version).local == local
Exemple #11
0
 def test_version_pre(self, version, pre):
     assert PythonVersion(version).pre == pre
Exemple #12
0
 def test_version_epoch(self, version, epoch):
     assert PythonVersion(version).epoch == epoch
Exemple #13
0
 def test_version_release(self, version, release):
     assert PythonVersion(version).release == release
Exemple #14
0
 def test_version_base_version(self, version, base_version):
     assert PythonVersion(version).base_version == base_version
Exemple #15
0
 def test_version_public(self, version, public):
     assert PythonVersion(version).public == public
Exemple #16
0
 def test_version_hash(self, version):
     assert hash(PythonVersion(version)) == hash(PythonVersion(version))
Exemple #17
0
 def test_minor_version(self):
     assert PythonVersion("2.1.0").minor == 1
     assert PythonVersion("2").minor == 0
Exemple #18
0
 def test_version_dev(self, version, dev):
     assert PythonVersion(version).dev == dev
Exemple #19
0
 def test_micro_version(self):
     assert PythonVersion("2.1.3").micro == 3
     assert PythonVersion("2.1").micro == 0
     assert PythonVersion("2").micro == 0
Exemple #20
0
class TestPythonSpecifierSet:
    @pytest.mark.parametrize("version", VERSIONS)
    def test_empty_specifier(self, version):
        spec = PythonSpecifierSet(prereleases=True)

        assert version in spec
        assert spec.contains(version)
        assert PythonVersion(version) in spec
        assert spec.contains(PythonVersion(version))

    def test_specifier_prereleases_explicit(self):
        spec = PythonSpecifierSet()
        assert not spec.prereleases
        assert "1.0.dev1" not in spec
        assert not spec.contains("1.0.dev1")
        spec.prereleases = True
        assert spec.prereleases
        assert "1.0.dev1" in spec
        assert spec.contains("1.0.dev1")

        spec = PythonSpecifierSet(prereleases=True)
        assert spec.prereleases
        assert "1.0.dev1" in spec
        assert spec.contains("1.0.dev1")
        spec.prereleases = False
        assert not spec.prereleases
        assert "1.0.dev1" not in spec
        assert not spec.contains("1.0.dev1")

        spec = PythonSpecifierSet(prereleases=True)
        assert spec.prereleases
        assert "1.0.dev1" in spec
        assert spec.contains("1.0.dev1")
        spec.prereleases = None
        assert not spec.prereleases
        assert "1.0.dev1" not in spec
        assert not spec.contains("1.0.dev1")

    def test_specifier_contains_prereleases(self):
        spec = PythonSpecifierSet()
        assert spec.prereleases is None
        assert not spec.contains("1.0.dev1")
        assert spec.contains("1.0.dev1", prereleases=True)

        spec = PythonSpecifierSet(prereleases=True)
        assert spec.prereleases
        assert spec.contains("1.0.dev1")
        assert not spec.contains("1.0.dev1", prereleases=False)

    @pytest.mark.parametrize(
        ("specifier", "specifier_prereleases", "prereleases", "input",
         "expected"),
        [
            # General test of the filter method
            ("", None, None, ["1.0", "2.0a1"], ["1.0"]),
            (">=1.0.dev1", None, None, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
            ("", None, None, ["1.0a1"], ["1.0a1"]),
            ("", None, None, ["1.0", PythonVersion("2.0")
                              ], ["1.0", PythonVersion("2.0")]),
            # Test overriding with the prereleases parameter on filter
            ("", None, False, ["1.0a1"], []),
            (">=1.0.dev1", None, False, ["1.0", "2.0a1"], ["1.0"]),
            ("", None, True, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
            # Test overriding with the overall specifier
            ("", True, None, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
            ("", False, None, ["1.0", "2.0a1"], ["1.0"]),
            (">=1.0.dev1", True, None, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
            (">=1.0.dev1", False, None, ["1.0", "2.0a1"], ["1.0"]),
            ("", True, None, ["1.0a1"], ["1.0a1"]),
            ("", False, None, ["1.0a1"], []),
        ],
    )
    def test_specifier_filter(self, specifier_prereleases, specifier,
                              prereleases, input, expected):
        if specifier_prereleases is None:
            spec = PythonSpecifierSet(specifier)
        else:
            spec = PythonSpecifierSet(specifier,
                                      prereleases=specifier_prereleases)

        kwargs = ({
            "prereleases": prereleases
        } if prereleases is not None else {})

        assert list(spec.filter(input, **kwargs)) == expected

    @pytest.mark.parametrize(
        ("specifier", "expected"),
        [
            # Single item specifiers should just be reflexive
            ("!=2.0", "!=2.0"),
            ("<2.0", "<2.0"),
            ("<=2.0", "<=2.0"),
            ("==2.0", "==2.0"),
            (">2.0", ">2.0"),
            (">=2.0", ">=2.0"),
            ("~=2.0", "~=2.0"),
            # Spaces should be removed
            ("< 2", "<2"),
            # Multiple item specifiers should work
            ("!=2.0,>1.0", "!=2.0,>1.0"),
            ("!=2.0 ,>1.0", "!=2.0,>1.0"),
        ],
    )
    def test_specifiers_str_and_repr(self, specifier, expected):
        spec = PythonSpecifierSet(specifier)

        assert str(spec) == expected
        assert repr(spec) == "<PythonSpecifierSet({0})>".format(repr(expected))

    @pytest.mark.parametrize("specifier", SPECIFIERS + LOOSE_SPECIFIERS)
    def test_specifiers_hash(self, specifier):
        assert (hash(PythonSpecifierSet(specifier)) == hash(
            PythonSpecifierSet(specifier)))

    @pytest.mark.parametrize(("left", "right", "expected"),
                             [(">2.0", "<5.0", ">2.0,<5.0")])
    def test_specifiers_combine(self, left, right, expected):
        result = PythonSpecifierSet(left) & PythonSpecifierSet(right)
        assert result == PythonSpecifierSet(expected)

        result = PythonSpecifierSet(left) & right
        assert result == PythonSpecifierSet(expected)

        result = (PythonSpecifierSet(left, prereleases=True)
                  & PythonSpecifierSet(right))
        assert result == PythonSpecifierSet(expected)
        assert result.prereleases

        result = (PythonSpecifierSet(left, prereleases=False)
                  & PythonSpecifierSet(right))
        assert result == PythonSpecifierSet(expected)
        assert not result.prereleases

        result = (PythonSpecifierSet(left)
                  & PythonSpecifierSet(right, prereleases=True))
        assert result == PythonSpecifierSet(expected)
        assert result.prereleases

        result = (PythonSpecifierSet(left)
                  & PythonSpecifierSet(right, prereleases=False))
        assert result == PythonSpecifierSet(expected)
        assert not result.prereleases

        result = (PythonSpecifierSet(left, prereleases=True)
                  & PythonSpecifierSet(right, prereleases=True))
        assert result == PythonSpecifierSet(expected)
        assert result.prereleases

        result = (PythonSpecifierSet(left, prereleases=False)
                  & PythonSpecifierSet(right, prereleases=False))
        assert result == PythonSpecifierSet(expected)
        assert not result.prereleases

        with pytest.raises(ValueError):
            result = (PythonSpecifierSet(left, prereleases=True)
                      & PythonSpecifierSet(right, prereleases=False))

        with pytest.raises(ValueError):
            result = (PythonSpecifierSet(left, prereleases=False)
                      & PythonSpecifierSet(right, prereleases=True))

    def test_specifiers_combine_not_implemented(self):
        with pytest.raises(TypeError):
            PythonSpecifierSet() & 12

    @pytest.mark.parametrize(
        ("left", "right", "op"),
        itertools.chain(
            *
            # Verify that the equal (==) operator works correctly
            [[(x, x, operator.eq) for x in SPECIFIERS]] +
            # Verify that the not equal (!=) operator works correctly
            [[(x, y, operator.ne) for j, y in enumerate(SPECIFIERS) if i != j]
             for i, x in enumerate(SPECIFIERS)]),
    )
    def test_comparison_true(self, left, right, op):
        assert op(PythonSpecifierSet(left), PythonSpecifierSet(right))
        assert op(PythonSpecifierSet(left), PythonSpecifier(right))
        assert op(PythonSpecifier(left), PythonSpecifierSet(right))
        assert op(left, PythonSpecifierSet(right))
        assert op(PythonSpecifierSet(left), right)

    @pytest.mark.parametrize(
        ("left", "right", "op"),
        itertools.chain(
            *
            # Verify that the equal (==) operator works correctly
            [[(x, x, operator.ne) for x in SPECIFIERS]] +
            # Verify that the not equal (!=) operator works correctly
            [[(x, y, operator.eq) for j, y in enumerate(SPECIFIERS) if i != j]
             for i, x in enumerate(SPECIFIERS)]),
    )
    def test_comparison_false(self, left, right, op):
        assert not op(PythonSpecifierSet(left), PythonSpecifierSet(right))
        assert not op(PythonSpecifierSet(left), PythonSpecifier(right))
        assert not op(PythonSpecifier(left), PythonSpecifierSet(right))
        assert not op(left, PythonSpecifierSet(right))
        assert not op(PythonSpecifierSet(left), right)

    @pytest.mark.parametrize(("left", "right"), [("==2.8.0", "==2.8")])
    def test_comparison_canonicalizes(self, left, right):
        assert PythonSpecifierSet(left) == PythonSpecifierSet(right)
        assert left == PythonSpecifierSet(right)
        assert PythonSpecifierSet(left) == right

    def test_comparison_non_specifier(self):
        assert PythonSpecifierSet("==1.0") != 12
        assert not PythonSpecifierSet("==1.0") == 12

    @pytest.mark.parametrize(
        ("version", "specifier", "expected"),
        [
            ("1.0.0+local", "==1.0.0", True),
            ("1.0.0+local", "!=1.0.0", False),
            ("1.0.0+local", "<=1.0.0", True),
            ("1.0.0+local", ">=1.0.0", True),
            ("1.0.0+local", "<1.0.0", False),
            ("1.0.0+local", ">1.0.0", False),
        ],
    )
    def test_comparison_ignores_local(self, version, specifier, expected):
        assert (PythonVersion(version)
                in PythonSpecifierSet(specifier)) == expected
Exemple #21
0
 def test_compare_with_pythonversion(self, op):
     method = getattr(LooseVersion, "__{0}__".format(op))
     assert method(LooseVersion("0"), PythonVersion("1")) is NotImplemented
Exemple #22
0
 def test_comparison_ignores_local(self, version, specifier, expected):
     assert (PythonVersion(version)
             in PythonSpecifierSet(specifier)) == expected
Exemple #23
0
 def test_version_post(self, version, post):
     assert PythonVersion(version).post == post
Exemple #24
0
 def test_version_str_repr(self, version, expected):
     assert str(PythonVersion(version)) == expected
     assert (repr(PythonVersion(version)) ==
             "<PythonVersion({0})>".format(repr(expected)))
Exemple #25
0
 def test_version_is_postrelease(self, version, expected):
     assert PythonVersion(version).is_postrelease is expected
Exemple #26
0
 def test_comparison_false(self, left, right, op):
     assert not op(PythonVersion(left), PythonVersion(right))
Exemple #27
0
 def test_specifier_explicit_python(self):
     assert LooseSpecifier("==1.0").contains(PythonVersion("1.0"))
Exemple #28
0
 def test_invalid_versions(self, version):
     with pytest.raises(InvalidVersion):
         PythonVersion(version)