Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded committed Dec 3, 2018
1 parent 9cb10ab commit 3d9b65a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
13 changes: 7 additions & 6 deletions conans/client/graph/range_resolver.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

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


re_param = re.compile(r"^(include_prerelease|loose)\s*=\s*(True|False)$")
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)

Expand All @@ -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))
Expand Down Expand Up @@ -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:
Expand All @@ -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)


Expand Down
File renamed without changes.
48 changes: 48 additions & 0 deletions conans/test/unittests/client/graph/test_range_resolver.py
Original file line number Diff line number Diff line change
@@ -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")

This file was deleted.

0 comments on commit 3d9b65a

Please sign in to comment.