Skip to content

Commit d4e22ea

Browse files
committed
Add tests
1 parent 1d10fca commit d4e22ea

File tree

6 files changed

+49
-23
lines changed

6 files changed

+49
-23
lines changed

pip/baseparser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def expand_default(self, option):
113113

114114

115115
class CustomOptionParser(optparse.OptionParser):
116+
116117
def insert_option_group(self, idx, *args, **kwargs):
117118
"""Insert an OptionGroup at a given position."""
118119
group = self.add_option_group(*args, **kwargs)

pip/index.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,8 @@ def _link_package_versions(self, link, search):
646646
try:
647647
support_this_python = check_requires_python(link.requires_python)
648648
except specifiers.InvalidSpecifier:
649+
logger.debug("Package %s has an invalid Requires-Python entry: %s",
650+
link.filename, link.requires_python)
649651
support_this_python = True
650652

651653
if not support_this_python:
@@ -841,7 +843,8 @@ def links(self):
841843
url = self.clean_link(
842844
urllib_parse.urljoin(self.base_url, href)
843845
)
844-
pyrequire = unescape(anchor.get('data-requires-python'))
846+
pyrequire = anchor.get('data-requires-python')
847+
pyrequire = unescape(pyrequire) if pyrequire else None
845848
yield Link(url, self, requires_python=pyrequire)
846849

847850
_clean_re = re.compile(r'[^a-z0-9$&+,/:;=?@.#%_\\|-]', re.I)
@@ -863,10 +866,11 @@ def __init__(self, url, comes_from=None, requires_python=None):
863866
url:
864867
url of the resource pointed to (href of the link)
865868
comes_from:
866-
<Not sure>
869+
instance of HTMLPage where the link was found, or string.
867870
requires_python:
868871
String containing the `Requires-Python` metadata field, specified
869-
in PEP 345.
872+
in PEP 345. This may be specified by a data-requires-python
873+
attribute in the HTML link tag, as described in PEP 503.
870874
"""
871875

872876
# url can be a UNC windows share
@@ -875,10 +879,7 @@ def __init__(self, url, comes_from=None, requires_python=None):
875879

876880
self.url = url
877881
self.comes_from = comes_from
878-
if not requires_python:
879-
self.requires_python = None
880-
else:
881-
self.requires_python = requires_python
882+
self.requires_python = requires_python if requires_python else None
882883

883884
def __str__(self):
884885
if self.requires_python:

pip/utils/packaging.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,18 @@
1010

1111
def check_requires_python(requires_python):
1212
"""
13-
Check if the python version in used match the `requires_python` specifier.
13+
Check if the python version in use match the `requires_python` specifier.
1414
15-
Return `True` if the version of python in use matches the requirement.
16-
Return `False` if the version of python in use does not matches the
17-
requirement. Raises an InvalidSpecifier if `requires_python` have an
18-
invalid format.
15+
Returns `True` if the version of python in use matches the requirement.
16+
Returns `False` if the version of python in use does not matches the
17+
requirement.
18+
19+
Raises an InvalidSpecifier if `requires_python` have an invalid format.
1920
"""
2021
if requires_python is None:
2122
# The package provides no information
2223
return True
23-
try:
24-
requires_python_specifier = specifiers.SpecifierSet(requires_python)
25-
except specifiers.InvalidSpecifier as e:
26-
logger.debug(
27-
"Package %s has an invalid Requires-Python entry - %s" % (
28-
requires_python, e))
29-
raise
24+
requires_python_specifier = specifiers.SpecifierSet(requires_python)
3025

3126
# We only use major.minor.micro
3227
python_version = version.parse('.'.join(map(str, sys.version_info[:3])))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html><head><title>Links for fakepackage</title><meta name="api-version" value="2" /></head><body><h1>Links for fakepackage</h1>
2+
<a data-requires-python='' href="/fakepackage-1.0.0.tar.gz#md5=00000000000000000000000000000000" rel="internal">fakepackage-1.0.0.tar.gz</a><br/>
3+
<a data-requires-python='&lt;2.7' href="/fakepackage-2.6.0.tar.gz#md5=00000000000000000000000000000000" rel="internal">fakepackage-2.6.0.tar.gz</a><br/>
4+
<a data-requires-python='&gt;=2.7,&lt;3' href="/fakepackage-2.7.0.tar.gz#md5=00000000000000000000000000000000" rel="internal">fakepackage-2.7.0.tar.gz</a><br/>
5+
<a data-requires-python='&gt;=3.3' href="/fakepackage-3.3.0.tar.gz#md5=00000000000000000000000000000000" rel="internal">fakepackage-3.3.0.tar.gz</a><br/>
6+
<a data-requires-python='&gt;&lt;X.y.z' href="/fakepackage-9.9.9.tar.gz#md5=00000000000000000000000000000000" rel="internal">fakepackage-9.9.9.tar.gz</a><br/>
7+
</body></html>
8+

tests/scripts/test_all_pip.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ def main(args=None):
3838
print('Downloading pending list')
3939
projects = all_projects()
4040
print('Found %s projects' % len(projects))
41-
f = open(pending_fn, 'w')
42-
for name in projects:
43-
f.write(name + '\n')
44-
f.close()
41+
with open(pending_fn, 'w') as f:
42+
for name in projects:
43+
f.write(name + '\n')
4544
print('Starting testing...')
4645
while os.stat(pending_fn).st_size:
4746
_test_packages(output, pending_fn)

tests/unit/test_finder.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import sys
23

34
import pip.wheel
45
import pip.pep425tags
@@ -365,6 +366,27 @@ def test_finder_only_installs_stable_releases(data):
365366
assert link.url == "https://foo/bar-1.0.tar.gz"
366367

367368

369+
def test_finder_only_installs_data_require(data):
370+
"""
371+
"""
372+
373+
# using a local index (that has pre & dev releases)
374+
finder = PackageFinder([],
375+
[data.index_url("datarequire")],
376+
session=PipSession())
377+
links = finder.find_all_candidates("fakepackage")
378+
379+
expected = ['1.0.0', '9.9.9']
380+
if sys.version_info < (2, 7):
381+
expected.append('2.6.0')
382+
elif (2, 7) < sys.version_info < (3,):
383+
expected.append('2.7.0')
384+
elif sys.version_info > (3, 3):
385+
expected.append('3.3.0')
386+
387+
assert set([str(v.version) for v in links]) == set(expected)
388+
389+
368390
def test_finder_installs_pre_releases(data):
369391
"""
370392
Test PackageFinder finds pre-releases if asked to.

0 commit comments

Comments
 (0)