Skip to content

Commit 2428cdd

Browse files
pouillonradoering
andauthored
Add a '--all-extras' option (#241)
Co-authored-by: Randy Döring <[email protected]>
1 parent c0efd0e commit 2428cdd

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ poetry export -f requirements.txt --output requirements.txt
5050
* `--default`: Only export the main dependencies. (**Deprecated**)
5151
* `--dev`: Include development dependencies. (**Deprecated**)
5252
* `--extras (-E)`: Extra sets of dependencies to include.
53+
* `--all-extras`: Include all sets of extra dependencies.
5354
* `--without-hashes`: Exclude hashes from the exported file.
5455
* `--with-credentials`: Include credentials for extra indices.

docs/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ poetry export --only test,docs
7373
* `--default`: Only export the main dependencies. (**Deprecated**)
7474
* {{< option name="dev" deprecated=true >}}Include development dependencies.{{< /option >}}
7575
* `--extras (-E)`: Extra sets of dependencies to include.
76+
* `--all-extras`: Include all sets of extra dependencies.
7677
* `--without-hashes`: Exclude hashes from the exported file.
7778
* `--with-credentials`: Include credentials for extra indices.

src/poetry_plugin_export/command.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
from __future__ import annotations
22

33
from pathlib import Path
4+
from typing import TYPE_CHECKING
45

56
from cleo.helpers import option
7+
from packaging.utils import NormalizedName
68
from packaging.utils import canonicalize_name
79
from poetry.console.commands.group_command import GroupCommand
810
from poetry.core.packages.dependency_group import MAIN_GROUP
911

1012
from poetry_plugin_export.exporter import Exporter
1113

1214

15+
if TYPE_CHECKING:
16+
from collections.abc import Iterable
17+
18+
1319
class ExportCommand(GroupCommand):
1420
name = "export"
1521
description = "Exports the lock file to alternative formats."
@@ -43,6 +49,7 @@ class ExportCommand(GroupCommand):
4349
flag=False,
4450
multiple=True,
4551
),
52+
option("all-extras", None, "Include all sets of extra dependencies."),
4653
option("with-credentials", None, "Include credentials for extra indices."),
4754
]
4855

@@ -86,16 +93,28 @@ def handle(self) -> int:
8693
)
8794

8895
# Checking extras
89-
extras = {
90-
canonicalize_name(extra)
91-
for extra_opt in self.option("extras")
92-
for extra in extra_opt.split()
93-
}
94-
invalid_extras = extras - self.poetry.package.extras.keys()
95-
if invalid_extras:
96-
raise ValueError(
97-
f"Extra [{', '.join(sorted(invalid_extras))}] is not specified."
96+
if self.option("extras") and self.option("all-extras"):
97+
self.line_error(
98+
"<error>You cannot specify explicit"
99+
" `<fg=yellow;options=bold>--extras</>` while exporting"
100+
" using `<fg=yellow;options=bold>--all-extras</>`.</error>"
98101
)
102+
return 1
103+
104+
extras: Iterable[NormalizedName]
105+
if self.option("all-extras"):
106+
extras = self.poetry.package.extras.keys()
107+
else:
108+
extras = {
109+
canonicalize_name(extra)
110+
for extra_opt in self.option("extras")
111+
for extra in extra_opt.split()
112+
}
113+
invalid_extras = extras - self.poetry.package.extras.keys()
114+
if invalid_extras:
115+
raise ValueError(
116+
f"Extra [{', '.join(sorted(invalid_extras))}] is not specified."
117+
)
99118

100119
exporter = Exporter(self.poetry, self.io)
101120
exporter.only_groups(list(self.activated_groups))

tests/command/test_command_export.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,24 @@ def test_export_reports_invalid_extras(tester: CommandTester, do_lock: None) ->
220220
assert str(error.value) == expected
221221

222222

223+
def test_export_with_all_extras(tester: CommandTester, do_lock: None) -> None:
224+
tester.execute("--format requirements.txt --all-extras")
225+
output = tester.io.fetch_output()
226+
assert f"bar==1.1.0 ; {MARKER_PY}" in output
227+
assert f"qux==1.2.0 ; {MARKER_PY}" in output
228+
229+
230+
def test_extras_conflicts_all_extras(tester: CommandTester, do_lock: None) -> None:
231+
tester.execute("--extras bar --all-extras")
232+
233+
assert tester.status_code == 1
234+
assert (
235+
tester.io.fetch_error()
236+
== "You cannot specify explicit `--extras` while exporting using"
237+
" `--all-extras`.\n"
238+
)
239+
240+
223241
def test_export_with_urls(
224242
monkeypatch: MonkeyPatch, tester: CommandTester, poetry: Poetry
225243
) -> None:

0 commit comments

Comments
 (0)