diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 92b59d8f0..5fb525e3d 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -221,6 +221,12 @@ def _get_default_option(option_name: str) -> Any: default=True, help="Add index URL to generated file", ) +@click.option( + "--emit-options/--no-emit-options", + is_flag=True, + default=True, + help="Add options to generated file", +) def cli( ctx: click.Context, verbose: int, @@ -252,6 +258,7 @@ def cli( cache_dir: str, pip_args_str: Optional[str], emit_index_url: bool, + emit_options: bool, ) -> None: """Compiles requirements.txt from requirements.in specs.""" log.verbosity = verbose - quiet @@ -473,6 +480,7 @@ def cli( allow_unsafe=allow_unsafe, find_links=repository.finder.find_links, emit_find_links=emit_find_links, + emit_options=emit_options, ) writer.write( results=results, diff --git a/piptools/writer.py b/piptools/writer.py index b99a96ca8..d26b3deff 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -70,6 +70,7 @@ def __init__( allow_unsafe: bool, find_links: List[str], emit_find_links: bool, + emit_options: bool, ) -> None: self.dst_file = dst_file self.click_ctx = click_ctx @@ -87,6 +88,7 @@ def __init__( self.allow_unsafe = allow_unsafe self.find_links = find_links self.emit_find_links = emit_find_links + self.emit_options = emit_options def _sort_key(self, ireq: InstallRequirement) -> Tuple[bool, str]: return (not ireq.editable, key_from_ireq(ireq)) @@ -131,6 +133,8 @@ def write_find_links(self) -> Iterator[str]: yield f"--find-links {find_link}" def write_flags(self) -> Iterator[str]: + if not self.emit_options: + return emitted = False for line in chain( self.write_index_options(), diff --git a/tests/test_utils.py b/tests/test_utils.py index 6a07d585b..73873dabf 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -238,8 +238,10 @@ def test_is_url_requirement_filename(caplog, from_line, line): (["--no-emit-trusted-host"], "pip-compile --no-emit-trusted-host"), (["--no-annotate"], "pip-compile --no-annotate"), (["--no-allow-unsafe"], "pip-compile"), + (["--no-emit-options"], "pip-compile --no-emit-options"), # Check that default values will be removed from the command (["--emit-trusted-host"], "pip-compile"), + (["--emit-options"], "pip-compile"), (["--annotate"], "pip-compile"), (["--emit-index-url"], "pip-compile"), (["--max-rounds=10"], "pip-compile"), diff --git a/tests/test_writer.py b/tests/test_writer.py index 2ff54a30a..b44677e99 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -45,6 +45,7 @@ def writer(tmpdir_cwd): find_links=[], emit_find_links=True, strip_extras=False, + emit_options=True, ) yield writer @@ -227,6 +228,37 @@ def test_write_header_no_emit_header(writer): next(writer.write_header()) +@pytest.mark.parametrize( + ("emit_options", "expected_flags"), + ( + pytest.param( + True, + ( + "--index-url https://index-server", + "--find-links links", + "--trusted-host index-server", + "--no-binary flask", + "--only-binary django", + "", + ), + id="on", + ), + pytest.param(False, (), id="off"), + ), +) +def test_write_flags_emit_options(writer, emit_options, expected_flags): + """ + There should be options if emit_options is True + """ + writer.emit_options = emit_options + writer.index_urls = ["https://index-server"] + writer.find_links = ["links"] + writer.trusted_hosts = ["index-server"] + writer.format_control = FormatControl(no_binary=["flask"], only_binary=["django"]) + + assert tuple(writer.write_flags()) == expected_flags + + def test_write_format_controls(writer): """ Tests --no-binary/--only-binary options.