diff --git a/src/poetry/console/commands/install.py b/src/poetry/console/commands/install.py index 9895745b48b..ad5b574ab37 100644 --- a/src/poetry/console/commands/install.py +++ b/src/poetry/console/commands/install.py @@ -93,6 +93,19 @@ def activated_groups(self) -> set[str]: def _alternative_sync_command(self) -> str: return "poetry sync" + @property + def _with_synchronization(self) -> bool: + with_synchronization = self.option("sync") + if with_synchronization: + self.line_error( + "The `--sync` option is" + " deprecated and slated for removal in the next minor release" + " after June 2025, use the" + f" `{self._alternative_sync_command}`" + " command instead." + ) + return bool(with_synchronization) + def handle(self) -> int: from poetry.core.masonry.utils.module import ModuleOrPackageNotFoundError @@ -150,20 +163,10 @@ def handle(self) -> int: self.installer.extras(extras) - with_synchronization = self.option("sync") - if with_synchronization: - self.line_error( - "The `--sync` option is" - " deprecated and slated for removal in the next minor release" - " after June 2025, use the" - f" `{self._alternative_sync_command}`" - " command instead." - ) - self.installer.only_groups(self.activated_groups) self.installer.skip_directory(self.option("no-directory")) self.installer.dry_run(self.option("dry-run")) - self.installer.requires_synchronization(with_synchronization) + self.installer.requires_synchronization(self._with_synchronization) self.installer.executor.enable_bytecode_compilation(self.option("compile")) self.installer.verbose(self.io.is_verbose()) diff --git a/src/poetry/console/commands/self/sync.py b/src/poetry/console/commands/self/sync.py index 0af576c7b80..05fc0c25a3b 100644 --- a/src/poetry/console/commands/self/sync.py +++ b/src/poetry/console/commands/self/sync.py @@ -29,3 +29,7 @@ class SelfSyncCommand(SelfInstallCommand): You can add more packages using the self add command and remove them using \ the self remove command. """ + + @property + def _with_synchronization(self) -> bool: + return True diff --git a/src/poetry/console/commands/sync.py b/src/poetry/console/commands/sync.py index 29d6c1be873..4723483f725 100644 --- a/src/poetry/console/commands/sync.py +++ b/src/poetry/console/commands/sync.py @@ -34,3 +34,7 @@ class SyncCommand(InstallCommand): If you want to use Poetry only for dependency management but not for packaging, you can set the "package-mode" to false in your pyproject.toml file. """ + + @property + def _with_synchronization(self) -> bool: + return True diff --git a/tests/console/commands/self/test_install.py b/tests/console/commands/self/test_install.py index a5e15e41ed6..0cd9663a4f9 100644 --- a/tests/console/commands/self/test_install.py +++ b/tests/console/commands/self/test_install.py @@ -56,14 +56,9 @@ def test_self_install( tester.execute() - expected_output = """\ -Updating dependencies -Resolving dependencies... - -Writing lock file -""" - - assert tester.io.fetch_output() == expected_output + output = tester.io.fetch_output() + assert output.startswith("Updating dependencies") + assert output.endswith("Writing lock file\n") assert tester.io.fetch_error() == "" diff --git a/tests/console/commands/self/test_sync.py b/tests/console/commands/self/test_sync.py index 6fdc27fc764..d1c351c74d3 100644 --- a/tests/console/commands/self/test_sync.py +++ b/tests/console/commands/self/test_sync.py @@ -6,6 +6,8 @@ from cleo.exceptions import CleoNoSuchOptionError +from poetry.console.commands.self.sync import SelfSyncCommand + # import all tests from the self install command # and run them for sync by overriding the command fixture from tests.console.commands.self.test_install import * # noqa: F403 @@ -13,6 +15,7 @@ if TYPE_CHECKING: from cleo.testers.command_tester import CommandTester + from pytest_mock import MockerFixture @pytest.fixture # type: ignore[no-redef] @@ -28,3 +31,15 @@ def test_sync_deprecation() -> None: def test_sync_option_not_available(tester: CommandTester) -> None: with pytest.raises(CleoNoSuchOptionError): tester.execute("--sync") + + +def test_synced_installer(tester: CommandTester, mocker: MockerFixture) -> None: + assert isinstance(tester.command, SelfSyncCommand) + mock = mocker.patch( + "poetry.console.commands.install.InstallCommand.installer", + new_callable=mocker.PropertyMock, + ) + + tester.execute() + + mock.return_value.requires_synchronization.assert_called_with(True) diff --git a/tests/console/commands/test_sync.py b/tests/console/commands/test_sync.py index af75afd86f3..89d7e8c7760 100644 --- a/tests/console/commands/test_sync.py +++ b/tests/console/commands/test_sync.py @@ -6,6 +6,8 @@ from cleo.exceptions import CleoNoSuchOptionError +from poetry.console.commands.sync import SyncCommand + # import all tests from the install command # and run them for sync by overriding the command fixture from tests.console.commands.test_install import * # noqa: F403 @@ -13,6 +15,7 @@ if TYPE_CHECKING: from cleo.testers.command_tester import CommandTester + from pytest_mock import MockerFixture @pytest.fixture # type: ignore[no-redef] @@ -28,3 +31,15 @@ def test_sync_option_is_passed_to_the_installer() -> None: def test_sync_option_not_available(tester: CommandTester) -> None: with pytest.raises(CleoNoSuchOptionError): tester.execute("--sync") + + +def test_synced_installer(tester: CommandTester, mocker: MockerFixture) -> None: + assert isinstance(tester.command, SyncCommand) + mock = mocker.patch( + "poetry.console.commands.install.InstallCommand.installer", + new_callable=mocker.PropertyMock, + ) + + tester.execute() + + mock.return_value.requires_synchronization.assert_called_with(True)