diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 9624684e8..177c732b7 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -17,6 +17,7 @@ UNSAFE_PACKAGES, create_install_command, dedup, + get_trusted_hosts, is_pinned_requirement, key_from_ireq, key_from_req, @@ -409,7 +410,7 @@ def cli( generate_hashes=generate_hashes, default_index_url=repository.DEFAULT_INDEX_URL, index_urls=repository.finder.index_urls, - trusted_hosts=repository.options.trusted_hosts, + trusted_hosts=get_trusted_hosts(repository.finder), format_control=repository.finder.format_control, allow_unsafe=allow_unsafe, find_links=repository.finder.find_links, diff --git a/piptools/utils.py b/piptools/utils.py index e17864d49..8727f1e2a 100644 --- a/piptools/utils.py +++ b/piptools/utils.py @@ -380,3 +380,13 @@ def create_install_command(): from pip._internal.commands import create_command return create_command("install") + + +def get_trusted_hosts(finder): + """ + Returns an iterable of trusted hosts from a given finder. + """ + if PIP_VERSION < (19, 2): + return (host for _, host, _ in finder.secure_origins) + + return finder.trusted_hosts diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 36661e055..60624b922 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -891,3 +891,30 @@ def test_upgrade_package_doesnt_remove_annotation(pip_conf, runner): "small-fake-a==0.2 # via small-fake-with-deps-a" in req_txt.read().splitlines() ) + + +@pytest.mark.parametrize( + "options", + [ + # TODO add --no-index support in OutputWriter + # "--no-index", + "--index-url https://example.com", + "--extra-index-url https://example.com", + "--find-links ./libs1", + "--trusted-host example.com", + "--no-binary :all:", + "--only-binary :all:", + ], +) +def test_options_in_requirements_file(runner, options): + """ + Test the options from requirements.in is copied to requirements.txt. + """ + with open("requirements.in", "w") as reqs_in: + reqs_in.write(options) + + out = runner.invoke(cli) + assert out.exit_code == 0, out + + with open("requirements.txt") as reqs_txt: + assert options in reqs_txt.read().splitlines() diff --git a/tests/test_utils.py b/tests/test_utils.py index 18a65cf5b..b0418c9f7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,12 +1,15 @@ # coding: utf-8 from __future__ import unicode_literals +import itertools import os +import pytest import six from pytest import mark, raises from six.moves import shlex_quote +from piptools.repositories import PyPIRepository from piptools.scripts.compile import cli as compile_cli from piptools.utils import ( as_tuple, @@ -19,6 +22,7 @@ fs_str, get_compile_command, get_hashes_from_ireq, + get_trusted_hosts, is_pinned_requirement, is_url_requirement, name_from_req, @@ -333,3 +337,21 @@ def test_create_install_command(): """ install_command = create_install_command() assert install_command.name == "install" + + +@mark.parametrize( + "hosts", + [ + pytest.param((), id="no hosts"), + pytest.param(("example.com",), id="single host"), + pytest.param(("example.com:8080",), id="host with port"), + pytest.param(("example1.com", "example2.com:8080"), id="multiple hosts"), + ], +) +def test_get_trusted_hosts(hosts): + """ + Test get_trusted_hosts(finder) returns a list of hosts. + """ + pip_args = list(itertools.chain(*zip(["--trusted-host"] * len(hosts), hosts))) + repository = PyPIRepository(pip_args) + assert tuple(get_trusted_hosts(repository.finder)) == hosts