Repository: jazzband/pip-tools (python)

Issue:
Option --trusted-host is not copied from requirements.in to requirements.txt
<!-- Describe the issue briefly here. -->

#### Environment Versions

1. OS Type: Mac OS
1. Python version: `Python 3.7.3`
1. pip version:  `pip 19.3.1 from /.venv/lib/python3.7/site-packages/pip (python 3.7)`
1. pip-tools version: `pip-compile, version 4.2.0`

#### Steps to replicate

1. `echo "--trusted-host exmaple.com" | pip-compile - -qo-`

#### Expected result

```bash
#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile --output-file=- -
#
--trusted-host exmaple.com
```

#### Actual result

```bash
#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile --output-file=- -
#
```

#### Additional context

Caught by @altendky, see https://github.com/jazzband/pip-tools/pull/824#issuecomment-544758205.
Related issues/PRs: #338, #371

Pull request:
Fix --trusted-host is not copied from *.in to *.txt
<!--- Describe the changes here. --->

Fixes #963.

**Changelog-friendly one-liner**: Fix `pip-compile` doesn't copy `--trusted-host` from `requirements.in` to `requirements.txt`.

##### Contributor checklist

- [X] Provided the tests for the changes.
- [X] Gave a clear one-line description in the PR (that the maintainers can add to CHANGELOG.md on release).
- [x] Assign the PR to an existing or new milestone for the target version (following [Semantic Versioning](https://blog.versioneye.com/2014/01/16/semantic-versioning/)).

Patch:
diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py
index 9624684..177c732 100755
--- a/piptools/scripts/compile.py
+++ b/piptools/scripts/compile.py
@@ -17,6 +17,7 @@ from ..utils import (
     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 e17864d..8727f1e 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


Test patch:
diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py
index 36661e0..60624b9 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 18a65cf..b0418c9 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 @@ from piptools.utils import (
     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