Skip to content

Commit dc91a5f

Browse files
authored
Handle multiple extras and invalid extras (#103)
Co-authored-by: KotlinIsland <[email protected]>
1 parent 7e6222f commit dc91a5f

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/poetry_plugin_export/command.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,19 @@ def handle(self) -> None:
8282
"</warning>"
8383
)
8484

85+
# Checking extras
86+
extras = {
87+
extra for extra_opt in self.option("extras") for extra in extra_opt.split()
88+
}
89+
invalid_extras = extras - self.poetry.package.extras.keys()
90+
if invalid_extras:
91+
raise ValueError(
92+
f"Extra [{', '.join(sorted(invalid_extras))}] is not specified."
93+
)
94+
8595
exporter = Exporter(self.poetry)
8696
exporter.only_groups(list(self.activated_groups))
87-
exporter.with_extras(self.option("extras"))
97+
exporter.with_extras(list(extras))
8898
exporter.with_hashes(not self.option("without-hashes"))
8999
exporter.with_credentials(self.option("with-credentials"))
90100
exporter.with_urls(not self.option("without-urls"))

tests/command/test_command_export.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
python = "~2.7 || ^3.6"
5151
foo = "^1.0"
5252
bar = { version = "^1.1", optional = true }
53+
qux = { version = "^1.2", optional = true }
5354
5455
[tool.poetry.group.dev.dependencies]
5556
baz = "^2.0"
@@ -63,6 +64,7 @@
6364
6465
[tool.poetry.extras]
6566
feature_bar = ["bar"]
67+
feature_qux = ["qux"]
6668
"""
6769

6870

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

7679

7780
@pytest.fixture
@@ -174,15 +177,40 @@ def test_export_groups(
174177
assert tester.io.fetch_output() == expected
175178

176179

177-
def test_export_includes_extras_by_flag(tester: CommandTester, do_lock: None) -> None:
178-
tester.execute("--format requirements.txt --extras feature_bar")
179-
expected = f"""\
180+
@pytest.mark.parametrize(
181+
"extras, expected",
182+
[
183+
(
184+
"feature_bar",
185+
f"""\
180186
bar==1.1.0 ; {MARKER_PY}
181187
foo==1.0.0 ; {MARKER_PY}
182-
"""
188+
""",
189+
),
190+
(
191+
"feature_bar feature_qux",
192+
f"""\
193+
bar==1.1.0 ; {MARKER_PY}
194+
foo==1.0.0 ; {MARKER_PY}
195+
qux==1.2.0 ; {MARKER_PY}
196+
""",
197+
),
198+
],
199+
)
200+
def test_export_includes_extras_by_flag(
201+
tester: CommandTester, do_lock: None, extras: str, expected: str
202+
) -> None:
203+
tester.execute(f"--format requirements.txt --extras '{extras}'")
183204
assert tester.io.fetch_output() == expected
184205

185206

207+
def test_export_reports_invalid_extras(tester: CommandTester, do_lock: None) -> None:
208+
with pytest.raises(ValueError) as error:
209+
tester.execute("--format requirements.txt --extras 'SUS AMONGUS'")
210+
expected = "Extra [AMONGUS, SUS] is not specified."
211+
assert str(error.value) == expected
212+
213+
186214
def test_export_with_urls(
187215
monkeypatch: MonkeyPatch, tester: CommandTester, poetry: Poetry
188216
) -> None:

0 commit comments

Comments
 (0)