Skip to content

Commit

Permalink
Canonicalize FrozenRequirement name for correct comparison
Browse files Browse the repository at this point in the history
- Fixes pypa#5716
  • Loading branch information
abs51295 committed Sep 2, 2019
1 parent d764181 commit ea2a889
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions news/5716.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix case sensitive comparison of pip freeze when used with -r option.
38 changes: 22 additions & 16 deletions src/pip/_internal/operations/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def freeze(
continue
if exclude_editable and req.editable:
continue
installations[req.name] = req
installations[req.canonical_name] = req

if requirement:
# the options that don't get turned into an InstallRequirement
Expand Down Expand Up @@ -138,22 +138,27 @@ def freeze(
" (add #egg=PackageName to the URL to avoid"
" this warning)"
)
elif line_req.name not in installations:
# either it's not installed, or it is installed
# but has been processed already
if not req_files[line_req.name]:
logger.warning(
"Requirement file [%s] contains %s, but "
"package %r is not installed",
req_file_path,
COMMENT_RE.sub('', line).strip(), line_req.name
)
else:
line_req_canonical_name = canonicalize_name(
line_req.name)
if line_req_canonical_name not in installations:
# either it's not installed, or it is installed
# but has been processed already
if not req_files[line_req.name]:
logger.warning(
"Requirement file [%s] contains %s, but "
"package %r is not installed",
req_file_path,
COMMENT_RE.sub('', line).strip(),
line_req.name
)
else:
req_files[line_req.name].append(req_file_path)
else:
yield str(installations[
line_req_canonical_name]).rstrip()
del installations[line_req_canonical_name]
req_files[line_req.name].append(req_file_path)
else:
yield str(installations[line_req.name]).rstrip()
del installations[line_req.name]
req_files[line_req.name].append(req_file_path)

# Warn about requirements that were included multiple times (in a
# single requirements file or in different requirements files).
Expand All @@ -168,7 +173,7 @@ def freeze(
)
for installation in sorted(
installations.values(), key=lambda x: x.name.lower()):
if canonicalize_name(installation.name) not in skip:
if installation.canonical_name not in skip:
yield str(installation).rstrip()


Expand Down Expand Up @@ -238,6 +243,7 @@ class FrozenRequirement(object):
def __init__(self, name, req, editable, comments=()):
# type: (str, Union[str, Requirement], bool, Iterable[str]) -> None
self.name = name
self.canonical_name = canonicalize_name(name)
self.req = req
self.editable = editable
self.comments = comments
Expand Down
20 changes: 17 additions & 3 deletions tests/functional/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,20 @@ def test_freeze_with_requirement_option(script):
"""

script.scratch_path.joinpath("hint.txt").write_text(textwrap.dedent("""\
script.scratch_path.joinpath("hint1.txt").write_text(textwrap.dedent("""\
INITools==0.1
NoExist==4.2 # A comment that ensures end of line comments work.
simple==3.0; python_version > '1.0'
""") + _freeze_req_opts)
script.scratch_path.joinpath("hint2.txt").write_text(textwrap.dedent("""\
iniTools==0.1
Noexist==4.2 # A comment that ensures end of line comments work.
Simple==3.0; python_version > '1.0'
""") + _freeze_req_opts)
result = script.pip_install_local('initools==0.2')
result = script.pip_install_local('simple')
result = script.pip(
'freeze', '--requirement', 'hint.txt',
'freeze', '--requirement', 'hint1.txt',
expect_stderr=True,
)
expected = textwrap.dedent("""\
Expand All @@ -521,9 +526,18 @@ def test_freeze_with_requirement_option(script):
expected += "## The following requirements were added by pip freeze:..."
_check_output(result.stdout, expected)
assert (
"Requirement file [hint.txt] contains NoExist==4.2, but package "
"Requirement file [hint1.txt] contains NoExist==4.2, but package "
"'NoExist' is not installed"
) in result.stderr
result = script.pip(
'freeze', '--requirement', 'hint2.txt',
expect_stderr=True,
)
_check_output(result.stdout, expected)
assert (
"Requirement file [hint2.txt] contains Noexist==4.2, but package "
"'Noexist' is not installed"
) in result.stderr


def test_freeze_with_requirement_option_multiple(script):
Expand Down

0 comments on commit ea2a889

Please sign in to comment.