From 1bb19d055aeb9d2dc5ab731fb527b976101cac96 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Fri, 29 Jul 2016 15:56:49 +0300 Subject: [PATCH] PY-20242 Fixed: PyCharm thinks package requirement is not satisfied, but it is. Update matching version against specs so '===' relation is supported now. Also PyRequirementVersionSpec become responsible for matching version against itself. --- .../python/packaging/PyRequirement.java | 21 +++++--------- .../requirement/PyRequirementRelation.java | 29 ------------------- .../requirement/PyRequirementVersionSpec.java | 25 ++++++++++++++++ .../python/packaging/PyRequirementTest.java | 18 ++++++++---- 4 files changed, 45 insertions(+), 48 deletions(-) diff --git a/python/openapi/src/com/jetbrains/python/packaging/PyRequirement.java b/python/openapi/src/com/jetbrains/python/packaging/PyRequirement.java index b9517de06777..d4ffb80ff961 100644 --- a/python/openapi/src/com/jetbrains/python/packaging/PyRequirement.java +++ b/python/openapi/src/com/jetbrains/python/packaging/PyRequirement.java @@ -22,7 +22,6 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.webcore.packaging.PackageVersionComparator; import com.jetbrains.python.packaging.requirement.PyRequirementRelation; import com.jetbrains.python.packaging.requirement.PyRequirementVersionNormalizer; import com.jetbrains.python.packaging.requirement.PyRequirementVersionSpec; @@ -293,20 +292,14 @@ public class PyRequirement { @Nullable public PyPackage match(@NotNull List packages) { - for (PyPackage pkg : packages) { - if (normalizeName(myName).equalsIgnoreCase(pkg.getName())) { - for (PyRequirementVersionSpec spec : myVersionSpecs) { - final int cmp = PackageVersionComparator.VERSION_COMPARATOR.compare(pkg.getVersion(), spec.getVersion()); + final String normalizedName = normalizeName(myName); - if (!spec.getRelation().isSuccessful(cmp)) { - return null; - } - } - return pkg; - } - } - - return null; + return packages + .stream() + .filter(pkg -> normalizedName.equalsIgnoreCase(pkg.getName())) + .findAny() + .filter(pkg -> myVersionSpecs.stream().allMatch(spec -> spec.matches(pkg.getVersion()))) + .orElse(null); } @Nullable diff --git a/python/openapi/src/com/jetbrains/python/packaging/requirement/PyRequirementRelation.java b/python/openapi/src/com/jetbrains/python/packaging/requirement/PyRequirementRelation.java index da56b973932a..6e1a96086106 100644 --- a/python/openapi/src/com/jetbrains/python/packaging/requirement/PyRequirementRelation.java +++ b/python/openapi/src/com/jetbrains/python/packaging/requirement/PyRequirementRelation.java @@ -16,7 +16,6 @@ package com.jetbrains.python.packaging.requirement; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; public enum PyRequirementRelation { @@ -41,32 +40,4 @@ public enum PyRequirementRelation { public String toString() { return myValue; } - - @Nullable - public static PyRequirementRelation fromString(@NotNull String value) { - for (PyRequirementRelation relation : PyRequirementRelation.values()) { - if (relation.myValue.equals(value)) { - return relation; - } - } - return null; - } - - public boolean isSuccessful(int comparisonResult) { - switch (this) { - case LT: - return comparisonResult < 0; - case LTE: - return comparisonResult <= 0; - case GT: - return comparisonResult > 0; - case GTE: - return comparisonResult >= 0; - case EQ: - return comparisonResult == 0; - case NE: - return comparisonResult != 0; - } - return false; - } } diff --git a/python/openapi/src/com/jetbrains/python/packaging/requirement/PyRequirementVersionSpec.java b/python/openapi/src/com/jetbrains/python/packaging/requirement/PyRequirementVersionSpec.java index 259dccf2c1a6..62f6afa43a93 100644 --- a/python/openapi/src/com/jetbrains/python/packaging/requirement/PyRequirementVersionSpec.java +++ b/python/openapi/src/com/jetbrains/python/packaging/requirement/PyRequirementVersionSpec.java @@ -17,6 +17,8 @@ package com.jetbrains.python.packaging.requirement; import org.jetbrains.annotations.NotNull; +import static com.intellij.webcore.packaging.PackageVersionComparator.VERSION_COMPARATOR; + public class PyRequirementVersionSpec { @NotNull @@ -58,4 +60,27 @@ public class PyRequirementVersionSpec { public String getVersion() { return myVersion; } + + public boolean matches(@NotNull String version) { + switch (myRelation) { + case LT: + return VERSION_COMPARATOR.compare(version, myVersion) < 0; + case LTE: + return VERSION_COMPARATOR.compare(version, myVersion) <= 0; + case GT: + return VERSION_COMPARATOR.compare(version, myVersion) > 0; + case GTE: + return VERSION_COMPARATOR.compare(version, myVersion) >= 0; + case EQ: + return VERSION_COMPARATOR.compare(version, myVersion) == 0; + case NE: + return VERSION_COMPARATOR.compare(version, myVersion) != 0; + case COMPATIBLE: + return false; // TODO: implement matching version against compatible relation + case STR_EQ: + return version.equals(myVersion); + default: + return false; + } + } } diff --git a/python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java b/python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java index bac7467bb314..971e70402a26 100644 --- a/python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java +++ b/python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java @@ -2203,10 +2203,10 @@ public class PyRequirementTest extends PyTestCase { // PY-6355 public void testTrailingZeroesInVersion() { - final PyRequirement req080 = PyRequirement.fromLine("foo==0.8.0"); - final PyPackage pack08 = new PyPackage("foo", "0.8", null, Collections.emptyList()); - assertNotNull(req080); - assertNotNull(req080.match(Collections.singletonList(pack08))); + final PyRequirement req = PyRequirement.fromLine("foo==0.8.0"); + final PyPackage pkg = new PyPackage("foo", "0.8", null, Collections.emptyList()); + assertNotNull(req); + assertEquals(pkg, req.match(Collections.singletonList(pkg))); } // PY-6438 @@ -2214,7 +2214,15 @@ public class PyRequirementTest extends PyTestCase { final PyRequirement req = PyRequirement.fromLine("pyramid_zcml"); final PyPackage pkg = new PyPackage("pyramid-zcml", "0.1", null, Collections.emptyList()); assertNotNull(req); - assertNotNull(req.match(Collections.singletonList(pkg))); + assertEquals(pkg, req.match(Collections.singletonList(pkg))); + } + + // PY-20242 + public void testVersionInterpretedAsString() { + final PyRequirement req = PyRequirement.fromLine("foo===version"); + final PyPackage pkg = new PyPackage("foo", "version", null, Collections.emptyList()); + assertNotNull(req); + assertEquals(pkg, req.match(Collections.singletonList(pkg))); } // OPTIONS