From: Daira Hopwood <daira@jacaranda.org>
Date: Fri, 11 Sep 2015 23:59:51 +0000 (+0100)
Subject: Avoid spurious errors when an imported version is consistent with pkg_resources
X-Git-Url: https://git.rkrishnan.org/specifications/components/com_hotproperty/%22doc.html/?a=commitdiff_plain;h=refs%2Fpull%2F188%2Fhead;p=tahoe-lafs%2Ftahoe-lafs.git

Avoid spurious errors when an imported version is consistent with pkg_resources
but not parseable; also improve related error reporting. fixes ticket:2499

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
---

diff --git a/src/allmydata/__init__.py b/src/allmydata/__init__.py
index 0b0c5f81..fa512a13 100644
--- a/src/allmydata/__init__.py
+++ b/src/allmydata/__init__.py
@@ -146,7 +146,8 @@ def get_platform():
 from allmydata.util import verlib
 def normalized_version(verstr, what=None):
     try:
-        return verlib.NormalizedVersion(verlib.suggest_normalized_version(verstr))
+        suggested = verlib.suggest_normalized_version(verstr) or verstr
+        return verlib.NormalizedVersion(suggested)
     except (StandardError, verlib.IrrationalVersionError):
         cls, value, trace = sys.exc_info()
         raise PackagingError, ("could not parse %s due to %s: %s"
@@ -351,6 +352,11 @@ def cross_check(pkg_resources_vers_and_locs, imported_vers_and_locs_list):
                               % (name, pr_ver, pr_loc, imp_comment))
                 continue
 
+            # If the pkg_resources version is identical to the imported version, don't attempt
+            # to normalize them, since it is unnecessary and may fail (ticket #2499).
+            if imp_ver != 'unknown' and pr_ver == imp_ver:
+                continue
+
             try:
                 pr_normver = normalized_version(pr_ver)
             except Exception, e:
diff --git a/src/allmydata/test/test_version.py b/src/allmydata/test/test_version.py
index 7851f91a..ee149b0e 100644
--- a/src/allmydata/test/test_version.py
+++ b/src/allmydata/test/test_version.py
@@ -91,17 +91,24 @@ class CheckRequirement(unittest.TestCase):
         self.failIfEqual(errors, [])
         self.failUnlessEqual([e for e in errors if "was not found by pkg_resources" not in e], [])
 
-    def test_cross_check_ticket_1355(self):
+    def test_cross_check_unparseable_versions(self):
         # The bug in #1355 is triggered when a version string from either pkg_resources or import
         # is not parseable at all by normalized_version.
 
         res = cross_check({"foo": ("unparseable", "")}, [("foo", ("1.0", "", None))])
         self.failUnlessEqual(len(res), 1)
         self.failUnlessIn("by pkg_resources could not be parsed", res[0])
+        self.failUnlessIn("due to IrrationalVersionError", res[0])
 
         res = cross_check({"foo": ("1.0", "")}, [("foo", ("unparseable", "", None))])
         self.failUnlessEqual(len(res), 1)
         self.failUnlessIn(") could not be parsed", res[0])
+        self.failUnlessIn("due to IrrationalVersionError", res[0])
+
+        # However, an error should not be triggered when the version strings are unparseable
+        # but equal (#2499).
+        res = cross_check({"foo": ("unparseable", "")}, [("foo", ("unparseable", "", None))])
+        self.failUnlessEqual(res, [])
 
     def test_cross_check(self):
         res = cross_check({}, [])
@@ -134,10 +141,18 @@ class CheckRequirement(unittest.TestCase):
         res = cross_check({"zope.interface": ("1.0", "")}, [("zope.interface", ("unknown", "", None))])
         self.failUnlessEqual(res, [])
 
+        res = cross_check({"zope.interface": ("unknown", "")}, [("zope.interface", ("unknown", "", None))])
+        self.failUnlessEqual(len(res), 1)
+        self.failUnlessIn("could not be parsed", res[0])
+
         res = cross_check({"foo": ("1.0", "")}, [("foo", ("unknown", "", None))])
         self.failUnlessEqual(len(res), 1)
         self.failUnlessIn("could not find a version number", res[0])
 
+        res = cross_check({"foo": ("unknown", "")}, [("foo", ("unknown", "", None))])
+        self.failUnlessEqual(len(res), 1)
+        self.failUnlessIn("could not be parsed", res[0])
+
         # When pkg_resources and import both find a package, there is only a warning if both
         # the version and the path fail to match.