From 111118a73c31f965361ac862fabe70cbbd248cd9 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Wed, 30 Oct 2024 18:59:06 +0100 Subject: [PATCH] feat(cli): add --markers to add command (#9814) --- docs/cli.md | 1 + src/poetry/console/commands/add.py | 9 +++++++++ tests/console/commands/test_add.py | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/docs/cli.md b/docs/cli.md index 37f1695de6b..b815a51c7ac 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -461,6 +461,7 @@ about dependency groups. * `--optional`: Add as an optional dependency to an extra. * `--python`: Python version for which the dependency must be installed. * `--platform`: Platforms for which the dependency must be installed. +* `--markers`: Environment markers which describe when the dependency should be installed. * `--source`: Name of the source to use to install the package. * `--allow-prereleases`: Accept prereleases. * `--dry-run`: Output the operations but do not execute anything (implicitly enables `--verbose`). diff --git a/src/poetry/console/commands/add.py b/src/poetry/console/commands/add.py index cfd7bc009a1..055c6c42005 100644 --- a/src/poetry/console/commands/add.py +++ b/src/poetry/console/commands/add.py @@ -71,6 +71,12 @@ class AddCommand(InstallerCommand, InitCommand): "Platforms for which the dependency must be installed.", flag=False, ), + option( + "markers", + None, + "Environment markers which describe when the dependency should be installed.", + flag=False, + ), option( "source", None, @@ -263,6 +269,9 @@ def handle(self) -> int: if self.option("platform"): constraint["platform"] = self.option("platform") + if self.option("markers"): + constraint["markers"] = self.option("markers") + if self.option("source"): constraint["source"] = self.option("source") diff --git a/tests/console/commands/test_add.py b/tests/console/commands/test_add.py index 9c7509d7c52..88794c859fd 100644 --- a/tests/console/commands/test_add.py +++ b/tests/console/commands/test_add.py @@ -347,6 +347,18 @@ def test_add_constraint_dependencies(tester: CommandTester) -> None: assert tester.command.installer.executor.installations_count == 2 +def test_add_with_markers(app: PoetryTestApplication, tester: CommandTester) -> None: + marker = "python_version <= '3.4' or sys_platform == 'win32'" + tester.execute(f"""cachy --markers "{marker}" """) + + pyproject: dict[str, Any] = app.poetry.file.read() + content = pyproject["tool"]["poetry"] + + assert "cachy" in content["dependencies"] + assert content["dependencies"]["cachy"]["version"] == "^0.2.0" + assert content["dependencies"]["cachy"]["markers"] == marker + + def test_add_git_constraint( app: PoetryTestApplication, tester: CommandTester,