From 3d9b65aa13de443dca59b6f7503c2367afab424b Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 3 Dec 2018 23:09:13 +0100 Subject: [PATCH] minor improvements --- conans/client/graph/range_resolver.py | 13 ++--- .../{test_client => client}/__init__.py | 0 .../client/graph/test_range_resolver.py | 48 ++++++++++++++++++ .../test_graph/test_range_resolver.py | 50 ------------------- 4 files changed, 55 insertions(+), 56 deletions(-) rename conans/test/unittests/{test_client => client}/__init__.py (100%) create mode 100644 conans/test/unittests/client/graph/test_range_resolver.py delete mode 100644 conans/test/unittests/test_client/test_graph/test_range_resolver.py diff --git a/conans/client/graph/range_resolver.py b/conans/client/graph/range_resolver.py index 3afa0cdce36..9fdf6cfdbb0 100644 --- a/conans/client/graph/range_resolver.py +++ b/conans/client/graph/range_resolver.py @@ -1,7 +1,7 @@ - import re -from conans.model.ref import ConanFileReference, ConanName + from conans.errors import ConanException +from conans.model.ref import ConanFileReference from conans.search.search import search_recipes @@ -9,7 +9,7 @@ re_version = re.compile(r"^((?!(include_prerelease|loose))[a-zA-Z0-9_+.\-~<>=|*^\(\)\s])*$") -def _parse_versionexpr(versionexpr, output): +def _parse_versionexpr(versionexpr): expression = [it.strip() for it in versionexpr.split(",")] assert 1 <= len(expression) <= 4, "Invalid expression for version_range '{}'".format(versionexpr) @@ -20,7 +20,7 @@ def _parse_versionexpr(versionexpr, output): for i, expr in enumerate(expression): match_param = re_param.match(expr) match_version = re_version.match(expr) - + if match_param == match_version: raise ConanException("Invalid version range '{}', failed in " "chunk '{}'".format(versionexpr, expr)) @@ -54,7 +54,7 @@ def satisfying(list_versions, versionexpr, output): """ from semver import SemVer, Range, max_satisfying - version_range, loose, include_prerelease = _parse_versionexpr(versionexpr, output) + version_range, loose, include_prerelease = _parse_versionexpr(versionexpr) # Check version range expression try: @@ -72,7 +72,8 @@ def satisfying(list_versions, versionexpr, output): output.warn("Version '%s' is not semver, cannot be compared with a range" % str(v)) # Search best matching version in range - result = max_satisfying(candidates, act_range, loose=loose, include_prerelease=include_prerelease) + result = max_satisfying(candidates, act_range, loose=loose, + include_prerelease=include_prerelease) return candidates.get(result) diff --git a/conans/test/unittests/test_client/__init__.py b/conans/test/unittests/client/__init__.py similarity index 100% rename from conans/test/unittests/test_client/__init__.py rename to conans/test/unittests/client/__init__.py diff --git a/conans/test/unittests/client/graph/test_range_resolver.py b/conans/test/unittests/client/graph/test_range_resolver.py new file mode 100644 index 00000000000..497dbecd6be --- /dev/null +++ b/conans/test/unittests/client/graph/test_range_resolver.py @@ -0,0 +1,48 @@ +import unittest + +from conans.client.graph.range_resolver import _parse_versionexpr +from conans.errors import ConanException + + +class ParseVersionExprTest(unittest.TestCase): + def test_backwards_compatibility(self): + self.assertEqual(_parse_versionexpr("2.3, 3.2"), ("2.3 3.2", True, False)) + self.assertEqual(_parse_versionexpr("2.3, <=3.2"), ("2.3 <=3.2", True, False)) + + def test_standard_semver(self): + self.assertEqual(_parse_versionexpr("*"), ("*", True, False)) + self.assertEqual(_parse_versionexpr(""), ("", True, False)) # Defaults to '*' + self.assertEqual(_parse_versionexpr("~1"), ("~1", True, False)) + self.assertEqual(_parse_versionexpr("~1.2.3-beta.2"), ("~1.2.3-beta.2", True, False)) + self.assertEqual(_parse_versionexpr("^0.0"), ("^0.0", True, False)) + self.assertEqual(_parse_versionexpr("1.2.3 - 2.3.4"), ("1.2.3 - 2.3.4", True, False)) + self.assertEqual(_parse_versionexpr(">=1.2.3 <1.(2+1).0"), + (">=1.2.3 <1.(2+1).0", True, False)) + + def test_only_loose(self): + self.assertEqual(_parse_versionexpr("2.3 ,3.2, loose=True"), ("2.3 3.2", True, False)) + self.assertEqual(_parse_versionexpr("2.3 3.2, loose=False"), ("2.3 3.2", False, False)) + self.assertEqual(_parse_versionexpr("2.3 3.2, loose = False"), ("2.3 3.2", False, False)) + self.assertEqual(_parse_versionexpr("2.3 3.2, loose = True"), ("2.3 3.2", True, False)) + + def test_only_prerelease(self): + self.assertEqual(_parse_versionexpr("2.3, 3.2, include_prerelease=False"), + ("2.3 3.2", True, False)) + self.assertEqual(_parse_versionexpr("2.3, 3.2, include_prerelease=True"), + ("2.3 3.2", True, True)) + + def test_both(self): + self.assertEqual(_parse_versionexpr("2.3, 3.2, loose=False, include_prerelease=True"), + ("2.3 3.2", False, True)) + self.assertEqual(_parse_versionexpr("2.3, 3.2, include_prerelease=True, loose=False"), + ("2.3 3.2", False, True)) + + def test_invalid(self): + self.assertRaises(ConanException, _parse_versionexpr, "loose=False, include_prerelease=True") + self.assertRaises(ConanException, _parse_versionexpr, "2.3, 3.2, unexpected=True") + self.assertRaises(ConanException, _parse_versionexpr, "2.3, 3.2, loose=Other") + self.assertRaises(ConanException, _parse_versionexpr, "2.3, 3.2, ") + self.assertRaises(ConanException, _parse_versionexpr, "2.3, 3.2, 1.2.3") + self.assertRaises(ConanException, _parse_versionexpr, + "2.3 3.2; loose=True, include_prerelease=True") + self.assertRaises(ConanException, _parse_versionexpr, "loose=True, 2.3 3.3") diff --git a/conans/test/unittests/test_client/test_graph/test_range_resolver.py b/conans/test/unittests/test_client/test_graph/test_range_resolver.py deleted file mode 100644 index 2fa25bc904b..00000000000 --- a/conans/test/unittests/test_client/test_graph/test_range_resolver.py +++ /dev/null @@ -1,50 +0,0 @@ -import unittest -from conans.client.graph.range_resolver import _parse_versionexpr -from conans.test.utils.tools import TestBufferConanOutput -from conans.errors import ConanException - - -class ParseVersionExpr(unittest.TestCase): - def test_backwards_compatibility(self): - output = TestBufferConanOutput() - self.assertEqual(_parse_versionexpr("2.3, 3.2", output), ("2.3 3.2", True, False)) - self.assertEqual(_parse_versionexpr("2.3, <=3.2", output), ("2.3 <=3.2", True, False)) - - def test_standard_semver(self): - output = TestBufferConanOutput() - self.assertEqual(_parse_versionexpr("*", output), ("*", True, False)) - self.assertEqual(_parse_versionexpr("", output), ("", True, False)) # Defaults to '*' - self.assertEqual(_parse_versionexpr("~1", output), ("~1", True, False)) - self.assertEqual(_parse_versionexpr("~1.2.3-beta.2", output), ("~1.2.3-beta.2", True, False)) - self.assertEqual(_parse_versionexpr("^0.0", output), ("^0.0", True, False)) - self.assertEqual(_parse_versionexpr("1.2.3 - 2.3.4", output), ("1.2.3 - 2.3.4", True, False)) - self.assertEqual(_parse_versionexpr(">=1.2.3 <1.(2+1).0", output), (">=1.2.3 <1.(2+1).0", True, False)) - - def test_only_loose(self): - output = TestBufferConanOutput() - self.assertEqual(_parse_versionexpr("2.3 ,3.2, loose=True", output), ("2.3 3.2", True, False)) - self.assertEqual(_parse_versionexpr("2.3 3.2, loose=False", output), ("2.3 3.2", False, False)) - self.assertEqual(_parse_versionexpr("2.3 3.2, loose = False", output), ("2.3 3.2", False, False)) - self.assertEqual(_parse_versionexpr("2.3 3.2, loose = True", output), ("2.3 3.2", True, False)) - - def test_only_prerelease(self): - output = TestBufferConanOutput() - self.assertEqual(_parse_versionexpr("2.3, 3.2, include_prerelease=False", output), ("2.3 3.2", True, False)) - self.assertEqual(_parse_versionexpr("2.3, 3.2, include_prerelease=True", output), ("2.3 3.2", True, True)) - - def test_both(self): - output = TestBufferConanOutput() - self.assertEqual(_parse_versionexpr("2.3, 3.2, loose=False, include_prerelease=True", output), - ("2.3 3.2", False, True)) - self.assertEqual(_parse_versionexpr("2.3, 3.2, include_prerelease=True, loose=False", output), - ("2.3 3.2", False, True)) - - def test_invalid(self): - output = TestBufferConanOutput() - self.assertRaises(ConanException, _parse_versionexpr, "loose=False, include_prerelease=True", output) - self.assertRaises(ConanException, _parse_versionexpr, "2.3, 3.2, unexpected=True", output) - self.assertRaises(ConanException, _parse_versionexpr, "2.3, 3.2, loose=Other", output) - self.assertRaises(ConanException, _parse_versionexpr, "2.3, 3.2, ", output) - self.assertRaises(ConanException, _parse_versionexpr, "2.3, 3.2, 1.2.3", output) - self.assertRaises(ConanException, _parse_versionexpr, "2.3 3.2; loose=True, include_prerelease=True", output) - self.assertRaises(ConanException, _parse_versionexpr, "loose=True, 2.3 3.3", output)