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 a '--all-extras' option #241

Merged
merged 2 commits into from
Oct 30, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ poetry export -f requirements.txt --output requirements.txt
* `--default`: Only export the main dependencies. (**Deprecated**)
* `--dev`: Include development dependencies. (**Deprecated**)
* `--extras (-E)`: Extra sets of dependencies to include.
* `--all-extras`: Include all sets of extra dependencies.
* `--without-hashes`: Exclude hashes from the exported file.
* `--with-credentials`: Include credentials for extra indices.
1 change: 1 addition & 0 deletions docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ poetry export --only test,docs
* `--default`: Only export the main dependencies. (**Deprecated**)
* {{< option name="dev" deprecated=true >}}Include development dependencies.{{< /option >}}
* `--extras (-E)`: Extra sets of dependencies to include.
* `--all-extras`: Include all sets of extra dependencies.
* `--without-hashes`: Exclude hashes from the exported file.
* `--with-credentials`: Include credentials for extra indices.
37 changes: 28 additions & 9 deletions src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

from cleo.helpers import option
from packaging.utils import NormalizedName
from packaging.utils import canonicalize_name
from poetry.console.commands.group_command import GroupCommand
from poetry.core.packages.dependency_group import MAIN_GROUP

from poetry_plugin_export.exporter import Exporter


if TYPE_CHECKING:
from collections.abc import Iterable


class ExportCommand(GroupCommand):
name = "export"
description = "Exports the lock file to alternative formats."
Expand Down Expand Up @@ -43,6 +49,7 @@ class ExportCommand(GroupCommand):
flag=False,
multiple=True,
),
option("all-extras", None, "Include all sets of extra dependencies."),
radoering marked this conversation as resolved.
Show resolved Hide resolved
option("with-credentials", None, "Include credentials for extra indices."),
]

Expand Down Expand Up @@ -86,16 +93,28 @@ def handle(self) -> int:
)

# Checking extras
extras = {
canonicalize_name(extra)
for extra_opt in self.option("extras")
for extra in extra_opt.split()
}
invalid_extras = extras - self.poetry.package.extras.keys()
if invalid_extras:
raise ValueError(
f"Extra [{', '.join(sorted(invalid_extras))}] is not specified."
if self.option("extras") and self.option("all-extras"):
self.line_error(
"<error>You cannot specify explicit"
" `<fg=yellow;options=bold>--extras</>` while exporting"
" using `<fg=yellow;options=bold>--all-extras</>`.</error>"
)
return 1

extras: Iterable[NormalizedName]
if self.option("all-extras"):
extras = self.poetry.package.extras.keys()
else:
extras = {
canonicalize_name(extra)
for extra_opt in self.option("extras")
for extra in extra_opt.split()
}
radoering marked this conversation as resolved.
Show resolved Hide resolved
invalid_extras = extras - self.poetry.package.extras.keys()
if invalid_extras:
raise ValueError(
f"Extra [{', '.join(sorted(invalid_extras))}] is not specified."
)

exporter = Exporter(self.poetry, self.io)
exporter.only_groups(list(self.activated_groups))
Expand Down
18 changes: 18 additions & 0 deletions tests/command/test_command_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ def test_export_reports_invalid_extras(tester: CommandTester, do_lock: None) ->
assert str(error.value) == expected


def test_export_with_all_extras(tester: CommandTester, do_lock: None) -> None:
tester.execute("--format requirements.txt --all-extras")
output = tester.io.fetch_output()
assert f"bar==1.1.0 ; {MARKER_PY}" in output
assert f"qux==1.2.0 ; {MARKER_PY}" in output


def test_extras_conflicts_all_extras(tester: CommandTester, do_lock: None) -> None:
tester.execute("--extras bar --all-extras")

assert tester.status_code == 1
assert (
tester.io.fetch_error()
== "You cannot specify explicit `--extras` while exporting using"
" `--all-extras`.\n"
)


def test_export_with_urls(
monkeypatch: MonkeyPatch, tester: CommandTester, poetry: Poetry
) -> None:
Expand Down