Repository: Midnighter/dependency-info

Issue:
Parsing of packages fails for most package strings (requirement)
<!--

Before you create a new problem report, please look through the list of existing open
 and closed issues to see if there are similar ones.
 
https://github.com/Midnighter/dependency-info/issues

-->

#### Problem description
Most package strings which can occur in setup.cfg are currently not parsed correctly by the package resulting in "not installed".
E.g.
```
        ("pip", "pip"),
        ("pip (~=1.4)", "pip"),
        ("pip (==5.18.3)", "pip"),
        ("pip (<1.4.6)", "pip"),
        ("pip (>1.4.6)", "pip"),
        ("pip>=2.0.5", "pip"),
        ("pip==1.19.3", "pip"),
        ('pip>=6.1; extra == "development"', "pip"),
        ('pip>=6.1 ; extra == "development"', "pip"),
```
Pull request with fix comes in a minute.




Discussion:
matthiaskoenig: @Midnighter Could you merge this and make a bugfix release?

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 432ff96..8e22715 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -4,6 +4,7 @@ Change Log
 
 Next Release
 ------------
+* Improved parsing of package information
 
 1.6.0 (2020-10-11)
 ------------------
diff --git a/README.rst b/README.rst
index 69fba35..9db8039 100644
--- a/README.rst
+++ b/README.rst
@@ -75,6 +75,6 @@ Example
 Copyright
 =========
 
-* Copyright © 2018-2020, Moritz E. Beber.
+* Copyright © 2018-2021, Moritz E. Beber.
 * Free software distributed under the `Apache Software License 2.0
   <https://www.apache.org/licenses/LICENSE-2.0>`_.
diff --git a/src/depinfo/info.py b/src/depinfo/info.py
index 0be5ebd..54b9c5b 100644
--- a/src/depinfo/info.py
+++ b/src/depinfo/info.py
@@ -17,6 +17,7 @@
 
 
 import platform
+import re
 from typing import Dict, Iterable, Tuple
 
 
@@ -53,7 +54,7 @@ def _get_package_version(requirement: str) -> Tuple[str, str]:
             environment.
 
     """
-    package = requirement.split(";", 1)[0].strip()
+    package = re.split(r"[;<>=\s]", requirement)[0]
     return package, version(package)
 
 


Test patch:
diff --git a/tests/test_info.py b/tests/test_info.py
index 6ee81b1..148086d 100644
--- a/tests/test_info.py
+++ b/tests/test_info.py
@@ -22,6 +22,7 @@ from typing import Dict
 import pytest
 
 import depinfo
+from depinfo.info import _get_package_version
 
 
 def test_get_sys_info() -> None:
@@ -58,6 +59,26 @@ def test_print_info(capsys, blob: Dict[str, str], output: str) -> None:
     assert captured.out == output
 
 
+@pytest.mark.parametrize(
+    "requirement, package",
+    [
+        ("pip", "pip"),
+        ("pip (~=1.4)", "pip"),
+        ("pip (==5.18.3)", "pip"),
+        ("pip (<1.4.6)", "pip"),
+        ("pip (>1.4.6)", "pip"),
+        ("pip>=2.0.5", "pip"),
+        ("pip==1.19.3", "pip"),
+        ('pip>=6.1; extra == "development"', "pip"),
+        ('pip>=6.1 ; extra == "development"', "pip"),
+    ],
+)
+def test_get_package_version(requirement, package):
+    """Expect package to be parsed from requirement."""
+    p, _ = _get_package_version(requirement)
+    assert p == package
+
+
 def test_print_dependencies(capsys) -> None:
     """Expect all printed information in order."""
     depinfo.print_dependencies("depinfo")