Skip to content

Commit 8074db8

Browse files
committed
Add failing test.
1 parent acdbcf7 commit 8074db8

File tree

2 files changed

+73
-20
lines changed

2 files changed

+73
-20
lines changed

src/pip/_internal/wheel.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from pip._internal.cache import WheelCache # noqa: F401
5656
from pip._internal.pep425tags import Pep425Tag # noqa: F401
5757

58-
InstalledCSVRow = Tuple[str, Union[str, Text], str]
58+
InstalledCSVRow = Tuple[str, ...]
5959

6060

6161
VERSION_COMPATIBLE = (1, 0)
@@ -64,6 +64,10 @@
6464
logger = logging.getLogger(__name__)
6565

6666

67+
def normpath(src, p):
68+
return os.path.relpath(src, p).replace(os.path.sep, '/')
69+
70+
6771
def rehash(path, blocksize=1 << 20):
6872
# type: (str, int) -> Tuple[str, str]
6973
"""Return (hash, length) for path using hashlib.sha256()"""
@@ -255,6 +259,28 @@ def sorted_outrows(outrows):
255259
return sorted(outrows, key=lambda row: tuple(str(x) for x in row))
256260

257261

262+
def get_csv_rows_for_installed(
263+
old_csv_rows, # type: Iterable[List[str]]
264+
installed, # type: Dict[str, str]
265+
changed, # set
266+
generated, # type: List[str]
267+
lib_dir,
268+
):
269+
# type: (...) -> List[InstalledCSVRow]
270+
installed_rows = [] # type: List[InstalledCSVRow]
271+
for fpath, digest, length in old_csv_rows:
272+
fpath = installed.pop(fpath, fpath)
273+
if fpath in changed:
274+
digest, length = rehash(fpath)
275+
installed_rows.append((fpath, digest, str(length)))
276+
for f in generated:
277+
digest, length = rehash(f)
278+
installed_rows.append((normpath(f, lib_dir), digest, str(length)))
279+
for f in installed:
280+
installed_rows.append((installed[f], '', ''))
281+
return installed_rows
282+
283+
258284
def move_wheel_files(
259285
name, # type: str
260286
req, # type: Requirement
@@ -305,9 +331,6 @@ def move_wheel_files(
305331
compileall.compile_dir(source, force=True, quiet=True)
306332
logger.debug(stdout.getvalue())
307333

308-
def normpath(src, p):
309-
return os.path.relpath(src, p).replace(os.path.sep, '/')
310-
311334
def record_installed(srcfile, destfile, modified=False):
312335
"""Map archive RECORD paths to installation RECORD paths."""
313336
oldpath = normpath(srcfile, wheeldir)
@@ -559,28 +582,16 @@ def _get_script_text(entry):
559582
shutil.move(temp_installer, installer)
560583
generated.append(installer)
561584

562-
def get_csv_rows_for_installed(old_csv_rows):
563-
# type: (Iterable[List[str]]) -> List[InstalledCSVRow]
564-
installed_rows = [] # type: List[InstalledCSVRow]
565-
for fpath, digest, length in old_csv_rows:
566-
fpath = installed.pop(fpath, fpath)
567-
if fpath in changed:
568-
digest, length = rehash(fpath)
569-
installed_rows.append((fpath, digest, str(length)))
570-
for f in generated:
571-
digest, length = rehash(f)
572-
installed_rows.append((normpath(f, lib_dir), digest, str(length)))
573-
for f in installed:
574-
installed_rows.append((installed[f], '', ''))
575-
return installed_rows
576-
577585
# Record details of all files installed
578586
record = os.path.join(info_dir[0], 'RECORD')
579587
temp_record = os.path.join(info_dir[0], 'RECORD.pip')
580588
with open_for_csv(record, 'r') as record_in:
581589
with open_for_csv(temp_record, 'w+') as record_out:
582590
reader = csv.reader(record_in)
583-
outrows = get_csv_rows_for_installed(reader)
591+
outrows = get_csv_rows_for_installed(
592+
reader, installed=installed, changed=changed,
593+
generated=generated, lib_dir=lib_dir,
594+
)
584595
writer = csv.writer(record_out)
585596
# Sort to simplify testing.
586597
for row in sorted_outrows(outrows):

tests/unit/test_wheel.py

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Tests for wheel binary packages and .dist-info."""
2+
import csv
23
import logging
34
import os
5+
import textwrap
46

57
import pytest
68
from mock import Mock, patch
@@ -76,6 +78,46 @@ def test_sorted_outrows(outrows, expected):
7678
assert actual == expected
7779

7880

81+
def call_get_csv_rows_for_installed(tmpdir, text):
82+
path = tmpdir.join('temp.txt')
83+
path.write(text)
84+
85+
installed = {}
86+
changed = set()
87+
generated = []
88+
lib_dir = '/lib/dir'
89+
90+
with wheel.open_for_csv(path, 'r') as f:
91+
reader = csv.reader(f)
92+
outrows = wheel.get_csv_rows_for_installed(
93+
reader, installed=installed, changed=changed,
94+
generated=generated, lib_dir=lib_dir,
95+
)
96+
return outrows
97+
98+
99+
def test_get_csv_rows_for_installed(tmpdir):
100+
text = textwrap.dedent("""\
101+
a,b,c
102+
d,e,f
103+
""")
104+
outrows = call_get_csv_rows_for_installed(tmpdir, text)
105+
106+
expected = [
107+
('a', 'b', 'c'),
108+
('d', 'e', 'f'),
109+
]
110+
assert outrows == expected
111+
112+
113+
def test_get_csv_rows_for_installed__long_lines(tmpdir):
114+
text = textwrap.dedent("""\
115+
a,b,c,d
116+
e,f,g,h
117+
""")
118+
outrows = call_get_csv_rows_for_installed(tmpdir, text)
119+
120+
79121
def test_wheel_version(tmpdir, data):
80122
future_wheel = 'futurewheel-1.9-py2.py3-none-any.whl'
81123
broken_wheel = 'brokenwheel-1.0-py2.py3-none-any.whl'

0 commit comments

Comments
 (0)