Skip to content

Commit

Permalink
all groups option for peotry export
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiafi committed Aug 16, 2024
1 parent af26af6 commit a8c2fe4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 53 deletions.
72 changes: 42 additions & 30 deletions src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
@@ -1,14 +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 All @@ -34,16 +41,8 @@ class ExportCommand(GroupCommand):
None,
"Include development dependencies. (<warning>Deprecated</warning>)",
),
option(
"all",
None,
"Include all groups and extras",
),
option(
"all-extras",
None,
"Include all extras",
),
option("all-groups", None, "Include all groups"),
option("all-extras", None, "Include all extras"),
*GroupCommand._group_dependency_options(),
option(
"extras",
Expand All @@ -52,7 +51,6 @@ class ExportCommand(GroupCommand):
flag=False,
multiple=True,
),
option("all-extras", None, "Include all sets of extra dependencies."),
option("with-credentials", None, "Include credentials for extra indices."),
]

Expand Down Expand Up @@ -95,28 +93,42 @@ def handle(self) -> int:
"</warning>"
)

groups = self.activated_groups

# Checking extras
extras = {
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

# handle 'all'
if self.option("all"):
extras = set(self.poetry.package.extras.keys())
groups = self.poetry.package.dependency_group_names(include_optional=True)

# handle 'all-extras'
extras: Iterable[NormalizedName]
if self.option("all-extras"):
if self.option("extras"):
raise ValueError("Can't have --all-extras and --extras together.")
extras = set(self.poetry.package.extras.keys())
extras = self.poetry.package.extras.keys()
else:
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("with") and self.option("all-groups"):
self.line_error(
"<error>You cannot specify explicit"
" `<fg=yellow;options=bold>--with</>` while exporting"
" using `<fg=yellow;options=bold>--all-groups</>`.</error>"
)
return 1

groups = (
self.poetry.package.dependency_group_names(include_optional=True)
if self.option("all-groups")
else self.activated_groups
)

exporter = Exporter(self.poetry, self.io)
exporter.only_groups(list(groups))
Expand Down
52 changes: 29 additions & 23 deletions tests/command/test_command_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,36 +216,42 @@ def test_export_includes_extras_by_flag(
def test_export_reports_invalid_extras(tester: CommandTester, do_lock: None) -> None:
with pytest.raises(ValueError) as error:
tester.execute("--format requirements.txt --extras 'SUS AMONGUS'")
expected = "Extra [AMONGUS, SUS] is not specified."
expected = "Extra [amongus, sus] is not specified."
assert str(error.value) == expected


def test_all_option(tester: CommandTester, do_lock: None) -> None:
tester.execute("--all")
expected = f"""\
bar==1.1.0 ; {MARKER_PY}
baz==2.0.0 ; {MARKER_PY}
foo==1.0.0 ; {MARKER_PY}
opt==2.2.0 ; {MARKER_PY}
qux==1.2.0 ; {MARKER_PY}
"""
assert tester.io.fetch_output() == 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_all_extras_option(tester: CommandTester, do_lock: None) -> None:
tester.execute("--all-extras")
expected = f"""\
bar==1.1.0 ; {MARKER_PY}
foo==1.0.0 ; {MARKER_PY}
qux==1.2.0 ; {MARKER_PY}
"""
assert tester.io.fetch_output() == expected
def test_extras_conflicts_all_extras(tester: CommandTester, do_lock: None) -> None:
tester.execute("--extras bar --all-extras")

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

def test_all_extras_and_extras(tester: CommandTester, do_lock: None) -> None:
with pytest.raises(ValueError) as error:
tester.execute("--all-extras --extras 'feature_bar'")
assert str(error.value) == "Can't have --all-extras and --extras together."

def test_export_with_all_groups(tester: CommandTester, do_lock: None) -> None:
tester.execute("--format requirements.txt --all-groups")
output = tester.io.fetch_output()
assert f"baz==2.0.0 ; {MARKER_PY}" in output
assert f"opt==2.2.0 ; {MARKER_PY}" in output


def test_with_conflicts_all_groups(tester: CommandTester, do_lock: None) -> None:
tester.execute("--with=bar --all-groups")

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


def test_export_with_urls(
Expand Down

0 comments on commit a8c2fe4

Please sign in to comment.