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

all and all-extras #105

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 35 additions & 2 deletions src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class ExportCommand(InstallerCommand):
None,
"Include development dependencies. (<warning>Deprecated</warning>)",
),
option(
"all",
None,
"Include all groups and extras",
),
option(
"all-extras",
None,
"Include all extras",
),
*InstallerCommand._group_dependency_options(),
option(
"extras",
Expand Down Expand Up @@ -82,9 +92,32 @@ def handle(self) -> None:
"</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."
)

# 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'
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())

exporter = Exporter(self.poetry)
exporter.only_groups(list(self.activated_groups))
exporter.with_extras(self.option("extras"))
exporter.only_groups(list(groups))
exporter.with_extras(list(extras))
exporter.with_hashes(not self.option("without-hashes"))
exporter.with_credentials(self.option("with-credentials"))
exporter.with_urls(not self.option("without-urls"))
Expand Down
60 changes: 58 additions & 2 deletions tests/command/test_command_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
python = "~2.7 || ^3.6"
foo = "^1.0"
bar = { version = "^1.1", optional = true }
qux = { version = "^1.2", optional = true }

[tool.poetry.group.dev.dependencies]
baz = "^2.0"
Expand All @@ -63,6 +64,7 @@

[tool.poetry.extras]
feature_bar = ["bar"]
feature_qux = ["qux"]
"""


Expand All @@ -72,6 +74,7 @@ def setup(repo: Repository) -> None:
repo.add_package(Package("bar", "1.1.0"))
repo.add_package(Package("baz", "2.0.0"))
repo.add_package(Package("opt", "2.2.0"))
repo.add_package(Package("qux", "1.2.0"))


@pytest.fixture
Expand Down Expand Up @@ -174,15 +177,68 @@ def test_export_groups(
assert tester.io.fetch_output() == expected


def test_export_includes_extras_by_flag(tester: CommandTester, do_lock: None) -> None:
tester.execute("--format requirements.txt --extras feature_bar")
@pytest.mark.parametrize(
"extras, expected",
[
(
"feature_bar",
f"""\
bar==1.1.0 ; {MARKER_PY}
foo==1.0.0 ; {MARKER_PY}
""",
),
(
"feature_bar feature_qux",
f"""\
bar==1.1.0 ; {MARKER_PY}
foo==1.0.0 ; {MARKER_PY}
qux==1.2.0 ; {MARKER_PY}
""",
),
],
)
def test_export_includes_extras_by_flag(
tester: CommandTester, do_lock: None, extras: str, expected: str
) -> None:
tester.execute(f"--format requirements.txt --extras '{extras}'")
assert tester.io.fetch_output() == expected


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."
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_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_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_urls(
monkeypatch: MonkeyPatch, tester: CommandTester, poetry: Poetry
) -> None:
Expand Down