Skip to content

Commit d0db371

Browse files
committed
Local tests passing on new pip version
1 parent 4d3c8a4 commit d0db371

File tree

4 files changed

+63
-32
lines changed

4 files changed

+63
-32
lines changed

pip_check_reqs/common.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,26 @@ def find_imported_modules(options):
125125
return vis.finalise()
126126

127127

128-
def find_required_modules(options):
128+
def find_required_modules(options, requirements_filename: str):
129129
explicit = set()
130-
for requirement in parse_requirements('requirements.txt',
130+
for requirement in parse_requirements(requirements_filename,
131131
session=PipSession()):
132+
try:
133+
requirement_name = requirement.name
134+
# The type of "requirement" changed between pip versions.
135+
# We exclude the "except" from coverage so that on any pip version we
136+
# can report 100% coverage.
137+
except AttributeError: # pragma: no cover
138+
from pip._internal.req.constructors import install_req_from_line
139+
requirement_name = install_req_from_line(
140+
requirement.requirement,
141+
).name
142+
132143
if options.ignore_reqs(requirement):
133-
log.debug('ignoring requirement: %s', requirement.name)
144+
log.debug('ignoring requirement: %s', requirement_name)
134145
else:
135-
log.debug('found requirement: %s', requirement.name)
136-
explicit.add(canonicalize_name(requirement.name))
146+
log.debug('found requirement: %s', requirement_name)
147+
explicit.add(canonicalize_name(requirement_name))
137148
return explicit
138149

139150

@@ -153,9 +164,19 @@ def ignorer(ignore_cfg):
153164

154165
def f(candidate, ignore_cfg=ignore_cfg):
155166
for ignore in ignore_cfg:
156-
if fnmatch.fnmatch(candidate, ignore):
167+
try:
168+
from pip._internal.req.constructors import (
169+
install_req_from_line,
170+
)
171+
candidate_path = install_req_from_line(
172+
candidate.requirement,
173+
).name
174+
except (ImportError, AttributeError):
175+
candidate_path = candidate
176+
177+
if fnmatch.fnmatch(candidate_path, ignore):
157178
return True
158-
elif fnmatch.fnmatch(os.path.relpath(candidate), ignore):
179+
elif fnmatch.fnmatch(os.path.relpath(candidate_path), ignore):
159180
return True
160181
return False
161182

pip_check_reqs/find_extra_reqs.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
log = logging.getLogger(__name__)
1313

1414

15-
def find_extra_reqs(options):
15+
def find_extra_reqs(options, requirements_filename):
1616
# 1. find files used by imports in the code (as best we can without
1717
# executing)
1818
used_modules = common.find_imported_modules(options)
@@ -50,7 +50,10 @@ def find_extra_reqs(options):
5050
modname, info.filename)
5151

5252
# 4. compare with requirements.txt
53-
explicit = common.find_required_modules(options)
53+
explicit = common.find_required_modules(
54+
options=options,
55+
requirements_filename=requirements_filename,
56+
)
5457

5558
return [name for name in explicit if name not in used]
5659

@@ -121,12 +124,20 @@ def main():
121124

122125
log.info('using pip_check_reqs-%s from %s', __version__, __file__)
123126

124-
extras = find_extra_reqs(options)
127+
requirements_filename = 'requirements.txt'
128+
extras = find_extra_reqs(
129+
options=options,
130+
requirements_filename=requirements_filename,
131+
)
125132

126133
if extras:
127134
log.warning('Extra requirements:')
128135
for name in extras:
129-
log.warning('%s in requirements.txt' % name)
136+
message = '{name} in {requirements_filename}'.format(
137+
name=name,
138+
requirements_filename=requirements_filename,
139+
)
140+
log.warning(message)
130141

131142
if extras:
132143
sys.exit(1)

tests/test_common.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import absolute_import
22

33
import ast
4-
import collections
54
import logging
65
import os.path
6+
from pathlib import Path
77

88
import pytest
99
import pretend
@@ -157,25 +157,23 @@ def ignore_mods(module):
157157
(['spam*'], 'eggs', False),
158158
(['spam'], '/spam', True),
159159
])
160-
def test_ignorer(monkeypatch, ignore_cfg, candidate, result):
160+
def test_ignorer(monkeypatch, tmp_path: Path, ignore_cfg, candidate, result):
161161
monkeypatch.setattr(os.path, 'relpath', lambda s: s.lstrip('/'))
162162
ignorer = common.ignorer(ignore_cfg)
163163
assert ignorer(candidate) == result
164164

165165

166-
def test_find_required_modules(monkeypatch):
166+
def test_find_required_modules(monkeypatch, tmp_path: Path):
167167
class options:
168-
@staticmethod
169-
def ignore_reqs(req):
170-
if req.name == 'barfoo':
171-
return True
172-
return False
168+
pass
169+
170+
options.ignore_reqs = common.ignorer(ignore_cfg=['barfoo'])
173171

174-
FakeReq = collections.namedtuple('FakeReq', ['name'])
175-
requirements = [FakeReq('foobar'), FakeReq('barfoo')]
176-
monkeypatch.setattr(
177-
common, 'parse_requirements',
178-
pretend.call_recorder(lambda a, session=None: requirements))
172+
fake_requirements_file = tmp_path / 'requirements.txt'
173+
fake_requirements_file.write_text('foobar==1\nbarfoo==2')
179174

180-
reqs = common.find_required_modules(options)
175+
reqs = common.find_required_modules(
176+
options=options,
177+
requirements_filename=str(fake_requirements_file),
178+
)
181179
assert reqs == set(['foobar'])

tests/test_find_extra_reqs.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections
44
import logging
55
import optparse
6+
from pathlib import Path
67

78
import pytest
89
import pretend
@@ -37,7 +38,7 @@ def parse_args(self):
3738
return FakeOptParse
3839

3940

40-
def test_find_extra_reqs(monkeypatch):
41+
def test_find_extra_reqs(monkeypatch, tmp_path: Path):
4142
imported_modules = dict(spam=common.FoundModule('spam',
4243
'site-spam/spam.py',
4344
[('ham.py', 1)]),
@@ -64,19 +65,19 @@ def test_find_extra_reqs(monkeypatch):
6465
monkeypatch.setattr(find_extra_reqs, 'search_packages_info',
6566
pretend.call_recorder(lambda x: packages_info))
6667

67-
FakeReq = collections.namedtuple('FakeReq', ['name'])
68-
requirements = [FakeReq('foobar')]
69-
monkeypatch.setattr(
70-
common, 'parse_requirements',
71-
pretend.call_recorder(lambda a, session=None: requirements))
68+
fake_requirements_file = tmp_path / 'requirements.txt'
69+
fake_requirements_file.write_text('foobar==1')
7270

7371
class options:
7472
def ignore_reqs(x, y):
7573
return False
7674

7775
options = options()
7876

79-
result = find_extra_reqs.find_extra_reqs(options)
77+
result = find_extra_reqs.find_extra_reqs(
78+
options=options,
79+
requirements_filename=str(fake_requirements_file),
80+
)
8081
assert result == ['foobar']
8182

8283

0 commit comments

Comments
 (0)