Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Jul 28, 2016
1 parent 1d10fca commit d4e22ea
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions pip/baseparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def expand_default(self, option):


class CustomOptionParser(optparse.OptionParser):

def insert_option_group(self, idx, *args, **kwargs):
"""Insert an OptionGroup at a given position."""
group = self.add_option_group(*args, **kwargs)
Expand Down
15 changes: 8 additions & 7 deletions pip/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,8 @@ def _link_package_versions(self, link, search):
try:
support_this_python = check_requires_python(link.requires_python)
except specifiers.InvalidSpecifier:
logger.debug("Package %s has an invalid Requires-Python entry: %s",
link.filename, link.requires_python)
support_this_python = True

if not support_this_python:
Expand Down Expand Up @@ -841,7 +843,8 @@ def links(self):
url = self.clean_link(
urllib_parse.urljoin(self.base_url, href)
)
pyrequire = unescape(anchor.get('data-requires-python'))
pyrequire = anchor.get('data-requires-python')
pyrequire = unescape(pyrequire) if pyrequire else None
yield Link(url, self, requires_python=pyrequire)

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

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

self.url = url
self.comes_from = comes_from
if not requires_python:
self.requires_python = None
else:
self.requires_python = requires_python
self.requires_python = requires_python if requires_python else None

def __str__(self):
if self.requires_python:
Expand Down
19 changes: 7 additions & 12 deletions pip/utils/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,18 @@

def check_requires_python(requires_python):
"""
Check if the python version in used match the `requires_python` specifier.
Check if the python version in use match the `requires_python` specifier.
Return `True` if the version of python in use matches the requirement.
Return `False` if the version of python in use does not matches the
requirement. Raises an InvalidSpecifier if `requires_python` have an
invalid format.
Returns `True` if the version of python in use matches the requirement.
Returns `False` if the version of python in use does not matches the
requirement.
Raises an InvalidSpecifier if `requires_python` have an invalid format.
"""
if requires_python is None:
# The package provides no information
return True
try:
requires_python_specifier = specifiers.SpecifierSet(requires_python)
except specifiers.InvalidSpecifier as e:
logger.debug(
"Package %s has an invalid Requires-Python entry - %s" % (
requires_python, e))
raise
requires_python_specifier = specifiers.SpecifierSet(requires_python)

# We only use major.minor.micro
python_version = version.parse('.'.join(map(str, sys.version_info[:3])))
Expand Down
8 changes: 8 additions & 0 deletions tests/data/indexes/datarequire/fakepackage/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html><head><title>Links for fakepackage</title><meta name="api-version" value="2" /></head><body><h1>Links for fakepackage</h1>
<a data-requires-python='' href="/fakepackage-1.0.0.tar.gz#md5=00000000000000000000000000000000" rel="internal">fakepackage-1.0.0.tar.gz</a><br/>
<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/>
<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/>
<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/>
<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/>
</body></html>

7 changes: 3 additions & 4 deletions tests/scripts/test_all_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ def main(args=None):
print('Downloading pending list')
projects = all_projects()
print('Found %s projects' % len(projects))
f = open(pending_fn, 'w')
for name in projects:
f.write(name + '\n')
f.close()
with open(pending_fn, 'w') as f:
for name in projects:
f.write(name + '\n')
print('Starting testing...')
while os.stat(pending_fn).st_size:
_test_packages(output, pending_fn)
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_finder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import sys

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


def test_finder_only_installs_data_require(data):
"""
"""

# using a local index (that has pre & dev releases)
finder = PackageFinder([],
[data.index_url("datarequire")],
session=PipSession())
links = finder.find_all_candidates("fakepackage")

expected = ['1.0.0', '9.9.9']
if sys.version_info < (2, 7):
expected.append('2.6.0')
elif (2, 7) < sys.version_info < (3,):
expected.append('2.7.0')
elif sys.version_info > (3, 3):
expected.append('3.3.0')

assert set([str(v.version) for v in links]) == set(expected)


def test_finder_installs_pre_releases(data):
"""
Test PackageFinder finds pre-releases if asked to.
Expand Down

0 comments on commit d4e22ea

Please sign in to comment.