Skip to content

Commit f6924d2

Browse files
author
Patrik Kopkan
committed
moved content of if block to method. Added unit_test for method. Fixed news to be in imperative tone.
1 parent 8e98a66 commit f6924d2

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

news/6340.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Added a new option ``--save-wheel-names`` to command ``wheel``.
1+
Add a new option ``--save-wheel-names`` to command ``wheel``.
22
The option takes a path and creates a file in which
33
the filenames of built or downloaded wheels will be stored,
44
separated by newlines

src/pip/_internal/commands/wheel.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ def __init__(self, *args, **kw):
114114
self.parser.insert_option_group(0, index_opts)
115115
self.parser.insert_option_group(0, cmd_opts)
116116

117+
def save_wheelnames(self, requirement_set, wb):
118+
entries_to_save = wb.wheel_filenames[::]
119+
for req in requirement_set.requirements.values():
120+
if req.link.filename.endswith('whl'):
121+
entries_to_save.append(req.link.filename)
122+
try:
123+
with open(wb.path_to_wheelnames, 'w') as file:
124+
file.write(
125+
os.linesep.join(
126+
entries_to_save
127+
) + os.linesep
128+
)
129+
except EnvironmentError as e:
130+
logger.error('Cannot write to the given path: %s%s PermissionError: %s' %
131+
(wb.path_to_wheelnames, os.linesep, str(e)))
132+
117133
def run(self, options, args):
118134
cmdoptions.check_install_build_global(options)
119135

@@ -188,21 +204,7 @@ def run(self, options, args):
188204
"Failed to build one or more wheels"
189205
)
190206
if wb.path_to_wheelnames is not None:
191-
entries_to_save = wb.wheel_filenames[::]
192-
for req in requirement_set.requirements.values():
193-
if req.link.filename.endswith('whl'):
194-
entries_to_save.append(req.link.filename)
195-
try:
196-
with open(wb.path_to_wheelnames, 'w') as file:
197-
file.write(
198-
'\n'.join(
199-
entries_to_save
200-
) + '\n'
201-
)
202-
except EnvironmentError as e:
203-
logger.error('Cannot write to the given path: %s',
204-
wb.path_to_wheelnames)
205-
logger.error('PermissionError: %s', str(e))
207+
self.save_wheelnames(requirement_set, wb)
206208

207209
except PreviousBuildDirError:
208210
options.no_clean = True

tests/unit/test_wheel.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import textwrap
66

77
import pytest
8-
from mock import Mock, patch
8+
from mock import Mock, patch, mock_open
99
from pip._vendor.packaging.requirements import Requirement
1010

1111
from pip._internal import pep425tags, wheel
@@ -17,6 +17,7 @@
1717
from pip._internal.utils.misc import unpack_file
1818
from tests.lib import DATA_DIR, assert_paths_equal
1919

20+
from pip._internal.commands.wheel import WheelCommand
2021

2122
@pytest.mark.parametrize(
2223
"s, expected",
@@ -806,3 +807,36 @@ def test_missing_PATH_env_treated_as_empty_PATH_env(self):
806807
retval_empty = wheel.message_about_scripts_not_on_PATH(scripts)
807808

808809
assert retval_missing == retval_empty
810+
811+
class TestWheelCommand(object):
812+
@patch('pip._internal.wheel.WheelBuilder')
813+
@patch('pip._internal.commands.wheel.RequirementSet')
814+
def test_save_wheelnames(self, mocked_rs, mocked_wb):
815+
816+
# simulating needed behaviour
817+
class Link:
818+
def __init__(self, filename):
819+
self.filename = filename
820+
821+
mocked_wb = mocked_wb()
822+
mocked_wb.wheel_filenames = ['Flask-1.1.dev0-py2.py3-none-any.whl']
823+
824+
filenames = [
825+
'flask',
826+
'Werkzeug-0.15.4-py2.py3-none-any.whl',
827+
'Jinja2-2.10.1-py2.py3-none-any.whl',
828+
'itsdangerous-1.1.0-py2.py3-none-any.whl',
829+
'Click-7.0-py2.py3-none-any.whl'
830+
]
831+
List = []
832+
expected = mocked_wb.wheel_filenames + filenames[1:]
833+
for filename in filenames:
834+
install_reg = InstallRequirement(None, None)
835+
install_reg.link = Link(filename)
836+
List.append(install_reg)
837+
mocked_rs.requirements.values = lambda: List
838+
m = (mock_open())()
839+
with patch('pip._internal.commands.wheel.open', m, create=True):
840+
WheelCommand().save_wheelnames(mocked_rs, mocked_wb)
841+
handle = m()
842+
handle.__enter__().write.assert_called_once_with(os.linesep.join(expected) + os.linesep)

0 commit comments

Comments
 (0)