Skip to content

Commit

Permalink
Replace extra wildcard with all-extras flag
Browse files Browse the repository at this point in the history
... to not give users the misleading impression that pattern matching,
with regex,  fnmatch or some other system, works.
  • Loading branch information
apljungquist committed Oct 1, 2022
1 parent 76ac553 commit b7dde7d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
20 changes: 12 additions & 8 deletions piptools/scripts/compile.py
Expand Up @@ -79,6 +79,12 @@ def _get_default_option(option_name: str) -> Any:
multiple=True,
help="Name of an extras_require group to install; may be used more than once",
)
@click.option(
"--all-extras",
is_flag=True,
default=False,
help="Install all extras_require groups",
)
@click.option(
"-f",
"--find-links",
Expand Down Expand Up @@ -259,6 +265,7 @@ def cli(
pre: bool,
rebuild: bool,
extras: Tuple[str, ...],
all_extras: bool,
find_links: Tuple[str, ...],
index_url: str,
extra_index_url: Tuple[str, ...],
Expand Down Expand Up @@ -401,8 +408,6 @@ def cli(
# Parsing/collecting initial requirements
###

extras = tuple(itertools.chain.from_iterable(ex.split(",") for ex in extras))

constraints: List[InstallRequirement] = []
setup_file_found = False
for src_file in src_files:
Expand Down Expand Up @@ -444,12 +449,9 @@ def cli(
for req in metadata.get_all("Requires-Dist") or []
]
)
# Since "*" is not a valid extra there is no risk confusing an actual extra
# for the wildcard.
# https://peps.python.org/pep-0508/#grammar
if "*" in extras:
if len(extras) != 1:
msg = "--extra=* only makes sense if it is the only --extra"
if all_extras:
if extras:
msg = "--extra has no effect when used with --all-extras"
raise click.BadParameter(msg)
extras = tuple(metadata.get_all("Provides-Extra"))
else:
Expand All @@ -462,6 +464,8 @@ def cli(
)
)

extras = tuple(itertools.chain.from_iterable(ex.split(",") for ex in extras))

if extras and not setup_file_found:
msg = "--extra has effect only with setup.py and PEP-517 input formats"
raise click.BadParameter(msg)
Expand Down
34 changes: 30 additions & 4 deletions tests/test_cli_compile.py
Expand Up @@ -2192,17 +2192,16 @@ def test_multiple_extras(fake_dists, runner, make_module, fname, content, extra_

@pytest.mark.network
@pytest.mark.parametrize(("fname", "content"), METADATA_TEST_CASES)
def test_wildcard_extras(fake_dists, runner, make_module, fname, content):
def test_all_extras(fake_dists, runner, make_module, fname, content):
"""
Test passing wildcard `--extra` includes all applicable extras.
Test passing `--all-extras` includes all applicable extras.
"""
meta_path = make_module(fname=fname, content=content)
out = runner.invoke(
cli,
[
"-n",
"--extra",
"*",
"--all-extras",
"--find-links",
fake_dists,
"--no-annotate",
Expand All @@ -2228,6 +2227,33 @@ def test_wildcard_extras(fake_dists, runner, make_module, fname, content):
)


# This should not depend on the metadata format so testing all cases is wasteful
@pytest.mark.parametrize(("fname", "content"), METADATA_TEST_CASES[:1])
def test_all_extras_fail_with_extra(fake_dists, runner, make_module, fname, content):
"""
Test that passing `--all-extras` and `--extra` fails.
"""
meta_path = make_module(fname=fname, content=content)
out = runner.invoke(
cli,
[
"-n",
"--all-extras",
"--extra",
"dev",
"--find-links",
fake_dists,
"--no-annotate",
"--no-emit-options",
"--no-header",
meta_path,
],
)
assert out.exit_code == 2
exp = "--extra has no effect when used with --all-extras"
assert exp in out.stderr


def test_extras_fail_with_requirements_in(runner, tmpdir):
"""
Test that passing `--extra` with `requirements.in` input file fails.
Expand Down

0 comments on commit b7dde7d

Please sign in to comment.