Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sync with a temp file on Windows #723

Merged
merged 1 commit into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions piptools/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,13 @@ def sync(to_install, to_uninstall, verbose=False, dry_run=False, pip_flags=None,
req_lines.append(format_requirement(ireq, hashes=ireq_hashes))

# save requirement lines to a temporary file
with tempfile.NamedTemporaryFile(mode='wt') as tmp_req_file:
tmp_req_file.write('\n'.join(req_lines))
tmp_req_file.flush()
tmp_req_file = tempfile.NamedTemporaryFile(mode='wt', delete=False)
tmp_req_file.write('\n'.join(req_lines))
tmp_req_file.close()

try:
check_call([pip, 'install', '-r', tmp_req_file.name] + pip_flags + install_flags)
finally:
os.unlink(tmp_req_file.name)

return 0
22 changes: 19 additions & 3 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@


@pytest.fixture
def mocked_tmp_req_file():
def mocked_tmp_file():
with mock.patch.object(tempfile, 'NamedTemporaryFile') as m:
m.return_value.__enter__.return_value.name = 'requirements.txt'
yield m.return_value.__enter__.return_value
yield m.return_value


@pytest.fixture
def mocked_tmp_req_file(mocked_tmp_file):
with mock.patch('os.unlink'):
mocked_tmp_file.name = 'requirements.txt'
yield mocked_tmp_file


@pytest.mark.parametrize(
Expand Down Expand Up @@ -227,6 +233,16 @@ def test_sync_install_temporary_requirement_file(from_line, from_editable, mocke
check_call.assert_called_once_with(['pip', 'install', '-r', mocked_tmp_req_file.name, '-q'])


def test_temporary_requirement_file_deleted(from_line, from_editable, mocked_tmp_file):
with mock.patch('piptools.sync.check_call'):
to_install = {from_line('django==1.8')}

with mock.patch('os.unlink') as unlink:
sync(to_install, set())

unlink.assert_called_once_with(mocked_tmp_file.name)


def test_sync_requirement_file(from_line, from_editable, mocked_tmp_req_file):
with mock.patch('piptools.sync.check_call'):
to_install = {
Expand Down