Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --emit-options/--no-emit-options flags to pip-compile #1123

Merged
merged 3 commits into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions piptools/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
32 changes: 32 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def writer(tmpdir_cwd):
find_links=[],
emit_find_links=True,
strip_extras=False,
emit_options=True,
)
yield writer

Expand Down Expand Up @@ -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.
Expand Down