Skip to content

Commit 295ba86

Browse files
authored
Add --emit-options/--no-emit-options flag (#1123)
* Add --emit-options/--no-emit-options flag * Use parametrization
1 parent cb53648 commit 295ba86

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

piptools/scripts/compile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ def _get_default_option(option_name: str) -> Any:
221221
default=True,
222222
help="Add index URL to generated file",
223223
)
224+
@click.option(
225+
"--emit-options/--no-emit-options",
226+
is_flag=True,
227+
default=True,
228+
help="Add options to generated file",
229+
)
224230
def cli(
225231
ctx: click.Context,
226232
verbose: int,
@@ -252,6 +258,7 @@ def cli(
252258
cache_dir: str,
253259
pip_args_str: Optional[str],
254260
emit_index_url: bool,
261+
emit_options: bool,
255262
) -> None:
256263
"""Compiles requirements.txt from requirements.in specs."""
257264
log.verbosity = verbose - quiet
@@ -473,6 +480,7 @@ def cli(
473480
allow_unsafe=allow_unsafe,
474481
find_links=repository.finder.find_links,
475482
emit_find_links=emit_find_links,
483+
emit_options=emit_options,
476484
)
477485
writer.write(
478486
results=results,

piptools/writer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def __init__(
7070
allow_unsafe: bool,
7171
find_links: List[str],
7272
emit_find_links: bool,
73+
emit_options: bool,
7374
) -> None:
7475
self.dst_file = dst_file
7576
self.click_ctx = click_ctx
@@ -87,6 +88,7 @@ def __init__(
8788
self.allow_unsafe = allow_unsafe
8889
self.find_links = find_links
8990
self.emit_find_links = emit_find_links
91+
self.emit_options = emit_options
9092

9193
def _sort_key(self, ireq: InstallRequirement) -> Tuple[bool, str]:
9294
return (not ireq.editable, key_from_ireq(ireq))
@@ -131,6 +133,8 @@ def write_find_links(self) -> Iterator[str]:
131133
yield f"--find-links {find_link}"
132134

133135
def write_flags(self) -> Iterator[str]:
136+
if not self.emit_options:
137+
return
134138
emitted = False
135139
for line in chain(
136140
self.write_index_options(),

tests/test_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,10 @@ def test_is_url_requirement_filename(caplog, from_line, line):
238238
(["--no-emit-trusted-host"], "pip-compile --no-emit-trusted-host"),
239239
(["--no-annotate"], "pip-compile --no-annotate"),
240240
(["--no-allow-unsafe"], "pip-compile"),
241+
(["--no-emit-options"], "pip-compile --no-emit-options"),
241242
# Check that default values will be removed from the command
242243
(["--emit-trusted-host"], "pip-compile"),
244+
(["--emit-options"], "pip-compile"),
243245
(["--annotate"], "pip-compile"),
244246
(["--emit-index-url"], "pip-compile"),
245247
(["--max-rounds=10"], "pip-compile"),

tests/test_writer.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def writer(tmpdir_cwd):
4545
find_links=[],
4646
emit_find_links=True,
4747
strip_extras=False,
48+
emit_options=True,
4849
)
4950
yield writer
5051

@@ -227,6 +228,37 @@ def test_write_header_no_emit_header(writer):
227228
next(writer.write_header())
228229

229230

231+
@pytest.mark.parametrize(
232+
("emit_options", "expected_flags"),
233+
(
234+
pytest.param(
235+
True,
236+
(
237+
"--index-url https://index-server",
238+
"--find-links links",
239+
"--trusted-host index-server",
240+
"--no-binary flask",
241+
"--only-binary django",
242+
"",
243+
),
244+
id="on",
245+
),
246+
pytest.param(False, (), id="off"),
247+
),
248+
)
249+
def test_write_flags_emit_options(writer, emit_options, expected_flags):
250+
"""
251+
There should be options if emit_options is True
252+
"""
253+
writer.emit_options = emit_options
254+
writer.index_urls = ["https://index-server"]
255+
writer.find_links = ["links"]
256+
writer.trusted_hosts = ["index-server"]
257+
writer.format_control = FormatControl(no_binary=["flask"], only_binary=["django"])
258+
259+
assert tuple(writer.write_flags()) == expected_flags
260+
261+
230262
def test_write_format_controls(writer):
231263
"""
232264
Tests --no-binary/--only-binary options.

0 commit comments

Comments
 (0)