Skip to content

Commit

Permalink
Handle multiple extras and invalid extras (#103)
Browse files Browse the repository at this point in the history
Co-authored-by: KotlinIsland <kotlinisland@users.noreply.github.com>
  • Loading branch information
KotlinIsland and KotlinIsland authored Sep 2, 2022
1 parent 7e6222f commit dc91a5f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,19 @@ def handle(self) -> None:
"</warning>"
)

# 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."
)

exporter = Exporter(self.poetry)
exporter.only_groups(list(self.activated_groups))
exporter.with_extras(self.option("extras"))
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
36 changes: 32 additions & 4 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,40 @@ 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")
expected = f"""\
@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_export_with_urls(
monkeypatch: MonkeyPatch, tester: CommandTester, poetry: Poetry
) -> None:
Expand Down

0 comments on commit dc91a5f

Please sign in to comment.