Skip to content

Commit c921465

Browse files
committed
fix program to generate exit code useful for testing
1 parent 18592ea commit c921465

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Release History
44
1.1.2
55
- corrected version of vendored search_packages_info() from pip
66
- handle relative imports
7+
- fix program to generate exit code useful for testing
78

89
1.1.1
910
- fixed handling of import from __future__

pip_missing_reqs/find_missing_reqs.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,8 @@ def find_missing_reqs(options):
203203
session=PipSession()):
204204
explicit.add(normalize_name(requirement.name))
205205

206-
for name in used:
207-
if name not in explicit:
208-
yield name, used[name]
206+
return [(name, used[name]) for name in used
207+
if name not in explicit]
209208

210209

211210
def ignorer(ignore_cfg):
@@ -238,7 +237,7 @@ def main():
238237

239238
if not args:
240239
parser.error("no source files or directories specified")
241-
sys.exit(-1)
240+
sys.exit(2)
242241

243242
options.ignore_files = ignorer(options.ignore_files)
244243
options.ignore_mods = ignorer(options.ignore_mods)
@@ -248,11 +247,17 @@ def main():
248247
logging.basicConfig(format='%(message)s')
249248
log.setLevel(logging.INFO if options.verbose else logging.WARN)
250249

251-
for name, uses in find_missing_reqs(options):
250+
missing = find_missing_reqs(options)
251+
for name, uses in missing:
252252
for use in uses:
253253
for filename, lineno in use.locations:
254+
print(os.path.relpath(filename), lineno, name, use.modname)
254255
log.warning('%s:%s dist=%s module=%s',
255256
os.path.relpath(filename), lineno, name, use.modname)
256257

258+
if missing:
259+
sys.exit(1)
260+
261+
257262
if __name__ == '__main__': # pragma: no cover
258263
main()

pip_missing_reqs/test_find_missing_reqs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def test_find_missing_reqs(monkeypatch):
182182
assert result == [('shrub', [imported_modules['shrub']])]
183183

184184

185-
def test_main(monkeypatch, caplog):
185+
def test_main_failure(monkeypatch, caplog):
186186
class options:
187187
paths = ['dummy']
188188
verbose = True
@@ -209,7 +209,9 @@ def parse_args(self):
209209
[('location.py', 1)])])
210210
])
211211

212-
find_missing_reqs.main()
212+
with pytest.raises(SystemExit) as excinfo:
213+
find_missing_reqs.main()
214+
assert excinfo.value == 1
213215

214216
assert caplog.records()[0].message == \
215217
'location.py:1 dist=missing module=missing'
@@ -232,8 +234,9 @@ def parse_args(self):
232234
monkeypatch.setattr(find_missing_reqs, 'ignorer',
233235
pretend.call_recorder(lambda a: None))
234236

235-
with pytest.raises(SystemExit):
237+
with pytest.raises(SystemExit) as excinfo:
236238
find_missing_reqs.main()
239+
assert excinfo.value == 2
237240

238241
assert FakeOptParse.error.calls
239242
assert not find_missing_reqs.ignorer.calls

0 commit comments

Comments
 (0)