From 1faf6116e189b9ca3fc9a47b763c593dfbbaf26b Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Sun, 28 Aug 2022 19:58:07 +1000 Subject: [PATCH 1/2] Handle multiple extras and invalid extras --- src/poetry_plugin_export/command.py | 12 +++++++++- tests/command/test_command_export.py | 36 ++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/poetry_plugin_export/command.py b/src/poetry_plugin_export/command.py index 5119eb0..2b2353a 100644 --- a/src/poetry_plugin_export/command.py +++ b/src/poetry_plugin_export/command.py @@ -82,9 +82,19 @@ def handle(self) -> None: "" ) + # 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")) diff --git a/tests/command/test_command_export.py b/tests/command/test_command_export.py index a6c44c2..0460adf 100644 --- a/tests/command/test_command_export.py +++ b/tests/command/test_command_export.py @@ -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" @@ -63,6 +64,7 @@ [tool.poetry.extras] feature_bar = ["bar"] +feature_qux = ["qux"] """ @@ -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 @@ -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: From 842689105864620a21e43aa44cba34716b479158 Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Mon, 29 Aug 2022 17:36:04 +1000 Subject: [PATCH 2/2] add 'all' and 'all-extras' options --- src/poetry_plugin_export/command.py | 25 ++++++++++++++++++++++++- tests/command/test_command_export.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/poetry_plugin_export/command.py b/src/poetry_plugin_export/command.py index 2b2353a..d23faca 100644 --- a/src/poetry_plugin_export/command.py +++ b/src/poetry_plugin_export/command.py @@ -31,6 +31,16 @@ class ExportCommand(InstallerCommand): None, "Include development dependencies. (Deprecated)", ), + option( + "all", + None, + "Include all groups and extras", + ), + option( + "all-extras", + None, + "Include all extras", + ), *InstallerCommand._group_dependency_options(), option( "extras", @@ -82,6 +92,8 @@ def handle(self) -> None: "" ) + groups = self.activated_groups + # Checking extras extras = { extra for extra_opt in self.option("extras") for extra in extra_opt.split() @@ -92,8 +104,19 @@ def handle(self) -> None: 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.only_groups(list(groups)) exporter.with_extras(list(extras)) exporter.with_hashes(not self.option("without-hashes")) exporter.with_credentials(self.option("with-credentials")) diff --git a/tests/command/test_command_export.py b/tests/command/test_command_export.py index 0460adf..87524f2 100644 --- a/tests/command/test_command_export.py +++ b/tests/command/test_command_export.py @@ -211,6 +211,34 @@ def test_export_reports_invalid_extras(tester: CommandTester, do_lock: None) -> 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: