Skip to content

Commit

Permalink
fix: sync command should really add --sync option to installer (#9946)
Browse files Browse the repository at this point in the history
Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com>
  • Loading branch information
trim21 and radoering authored Jan 6, 2025
1 parent 5d8f880 commit 4df8d63
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
25 changes: 14 additions & 11 deletions src/poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<warning>The `<fg=yellow;options=bold>--sync</>` option is"
" deprecated and slated for removal in the next minor release"
" after June 2025, use the"
f" `<fg=yellow;options=bold>{self._alternative_sync_command}</>`"
" command instead.</warning>"
)
return bool(with_synchronization)

def handle(self) -> int:
from poetry.core.masonry.utils.module import ModuleOrPackageNotFoundError

Expand Down Expand Up @@ -150,20 +163,10 @@ def handle(self) -> int:

self.installer.extras(extras)

with_synchronization = self.option("sync")
if with_synchronization:
self.line_error(
"<warning>The `<fg=yellow;options=bold>--sync</>` option is"
" deprecated and slated for removal in the next minor release"
" after June 2025, use the"
f" `<fg=yellow;options=bold>{self._alternative_sync_command}</>`"
" command instead.</warning>"
)

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())

Expand Down
4 changes: 4 additions & 0 deletions src/poetry/console/commands/self/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ class SelfSyncCommand(SelfInstallCommand):
You can add more packages using the <c1>self add</c1> command and remove them using \
the <c1>self remove</c1> command.
"""

@property
def _with_synchronization(self) -> bool:
return True
4 changes: 4 additions & 0 deletions src/poetry/console/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 3 additions & 8 deletions tests/console/commands/self/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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() == ""


Expand Down
15 changes: 15 additions & 0 deletions tests/console/commands/self/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

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


if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture


@pytest.fixture # type: ignore[no-redef]
Expand All @@ -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)
15 changes: 15 additions & 0 deletions tests/console/commands/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

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


if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture


@pytest.fixture # type: ignore[no-redef]
Expand All @@ -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)

0 comments on commit 4df8d63

Please sign in to comment.