diff --git a/src/poetry_plugin_export/command.py b/src/poetry_plugin_export/command.py index 150c86d..eabe1fa 100644 --- a/src/poetry_plugin_export/command.py +++ b/src/poetry_plugin_export/command.py @@ -41,6 +41,40 @@ class ExportCommand(InstallerCommand): option("with-credentials", None, "Include credentials for extra indices."), ] + @property + def activated_groups(self) -> set[str]: + groups = {} + + for key in {"with", "without", "only"}: + groups[key] = { + group.strip() + for groups in self.option(key) + for group in groups.split(",") + } + + for opt, new, group in [ + ("dev", "with", "dev"), + ]: + if self.io.input.has_option(opt) and self.option(opt): + self.line_error( + f"The `--{opt}` option is" + f" deprecated, use the `--{new} {group}`" + " notation instead." + ) + groups[new].add(group) + + if groups["only"] and (groups["with"] or groups["without"]): + self.line_error( + "The `--with` and " + "`--without` options are ignored when used" + " along with the `--only` option." + "" + ) + + return groups["only"] or {"default"}.union(groups["with"]).difference( + groups["without"] + ) + def handle(self) -> None: fmt = self.option("format") diff --git a/tests/command/test_command_export.py b/tests/command/test_command_export.py index 3df7d29..848d3cf 100644 --- a/tests/command/test_command_export.py +++ b/tests/command/test_command_export.py @@ -50,6 +50,16 @@ foo = "^1.0" bar = { version = "^1.1", optional = true } +[tool.poetry.group.dev.dependencies] +baz = "^2.0" + +[tool.poetry.group.opt] +optional = true + +[tool.poetry.group.opt.dependencies] +opt = "^2.2" + + [tool.poetry.extras] feature_bar = ["bar"] """ @@ -59,6 +69,8 @@ def setup(repo: Repository) -> None: repo.add_package(Package("foo", "1.0.0")) repo.add_package(Package("bar", "1.1.0")) + repo.add_package(Package("baz", "2.0.0")) + repo.add_package(Package("opt", "2.2.0")) @pytest.fixture @@ -129,6 +141,33 @@ def test_export_uses_requirements_txt_format_by_default( assert tester.io.fetch_output() == expected +@pytest.mark.parametrize( + "options, expected", + [ + ("", f"foo==1.0.0 ; {MARKER_PY}\n"), + ("--with dev", f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\n"), + ("--with opt", f"foo==1.0.0 ; {MARKER_PY}\nopt==2.2.0 ; {MARKER_PY}\n"), + ( + "--with dev,opt", + f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\nopt==2.2.0 ;" + f" {MARKER_PY}\n", + ), + ("--without default", "\n"), + ("--without dev", f"foo==1.0.0 ; {MARKER_PY}\n"), + ("--without opt", f"foo==1.0.0 ; {MARKER_PY}\n"), + ("--without default,dev,opt", "\n"), + ("--only default", f"foo==1.0.0 ; {MARKER_PY}\n"), + ("--only dev", f"baz==2.0.0 ; {MARKER_PY}\n"), + ("--only default,dev", f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\n"), + ], +) +def test_export_groups( + tester: CommandTester, do_lock: None, options: str, expected: str +): + tester.execute(options) + assert tester.io.fetch_output() == expected + + def test_export_includes_extras_by_flag(tester: CommandTester, do_lock: None): tester.execute("--format requirements.txt --extras feature_bar") expected = f"""\