Skip to content

Commit

Permalink
Added offending line from requirements file (#4227) (#4635)
Browse files Browse the repository at this point in the history
  • Loading branch information
fkokosinski authored and dstufft committed Aug 31, 2017
1 parent 283cf2c commit 6ebec4a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/4227.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Report the line which caused the hash error when using requirement files.
6 changes: 4 additions & 2 deletions pip/req/req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def process_line(line, filename, line_number, finder=None, comes_from=None,
:param constraint: If True, parsing a constraints file.
:param options: OptionParser options that we may update
"""
parser = build_parser()
parser = build_parser(line)
defaults = parser.get_default_values()
defaults.index_url = None
if finder:
Expand Down Expand Up @@ -228,7 +228,7 @@ def break_args_options(line):
return ' '.join(args), ' '.join(options)


def build_parser():
def build_parser(line):
"""
Return a parser for parsing requirement lines
"""
Expand All @@ -242,6 +242,8 @@ def build_parser():
# By default optparse sys.exits on parsing errors. We want to wrap
# that in our own exception.
def parser_exit(self, msg):
# add offending line
msg = 'Invalid requirement: %s\n%s' % (line, msg)
raise RequirementsFileParseError(msg)
parser.exit = parser_exit

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ def test_parser_error(self):
with pytest.raises(RequirementsFileParseError):
list(process_line("--bogus", "file", 1))

def test_parser_offending_line(self):
line = 'pkg==1.0.0 --hash=somehash'
with pytest.raises(RequirementsFileParseError) as err:
list(process_line(line, 'file', 1))
assert line in str(err.value)

def test_parser_non_offending_line(self):
try:
list(process_line('pkg==1.0.0 --hash=sha256:somehash', 'file', 1))
except RequirementsFileParseError:
pytest.fail('Reported offending line where it should not.')

def test_only_one_req_per_line(self):
# pkg_resources raises the ValueError
with pytest.raises(InstallationError):
Expand Down

0 comments on commit 6ebec4a

Please sign in to comment.