Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading whitespaces and add support for entries with multiple version requirements #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions libyear/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
def get_requirement_name_and_version(requirement):
no_requirement = None, None, None
# Remove comments if they are on the same line
requirement = requirement.split()[0].strip()
requirement = requirement.split("#")[0].strip()
requirement = requirement.split("\\")[0].strip()
requirement = requirement.split("--")[0].strip()
requirement = requirement.split(";")[0].strip()
if not requirement:
return no_requirement

Expand All @@ -22,9 +25,17 @@ def get_requirement_name_and_version(requirement):
if not version_lt and not version:
return no_requirement

if version:
return name[0], version[0], None
return name[0], None, version_lt[0]
if version and not version_lt:
return name[0].strip(), version[0].strip(), None
if not version and version_lt:
return name[0].strip(), None, version_lt[0].strip()
if version and version_lt:
if version[0] == version_lt[0] or version_is_greater_than(version[0], version_lt[0]):
return name[0].strip(), None, version_lt[0].strip()
else:
return name[0].strip(), version[0].strip(), None

return no_requirement


def get_requirement_files(path_or_file):
Expand Down Expand Up @@ -67,3 +78,17 @@ def load_requirements(*requirements_paths):
if is_requirement(line)
)
return list(requirements)

def version_is_greater_than(greater, lesser):
greater_major, greater_minor, greater_patch = greater.replace("*", "999999999").split('.')
lesser_major, lesser_minor, lesser_patch = lesser.replace("*", "999999999").split('.')
try:
if int(greater_major) > int(lesser_major):
return True
if int(greater_major) == int(lesser_major) and int(greater_minor) > int(lesser_minor):
return True
if int(greater_major) == int(lesser_major) and int(greater_minor) == int(lesser_minor) and int(greater_patch) > int(lesser_patch):
return True
except ValueError:
return False
return False
33 changes: 33 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,36 @@ def test_gets_name_and_version_from_requirements_file_with_hashes():
}

assert ("appdirs", "1.4.3", None) in results


def test_get_requirement_name_and_version():
assert(("xmltodict", "0.10.0", None) == get_requirement_name_and_version("xmltodict==0.10.0"))
assert(("xmltodict", "0.10.0", None) == get_requirement_name_and_version("xmltodict<=0.10.0"))
assert(("xmltodict", "0.10.*", None) == get_requirement_name_and_version("xmltodict==0.10.*"))


def test_get_requirement_name_and_version_less_than():
assert(("xmltodict", None, "0.10.0") == get_requirement_name_and_version("xmltodict<0.10.0"))


def test_get_requirement_name_and_version_no_version():
no_version = (None, None, None)
assert(no_version == get_requirement_name_and_version("xmltodict"))
assert(no_version == get_requirement_name_and_version("xmltodict>=0.10.0"))
assert(no_version == get_requirement_name_and_version("xmltodict>0.10.0"))
assert(no_version == get_requirement_name_and_version("urllib3 @ https://github.com/urllib3/urllib3/archive/refs/tags/1.26.8.zip"))


def test_get_requirement_name_and_version_handle_spaces():
assert(("xmltodict", "0.10.0", None) == get_requirement_name_and_version("xmltodict == 0.10.0"))
assert(("xmltodict", "0.10.0", None) == get_requirement_name_and_version("xmltodict == 0.10.0 "))
assert(("xmltodict", "0.10.0", None) == get_requirement_name_and_version(" xmltodict == 0.10.0"))
assert(("xmltodict", "0.10.0", None) == get_requirement_name_and_version("xmltodict == 0.10.0"))
assert(("xmltodict", "0.10.0", None) == get_requirement_name_and_version("xmltodict == 0.10.0"))


def test_get_requirement_name_and_version_handle_complex():
assert(("requests[security]", "2.8.*", None) == get_requirement_name_and_version("requests[security] >= 2.8.1, == 2.8.*; python_version < \"2.7\""))
assert(("requests[security]", "2.8.1", None) == get_requirement_name_and_version("requests[security] <= 2.8.1, == 2.8.*; python_version < \"2.7\""))
assert(("requests[security]", None, "2.8.1") == get_requirement_name_and_version("requests[security] < 2.8.1, == 2.8.*; python_version < \"2.7\""))
assert(("FooProject", "1.2", None) == get_requirement_name_and_version("FooProject == 1.2 --global-option=\"--no-user-cfg\""))