Skip to content

Commit 4df8d63

Browse files
trim21radoering
andauthored
fix: sync command should really add --sync option to installer (#9946)
Co-authored-by: Randy Döring <[email protected]>
1 parent 5d8f880 commit 4df8d63

File tree

6 files changed

+55
-19
lines changed

6 files changed

+55
-19
lines changed

src/poetry/console/commands/install.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ def activated_groups(self) -> set[str]:
9393
def _alternative_sync_command(self) -> str:
9494
return "poetry sync"
9595

96+
@property
97+
def _with_synchronization(self) -> bool:
98+
with_synchronization = self.option("sync")
99+
if with_synchronization:
100+
self.line_error(
101+
"<warning>The `<fg=yellow;options=bold>--sync</>` option is"
102+
" deprecated and slated for removal in the next minor release"
103+
" after June 2025, use the"
104+
f" `<fg=yellow;options=bold>{self._alternative_sync_command}</>`"
105+
" command instead.</warning>"
106+
)
107+
return bool(with_synchronization)
108+
96109
def handle(self) -> int:
97110
from poetry.core.masonry.utils.module import ModuleOrPackageNotFoundError
98111

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

151164
self.installer.extras(extras)
152165

153-
with_synchronization = self.option("sync")
154-
if with_synchronization:
155-
self.line_error(
156-
"<warning>The `<fg=yellow;options=bold>--sync</>` option is"
157-
" deprecated and slated for removal in the next minor release"
158-
" after June 2025, use the"
159-
f" `<fg=yellow;options=bold>{self._alternative_sync_command}</>`"
160-
" command instead.</warning>"
161-
)
162-
163166
self.installer.only_groups(self.activated_groups)
164167
self.installer.skip_directory(self.option("no-directory"))
165168
self.installer.dry_run(self.option("dry-run"))
166-
self.installer.requires_synchronization(with_synchronization)
169+
self.installer.requires_synchronization(self._with_synchronization)
167170
self.installer.executor.enable_bytecode_compilation(self.option("compile"))
168171
self.installer.verbose(self.io.is_verbose())
169172

src/poetry/console/commands/self/sync.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ class SelfSyncCommand(SelfInstallCommand):
2929
You can add more packages using the <c1>self add</c1> command and remove them using \
3030
the <c1>self remove</c1> command.
3131
"""
32+
33+
@property
34+
def _with_synchronization(self) -> bool:
35+
return True

src/poetry/console/commands/sync.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ class SyncCommand(InstallCommand):
3434
If you want to use Poetry only for dependency management but not for packaging,
3535
you can set the "package-mode" to false in your pyproject.toml file.
3636
"""
37+
38+
@property
39+
def _with_synchronization(self) -> bool:
40+
return True

tests/console/commands/self/test_install.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,9 @@ def test_self_install(
5656

5757
tester.execute()
5858

59-
expected_output = """\
60-
Updating dependencies
61-
Resolving dependencies...
62-
63-
Writing lock file
64-
"""
65-
66-
assert tester.io.fetch_output() == expected_output
59+
output = tester.io.fetch_output()
60+
assert output.startswith("Updating dependencies")
61+
assert output.endswith("Writing lock file\n")
6762
assert tester.io.fetch_error() == ""
6863

6964

tests/console/commands/self/test_sync.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
from cleo.exceptions import CleoNoSuchOptionError
88

9+
from poetry.console.commands.self.sync import SelfSyncCommand
10+
911
# import all tests from the self install command
1012
# and run them for sync by overriding the command fixture
1113
from tests.console.commands.self.test_install import * # noqa: F403
1214

1315

1416
if TYPE_CHECKING:
1517
from cleo.testers.command_tester import CommandTester
18+
from pytest_mock import MockerFixture
1619

1720

1821
@pytest.fixture # type: ignore[no-redef]
@@ -28,3 +31,15 @@ def test_sync_deprecation() -> None:
2831
def test_sync_option_not_available(tester: CommandTester) -> None:
2932
with pytest.raises(CleoNoSuchOptionError):
3033
tester.execute("--sync")
34+
35+
36+
def test_synced_installer(tester: CommandTester, mocker: MockerFixture) -> None:
37+
assert isinstance(tester.command, SelfSyncCommand)
38+
mock = mocker.patch(
39+
"poetry.console.commands.install.InstallCommand.installer",
40+
new_callable=mocker.PropertyMock,
41+
)
42+
43+
tester.execute()
44+
45+
mock.return_value.requires_synchronization.assert_called_with(True)

tests/console/commands/test_sync.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
from cleo.exceptions import CleoNoSuchOptionError
88

9+
from poetry.console.commands.sync import SyncCommand
10+
911
# import all tests from the install command
1012
# and run them for sync by overriding the command fixture
1113
from tests.console.commands.test_install import * # noqa: F403
1214

1315

1416
if TYPE_CHECKING:
1517
from cleo.testers.command_tester import CommandTester
18+
from pytest_mock import MockerFixture
1619

1720

1821
@pytest.fixture # type: ignore[no-redef]
@@ -28,3 +31,15 @@ def test_sync_option_is_passed_to_the_installer() -> None:
2831
def test_sync_option_not_available(tester: CommandTester) -> None:
2932
with pytest.raises(CleoNoSuchOptionError):
3033
tester.execute("--sync")
34+
35+
36+
def test_synced_installer(tester: CommandTester, mocker: MockerFixture) -> None:
37+
assert isinstance(tester.command, SyncCommand)
38+
mock = mocker.patch(
39+
"poetry.console.commands.install.InstallCommand.installer",
40+
new_callable=mocker.PropertyMock,
41+
)
42+
43+
tester.execute()
44+
45+
mock.return_value.requires_synchronization.assert_called_with(True)

0 commit comments

Comments
 (0)