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

feat(install): add --all-groups flag to install all dependency groups #9744

Merged
merged 7 commits into from
Oct 15, 2024
8 changes: 8 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ You can also select optional dependency groups with the `--with` option.
poetry install --with test,docs
```

To install all dependency groups including the optional groups, use the ``--all-groups`` flag.

```bash
poetry install --all-groups
```

It's also possible to only install specific dependency groups by using the `only` option.

```bash
Expand Down Expand Up @@ -261,12 +267,14 @@ poetry install --compile
* `--dry-run`: Output the operations but do not execute anything (implicitly enables --verbose).
* `--extras (-E)`: Features to install (multiple values allowed).
* `--all-extras`: Install all extra features (conflicts with --extras).
* `--all-groups`: Install dependencies from all groups (conflicts with --only).
* `--compile`: Compile Python source files to bytecode.
* `--no-dev`: Do not install dev dependencies. (**Deprecated**, use `--only main` or `--without dev` instead)
* `--remove-untracked`: Remove dependencies not presented in the lock file. (**Deprecated**, use `--sync` instead)

{{% note %}}
When `--only` is specified, `--with` and `--without` options are ignored.
When `--all-groups` is specified, the `--with` option is ignored.
schneebuzz marked this conversation as resolved.
Show resolved Hide resolved
{{% /note %}}


Expand Down
12 changes: 12 additions & 0 deletions src/poetry/console/commands/group_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ def activated_groups(self) -> set[str]:
for groups in self.option(key, "")
for group in groups.split(",")
}

if self.option("all-groups"):
if self.option("with"):
self.line_error(
"<warning>The `<fg=yellow;options=bold>--with</>` option is"
" ignored when using the `<fg=yellow;options=bold>--all-groups</>`"
" option.</warning>"
)
groups["with"] = self.poetry.package.dependency_group_names(
include_optional=True
)

self._validate_group_options(groups)

for opt, new, group in [
Expand Down
16 changes: 13 additions & 3 deletions src/poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class InstallCommand(InstallerCommand):
multiple=True,
),
option("all-extras", None, "Install all extra dependencies."),
option("all-groups", None, "Install dependencies from all groups."),
option("only-root", None, "Exclude all dependencies."),
option(
"compile",
Expand Down Expand Up @@ -116,12 +117,14 @@ def handle(self) -> int:
return 1

if self.option("only-root") and any(
self.option(key) for key in {"with", "without", "only"}
self.option(key) for key in {"with", "without", "only", "all-groups"}
):
self.line_error(
"<error>The `<fg=yellow;options=bold>--with</>`,"
" `<fg=yellow;options=bold>--without</>` and"
" `<fg=yellow;options=bold>--only</>` options cannot be used with"
" `<fg=yellow;options=bold>--without</>`,"
" `<fg=yellow;options=bold>--only</>` and"
" `<fg=yellow;options=bold>--all-groups</>`"
" options cannot be used with"
" the `<fg=yellow;options=bold>--only-root</>`"
" option.</error>"
)
Expand All @@ -134,6 +137,13 @@ def handle(self) -> int:
)
return 1

if self.option("only") and self.option("all-groups"):
self.line_error(
"<error>You cannot specify `<fg=yellow;options=bold>--all-groups</>`"
" when using `<fg=yellow;options=bold>--only</>`.</error>"
)
return 1

extras: list[str]
if self.option("all-extras"):
extras = list(self.poetry.package.extras.keys())
Expand Down
33 changes: 31 additions & 2 deletions tests/console/commands/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
[tool.poetry.group.bam.dependencies]
bam = "^1.4"

[tool.poetry.group.bum]
optional = true

[tool.poetry.group.bum.dependencies]
bam = "^1.5"

[tool.poetry.extras]
extras_a = [ "fizz" ]
extras_b = [ "buzz" ]
Expand Down Expand Up @@ -104,6 +110,12 @@ def _project_factory(
("--without foo,bar", {MAIN_GROUP, "baz", "bim"}),
(f"--without {MAIN_GROUP}", {"foo", "bar", "baz", "bim"}),
("--with foo,bar --without baz --without bim --only bam", {"bam"}),
("--all-groups", {MAIN_GROUP, "foo", "bar", "baz", "bim", "bam", "bum"}),
(
"--all-groups --with bum",
{MAIN_GROUP, "foo", "bar", "baz", "bim", "bam", "bum"},
),
("--all-groups --without bum", {MAIN_GROUP, "foo", "bar", "baz", "bim", "bam"}),
# net result zero options
("--with foo", {MAIN_GROUP, "foo", "bar", "baz", "bim"}),
("--without bam", {MAIN_GROUP, "foo", "bar", "baz", "bim"}),
Expand Down Expand Up @@ -287,9 +299,10 @@ def test_extras_conflicts_all_extras(
"--without foo",
"--with foo,bar --without baz",
"--only foo",
"--all-groups",
],
)
def test_only_root_conflicts_with_without_only(
def test_only_root_conflicts_with_without_only_all_groups(
options: str,
tester: CommandTester,
mocker: MockerFixture,
Expand All @@ -302,11 +315,27 @@ def test_only_root_conflicts_with_without_only(
assert tester.status_code == 1
assert (
tester.io.fetch_error()
== "The `--with`, `--without` and `--only` options cannot be used with"
== "The `--with`, `--without`, `--only` and `--all-groups` options cannot be used with"
" the `--only-root` option.\n"
)


def test_only_conflicts_with_all_groups(
tester: CommandTester,
mocker: MockerFixture,
) -> None:
assert isinstance(tester.command, InstallerCommand)
mocker.patch.object(tester.command.installer, "run", return_value=0)

tester.execute("--only foo --all-groups")

assert tester.status_code == 1
assert (
tester.io.fetch_error()
== "You cannot specify `--all-groups` when using `--only`.\n"
)


@pytest.mark.parametrize(
("options", "valid_groups", "should_raise"),
[
Expand Down
Loading