From 520ac3dcd497473f6488ab53bfd7954dedfa5a12 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 08:08:15 -0700 Subject: [PATCH 01/10] Add `--only-root` option to group commands, excluding all dependencies. --- src/poetry/console/commands/group_command.py | 19 +++++ tests/console/commands/test_install.py | 1 + tests/console/commands/test_show.py | 73 ++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/src/poetry/console/commands/group_command.py b/src/poetry/console/commands/group_command.py index e872721667b..7ca21b4771e 100644 --- a/src/poetry/console/commands/group_command.py +++ b/src/poetry/console/commands/group_command.py @@ -43,6 +43,13 @@ def _group_dependency_options() -> list[Option]: flag=False, multiple=True, ), + option( + "only-root", + None, + "Exclude all dependencies.", + flag=True, + multiple=False, + ), ] @property @@ -77,6 +84,18 @@ def default_groups(self) -> set[str]: def activated_groups(self) -> set[str]: groups = {} + group_keys = {"with", "without", "only"} + if self.option("only-root") and any([self.option(key) for key in group_keys]): + self.line_error( + "The `--with`," + " `--without` and" + " `--only` options are ignored when used" + " along with the `--only-root` option." + ) + + if self.option("only-root"): + return {} + for key in {"with", "without", "only"}: groups[key] = { group.strip() diff --git a/tests/console/commands/test_install.py b/tests/console/commands/test_install.py index a931c448d58..c6c33f66f88 100644 --- a/tests/console/commands/test_install.py +++ b/tests/console/commands/test_install.py @@ -73,6 +73,7 @@ def tester( ("options", "groups"), [ ("", {MAIN_GROUP, "foo", "bar", "baz", "bim"}), + ("--only-root", set()), (f"--only {MAIN_GROUP}", {MAIN_GROUP}), ("--only foo", {"foo"}), ("--only foo,bar", {"foo", "bar"}), diff --git a/tests/console/commands/test_show.py b/tests/console/commands/test_show.py index 2a92347af65..32a27e8e74c 100644 --- a/tests/console/commands/test_show.py +++ b/tests/console/commands/test_show.py @@ -1451,6 +1451,79 @@ def test_show_non_dev_with_basic_installed_packages( assert tester.io.fetch_output() == expected +def test_show_with_group_only_root( + tester: CommandTester, poetry: Poetry, installed: Repository +): + poetry.package.add_dependency(Factory.create_dependency("cachy", "^0.1.0")) + poetry.package.add_dependency(Factory.create_dependency("pendulum", "^2.0.0")) + poetry.package.add_dependency( + Factory.create_dependency("pytest", "*", groups=["dev"]) + ) + + cachy_010 = get_package("cachy", "0.1.0") + cachy_010.description = "Cachy package" + + pendulum_200 = get_package("pendulum", "2.0.0") + pendulum_200.description = "Pendulum package" + + pytest_373 = get_package("pytest", "3.7.3") + pytest_373.description = "Pytest package" + pytest_373.category = "dev" + + installed.add_package(cachy_010) + installed.add_package(pendulum_200) + installed.add_package(pytest_373) + + poetry.locker.mock_lock_data( + { + "package": [ + { + "name": "cachy", + "version": "0.1.0", + "description": "Cachy package", + "category": "main", + "optional": False, + "platform": "*", + "python-versions": "*", + "checksum": [], + }, + { + "name": "pendulum", + "version": "2.0.0", + "description": "Pendulum package", + "category": "main", + "optional": False, + "platform": "*", + "python-versions": "*", + "checksum": [], + }, + { + "name": "pytest", + "version": "3.7.3", + "description": "Pytest package", + "category": "dev", + "optional": False, + "platform": "*", + "python-versions": "*", + "checksum": [], + }, + ], + "metadata": { + "python-versions": "*", + "platform": "*", + "content-hash": "123456789", + "hashes": {"cachy": [], "pendulum": [], "pytest": []}, + }, + } + ) + + tester.execute("--only-root") + + expected = "" + + assert tester.io.fetch_output() == expected + + def test_show_with_group_only( tester: CommandTester, poetry: Poetry, installed: Repository ): From cdf48d25370257d966a075797d0262a8597cd9a9 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 08:12:16 -0700 Subject: [PATCH 02/10] Add documentation for usage. --- docs/managing-dependencies.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/managing-dependencies.md b/docs/managing-dependencies.md index 5d0046e17c2..b8b5f8636cf 100644 --- a/docs/managing-dependencies.md +++ b/docs/managing-dependencies.md @@ -158,6 +158,15 @@ poetry install --only main ``` {{% /note %}} +{{% note %}} +If you only want to install the project root, and no other dependencies, you can use +the `--only-root` option. + +```bash +poetry install --only-root +``` +{{% /note %}} + ### Removing dependencies from a group The [`remove`]({{< relref "cli#remove" >}}) command supports a `--group` option From 5ec5cc486effd13f667a75688f551eae94ce1d4f Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 08:15:49 -0700 Subject: [PATCH 03/10] Additional documentation in cli.md --- docs/cli.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/cli.md b/docs/cli.md index 98eac1bcfac..cf74fe05be4 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -167,6 +167,12 @@ It's also possible to only install specific dependency groups by using the `only poetry install --only test,docs ``` +To only install the project itself with no dependencies, use the `only-root` flag. + +```bash +poetry install --only-root +``` + See [Dependency groups]({{< relref "managing-dependencies#dependency-groups" >}}) for more information about dependency groups. From 2f565255111d03cf960b881ac0556507afdfcfbe Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 08:21:23 -0700 Subject: [PATCH 04/10] Minor cleanup. --- src/poetry/console/commands/group_command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/poetry/console/commands/group_command.py b/src/poetry/console/commands/group_command.py index 7ca21b4771e..0af8491dd00 100644 --- a/src/poetry/console/commands/group_command.py +++ b/src/poetry/console/commands/group_command.py @@ -96,7 +96,7 @@ def activated_groups(self) -> set[str]: if self.option("only-root"): return {} - for key in {"with", "without", "only"}: + for key in group_keys: groups[key] = { group.strip() for groups in self.option(key, "") From 7b7c91d885fabe6ce66190928589a64e1ea07a86 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 08:24:46 -0700 Subject: [PATCH 05/10] Fix typing issue. --- src/poetry/console/commands/group_command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/poetry/console/commands/group_command.py b/src/poetry/console/commands/group_command.py index 0af8491dd00..208daa4c596 100644 --- a/src/poetry/console/commands/group_command.py +++ b/src/poetry/console/commands/group_command.py @@ -94,7 +94,7 @@ def activated_groups(self) -> set[str]: ) if self.option("only-root"): - return {} + return set() for key in group_keys: groups[key] = { From 8c44e60e48dc17fd17b953c1600a181ca08eb99d Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 09:06:55 -0700 Subject: [PATCH 06/10] Add `--only-root` to `poetry install` options in cli.md --- docs/cli.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/cli.md b/docs/cli.md index cf74fe05be4..c8e6db69808 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -226,6 +226,7 @@ option is used. * `--without`: The dependency groups to ignore. * `--with`: The optional dependency groups to include. * `--only`: The only dependency groups to include. +* `--only-root`: Install only the root project, exclude all dependencies. * `--default`: Only include the main dependencies. (**Deprecated**) * `--sync`: Synchronize the environment with the locked packages and the specified groups. * `--no-root`: Do not install the root package (your project). From 375ae11ea576752c5abcd53105a00ac316b52e57 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 09:44:17 -0700 Subject: [PATCH 07/10] Simplify test. --- tests/console/commands/test_show.py | 74 +---------------------------- 1 file changed, 1 insertion(+), 73 deletions(-) diff --git a/tests/console/commands/test_show.py b/tests/console/commands/test_show.py index 32a27e8e74c..6c7029d9e7a 100644 --- a/tests/console/commands/test_show.py +++ b/tests/console/commands/test_show.py @@ -254,6 +254,7 @@ def _configure_project_with_groups(poetry: Poetry, installed: Repository) -> Non pytest 3.7.3 Pytest package """, ), + ("--only-root", ""), ], ) def test_show_basic_with_group_options( @@ -1451,79 +1452,6 @@ def test_show_non_dev_with_basic_installed_packages( assert tester.io.fetch_output() == expected -def test_show_with_group_only_root( - tester: CommandTester, poetry: Poetry, installed: Repository -): - poetry.package.add_dependency(Factory.create_dependency("cachy", "^0.1.0")) - poetry.package.add_dependency(Factory.create_dependency("pendulum", "^2.0.0")) - poetry.package.add_dependency( - Factory.create_dependency("pytest", "*", groups=["dev"]) - ) - - cachy_010 = get_package("cachy", "0.1.0") - cachy_010.description = "Cachy package" - - pendulum_200 = get_package("pendulum", "2.0.0") - pendulum_200.description = "Pendulum package" - - pytest_373 = get_package("pytest", "3.7.3") - pytest_373.description = "Pytest package" - pytest_373.category = "dev" - - installed.add_package(cachy_010) - installed.add_package(pendulum_200) - installed.add_package(pytest_373) - - poetry.locker.mock_lock_data( - { - "package": [ - { - "name": "cachy", - "version": "0.1.0", - "description": "Cachy package", - "category": "main", - "optional": False, - "platform": "*", - "python-versions": "*", - "checksum": [], - }, - { - "name": "pendulum", - "version": "2.0.0", - "description": "Pendulum package", - "category": "main", - "optional": False, - "platform": "*", - "python-versions": "*", - "checksum": [], - }, - { - "name": "pytest", - "version": "3.7.3", - "description": "Pytest package", - "category": "dev", - "optional": False, - "platform": "*", - "python-versions": "*", - "checksum": [], - }, - ], - "metadata": { - "python-versions": "*", - "platform": "*", - "content-hash": "123456789", - "hashes": {"cachy": [], "pendulum": [], "pytest": []}, - }, - } - ) - - tester.execute("--only-root") - - expected = "" - - assert tester.io.fetch_output() == expected - - def test_show_with_group_only( tester: CommandTester, poetry: Poetry, installed: Repository ): From b89e16734aefdd7e59e276db25979ec8ccac2d24 Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 12:33:21 -0700 Subject: [PATCH 08/10] Migrate to use only in 'poetry install', add error for case of '--no-root --root-only'. --- src/poetry/console/commands/group_command.py | 21 +------------ src/poetry/console/commands/install.py | 32 ++++++++++++++++++++ tests/console/commands/test_install.py | 8 ++++- tests/console/commands/test_show.py | 1 - 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/poetry/console/commands/group_command.py b/src/poetry/console/commands/group_command.py index 208daa4c596..e872721667b 100644 --- a/src/poetry/console/commands/group_command.py +++ b/src/poetry/console/commands/group_command.py @@ -43,13 +43,6 @@ def _group_dependency_options() -> list[Option]: flag=False, multiple=True, ), - option( - "only-root", - None, - "Exclude all dependencies.", - flag=True, - multiple=False, - ), ] @property @@ -84,19 +77,7 @@ def default_groups(self) -> set[str]: def activated_groups(self) -> set[str]: groups = {} - group_keys = {"with", "without", "only"} - if self.option("only-root") and any([self.option(key) for key in group_keys]): - self.line_error( - "The `--with`," - " `--without` and" - " `--only` options are ignored when used" - " along with the `--only-root` option." - ) - - if self.option("only-root"): - return set() - - for key in group_keys: + for key in {"with", "without", "only"}: groups[key] = { group.strip() for groups in self.option(key, "") diff --git a/src/poetry/console/commands/install.py b/src/poetry/console/commands/install.py index af71a12c180..f8caf0b169d 100644 --- a/src/poetry/console/commands/install.py +++ b/src/poetry/console/commands/install.py @@ -47,6 +47,13 @@ class InstallCommand(InstallerCommand): multiple=True, ), option("all-extras", None, "Install all extra dependencies."), + option( + "only-root", + None, + "Exclude all dependencies.", + flag=True, + multiple=False, + ), ] help = """The install command reads the poetry.lock file from @@ -65,6 +72,13 @@ class InstallCommand(InstallerCommand): _loggers = ["poetry.repositories.pypi_repository", "poetry.inspection.info"] + @property + def activated_groups(self) -> set[str]: + if self.option("only-root"): + return set() + else: + return super().activated_groups + def handle(self) -> int: from poetry.core.masonry.utils.module import ModuleOrPackageNotFound @@ -82,6 +96,24 @@ def handle(self) -> int: ) return 1 + if self.option("only-root") and any( + [self.option(key) for key in {"with", "without", "only"}] + ): + self.line_error( + "The `--with`," + " `--without` and" + " `--only` options are ignored when used" + " along with the `--only-root`" + " option." + ) + + if self.option("only-root") and self.option("no-root"): + self.line_error( + "You cannot specify `--no-root`" + " when using `--only-root`." + ) + return 1 + if self.option("all-extras"): extras = list(self.poetry.package.extras.keys()) else: diff --git a/tests/console/commands/test_install.py b/tests/console/commands/test_install.py index c6c33f66f88..99182c61a90 100644 --- a/tests/console/commands/test_install.py +++ b/tests/console/commands/test_install.py @@ -112,7 +112,13 @@ def test_group_options_are_passed_to_the_installer( if not with_root: options = f"--no-root {options}" - tester.execute(options) + status_code = tester.execute(options) + + if options == "--no-root --only-root": + assert status_code == 1 + return + else: + assert status_code == 0 package_groups = set(tester.command.poetry.package._dependency_groups.keys()) installer_groups = set(tester.command.installer._groups) diff --git a/tests/console/commands/test_show.py b/tests/console/commands/test_show.py index 6c7029d9e7a..2a92347af65 100644 --- a/tests/console/commands/test_show.py +++ b/tests/console/commands/test_show.py @@ -254,7 +254,6 @@ def _configure_project_with_groups(poetry: Poetry, installed: Repository) -> Non pytest 3.7.3 Pytest package """, ), - ("--only-root", ""), ], ) def test_show_basic_with_group_options( From 3568484c3629dd071792202f71bb67c156dbe29f Mon Sep 17 00:00:00 2001 From: Chad Crawford Date: Mon, 6 Jun 2022 13:15:13 -0700 Subject: [PATCH 09/10] Performance fix, swap warning to error. --- src/poetry/console/commands/install.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/poetry/console/commands/install.py b/src/poetry/console/commands/install.py index f8caf0b169d..c65a3225202 100644 --- a/src/poetry/console/commands/install.py +++ b/src/poetry/console/commands/install.py @@ -97,15 +97,16 @@ def handle(self) -> int: return 1 if self.option("only-root") and any( - [self.option(key) for key in {"with", "without", "only"}] + self.option(key) for key in {"with", "without", "only"} ): self.line_error( - "The `--with`," + "The `--with`," " `--without` and" - " `--only` options are ignored when used" - " along with the `--only-root`" - " option." + " `--only` options cannot be used with" + " the `--only-root`" + " option." ) + return 1 if self.option("only-root") and self.option("no-root"): self.line_error( From cbd1d480957def8e69980c3c297a06415edce0ab Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Tue, 14 Jun 2022 21:21:57 -0600 Subject: [PATCH 10/10] Apply suggestions from code review --- docs/cli.md | 2 +- docs/managing-dependencies.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index c8e6db69808..4c29035c80b 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -167,7 +167,7 @@ It's also possible to only install specific dependency groups by using the `only poetry install --only test,docs ``` -To only install the project itself with no dependencies, use the `only-root` flag. +To only install the project itself with no dependencies, use the `--only-root` flag. ```bash poetry install --only-root diff --git a/docs/managing-dependencies.md b/docs/managing-dependencies.md index b8b5f8636cf..33d44cbcd94 100644 --- a/docs/managing-dependencies.md +++ b/docs/managing-dependencies.md @@ -159,7 +159,7 @@ poetry install --only main {{% /note %}} {{% note %}} -If you only want to install the project root, and no other dependencies, you can use +If you want to install the project root, and no other dependencies, you can use the `--only-root` option. ```bash