From 14b6fd5fd78830c970cc02b0747aa222ebb11c9e Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Tue, 28 Apr 2020 03:23:51 +0700 Subject: [PATCH 1/2] Add --emit-options/--no-emit-options flag --- piptools/scripts/compile.py | 8 ++++++++ piptools/writer.py | 5 +++++ tests/test_utils.py | 2 ++ tests/test_writer.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 7bd6e0822..85e8a2595 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -179,6 +179,12 @@ type=click.Path(file_okay=False, writable=True), ) @click.option("--pip-args", help="Arguments to pass directly to the pip command.") +@click.option( + "--emit-options/--no-emit-options", + is_flag=True, + default=True, + help="Add options to generated file", +) def cli( ctx, verbose, @@ -207,6 +213,7 @@ def cli( emit_find_links, cache_dir, pip_args, + emit_options, ): """Compiles requirements.txt from requirements.in specs.""" log.verbosity = verbose - quiet @@ -421,6 +428,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 694fd3cc1..782eba9ee 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -67,6 +67,7 @@ def __init__( allow_unsafe, find_links, emit_find_links, + emit_options, ): self.src_files = src_files self.dst_file = dst_file @@ -84,6 +85,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): return (not ireq.editable, str(ireq.req).lower()) @@ -125,6 +127,9 @@ def write_find_links(self): yield "--find-links {}".format(find_link) def write_flags(self): + 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 5a92dae87..6e2b6ade5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -249,8 +249,10 @@ def test_force_text(value, expected_text): (["--no-index"], "pip-compile --no-index"), (["--no-emit-trusted-host"], "pip-compile --no-emit-trusted-host"), (["--no-annotate"], "pip-compile --no-annotate"), + (["--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"), (["--index"], "pip-compile"), (["--max-rounds=10"], "pip-compile"), diff --git a/tests/test_writer.py b/tests/test_writer.py index f1fc3e08c..46ab99e32 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -42,6 +42,7 @@ def writer(tmpdir_cwd): allow_unsafe=False, find_links=[], emit_find_links=True, + emit_options=True, ) yield writer @@ -224,6 +225,40 @@ def test_write_header_no_emit_header(writer): next(writer.write_header()) +def test_write_flags_emit_options(writer): + """ + There should be options if emit_options is True + """ + writer.emit_options = True + 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()) == ( + "--index-url https://index-server", + "--find-links links", + "--trusted-host index-server", + "--no-binary flask", + "--only-binary django", + "", + ) + + +def test_write_flags_no_emit_options(writer): + """ + There should not be options if emit_options is False + """ + writer.emit_options = False + 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"]) + + with raises(StopIteration): + next(writer.write_flags()) + + def test_write_format_controls(writer): """ Tests --no-binary/--only-binary options. From 0d1bdf6504173b269308309713bcd41c326ed23a Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Sat, 19 Jun 2021 22:15:54 +0700 Subject: [PATCH 2/2] Use parametrization --- piptools/writer.py | 2 +- tests/test_writer.py | 45 +++++++++++++++++++++----------------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/piptools/writer.py b/piptools/writer.py index f3fd37cc0..d26b3deff 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -70,7 +70,7 @@ def __init__( allow_unsafe: bool, find_links: List[str], emit_find_links: bool, - emit_options, + emit_options: bool, ) -> None: self.dst_file = dst_file self.click_ctx = click_ctx diff --git a/tests/test_writer.py b/tests/test_writer.py index 18dd6e66e..b44677e99 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -228,38 +228,35 @@ def test_write_header_no_emit_header(writer): next(writer.write_header()) -def test_write_flags_emit_options(writer): +@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 = True - 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()) == ( - "--index-url https://index-server", - "--find-links links", - "--trusted-host index-server", - "--no-binary flask", - "--only-binary django", - "", - ) - - -def test_write_flags_no_emit_options(writer): - """ - There should not be options if emit_options is False - """ - writer.emit_options = False + 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"]) - with raises(StopIteration): - next(writer.write_flags()) + assert tuple(writer.write_flags()) == expected_flags def test_write_format_controls(writer):