Skip to content

Commit e1ccc43

Browse files
committed
Test the file entries are correct
This is passing against 21.1.3, but not main. The installed-files.txt test case is substentially rewritten to correctly test against a legacy setuptools installation.
1 parent 5fe6ee6 commit e1ccc43

File tree

2 files changed

+69
-42
lines changed

2 files changed

+69
-42
lines changed

src/pip/_internal/operations/install/legacy.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,46 @@ def __init__(self):
2525
self.parent = sys.exc_info()
2626

2727

28+
def write_installed_files_from_setuptools_record(
29+
record_lines: List[str],
30+
root: Optional[str],
31+
req_description: str,
32+
) -> None:
33+
def prepend_root(path):
34+
# type: (str) -> str
35+
if root is None or not os.path.isabs(path):
36+
return path
37+
else:
38+
return change_root(root, path)
39+
40+
for line in record_lines:
41+
directory = os.path.dirname(line)
42+
if directory.endswith('.egg-info'):
43+
egg_info_dir = prepend_root(directory)
44+
break
45+
else:
46+
message = (
47+
"{} did not indicate that it installed an "
48+
".egg-info directory. Only setup.py projects "
49+
"generating .egg-info directories are supported."
50+
).format(req_description)
51+
raise InstallationError(message)
52+
53+
new_lines = []
54+
for line in record_lines:
55+
filename = line.strip()
56+
if os.path.isdir(filename):
57+
filename += os.path.sep
58+
new_lines.append(
59+
os.path.relpath(prepend_root(filename), egg_info_dir)
60+
)
61+
new_lines.sort()
62+
ensure_dir(egg_info_dir)
63+
inst_files_path = os.path.join(egg_info_dir, 'installed-files.txt')
64+
with open(inst_files_path, 'w') as f:
65+
f.write('\n'.join(new_lines) + '\n')
66+
67+
2868
def install(
2969
install_options, # type: List[str]
3070
global_options, # type: Sequence[str]
@@ -88,38 +128,5 @@ def install(
88128
with open(record_filename) as f:
89129
record_lines = f.read().splitlines()
90130

91-
def prepend_root(path):
92-
# type: (str) -> str
93-
if root is None or not os.path.isabs(path):
94-
return path
95-
else:
96-
return change_root(root, path)
97-
98-
for line in record_lines:
99-
directory = os.path.dirname(line)
100-
if directory.endswith('.egg-info'):
101-
egg_info_dir = prepend_root(directory)
102-
break
103-
else:
104-
message = (
105-
"{} did not indicate that it installed an "
106-
".egg-info directory. Only setup.py projects "
107-
"generating .egg-info directories are supported."
108-
).format(req_description)
109-
raise InstallationError(message)
110-
111-
new_lines = []
112-
for line in record_lines:
113-
filename = line.strip()
114-
if os.path.isdir(filename):
115-
filename += os.path.sep
116-
new_lines.append(
117-
os.path.relpath(prepend_root(filename), egg_info_dir)
118-
)
119-
new_lines.sort()
120-
ensure_dir(egg_info_dir)
121-
inst_files_path = os.path.join(egg_info_dir, 'installed-files.txt')
122-
with open(inst_files_path, 'w') as f:
123-
f.write('\n'.join(new_lines) + '\n')
124-
131+
write_installed_files_from_setuptools_record(record_lines, root, req_description)
125132
return True

tests/functional/test_show.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
22
import re
33

4-
import pytest
5-
64
from pip import __version__
75
from pip._internal.commands.show import search_packages_info
6+
from pip._internal.operations.install.legacy import (
7+
write_installed_files_from_setuptools_record,
8+
)
9+
from pip._internal.utils.unpacking import untar_file
810
from tests.lib import create_test_package_with_setup
911

1012

@@ -41,7 +43,7 @@ def test_show_with_files_not_found(script, data):
4143

4244
def test_show_with_files_from_wheel(script, data):
4345
"""
44-
Test that a wheel's files can be listed
46+
Test that a wheel's files can be listed.
4547
"""
4648
wheel_file = data.packages.joinpath('simple.dist-0.1-py2.py3-none-any.whl')
4749
script.pip('install', '--no-index', wheel_file)
@@ -50,18 +52,36 @@ def test_show_with_files_from_wheel(script, data):
5052
assert 'Name: simple.dist' in lines
5153
assert 'Cannot locate RECORD or installed-files.txt' not in lines[6], lines[6]
5254
assert re.search(r"Files:\n( .+\n)+", result.stdout)
55+
assert f" simpledist{os.sep}__init__.py" in lines[6:]
5356

5457

55-
@pytest.mark.network
56-
def test_show_with_all_files(script):
58+
def test_show_with_files_from_legacy(tmp_path, script, data):
5759
"""
58-
Test listing all files in the show command.
60+
Test listing files in the show command (legacy installed-files.txt).
5961
"""
60-
script.pip('install', 'initools==0.2')
61-
result = script.pip('show', '--files', 'initools')
62+
# Since 'pip install' now always tries to build a wheel from sdist, it
63+
# cannot properly generate a setup. The legacy code path is basically
64+
# 'setup.py install' plus installed-files.txt, which we manually generate.
65+
source_dir = tmp_path.joinpath("unpacked-sdist")
66+
setuptools_record = tmp_path.joinpath("installed-record.txt")
67+
untar_file(data.packages.joinpath("simple-1.0.tar.gz"), str(source_dir))
68+
script.run(
69+
"python", "setup.py", "install",
70+
"--single-version-externally-managed",
71+
"--record", str(setuptools_record),
72+
cwd=source_dir,
73+
)
74+
write_installed_files_from_setuptools_record(
75+
setuptools_record.read_text().splitlines(),
76+
root=None,
77+
req_description="simple==1.0",
78+
)
79+
80+
result = script.pip('show', '--files', 'simple')
6281
lines = result.stdout.splitlines()
6382
assert 'Cannot locate RECORD or installed-files.txt' not in lines[6], lines[6]
6483
assert re.search(r"Files:\n( .+\n)+", result.stdout)
84+
assert f" simple{os.sep}__init__.py" in lines[6:]
6585

6686

6787
def test_missing_argument(script):

0 commit comments

Comments
 (0)