Skip to content

Commit 8d8c7da

Browse files
feat: compile flag for installer (#88)
1 parent de20d90 commit 8d8c7da

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

src/poetry_plugin_bundle/bundlers/venv_bundler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self) -> None:
2626
self._executable: str | None = None
2727
self._remove: bool = False
2828
self._activated_groups: set[str] | None = None
29+
self._compile: bool = False
2930

3031
def set_path(self, path: Path) -> VenvBundler:
3132
self._path = path
@@ -47,6 +48,11 @@ def set_remove(self, remove: bool = True) -> VenvBundler:
4748

4849
return self
4950

51+
def set_compile(self, compile: bool = False) -> VenvBundler:
52+
self._compile = compile
53+
54+
return self
55+
5056
def bundle(self, poetry: Poetry, io: IO) -> bool:
5157
from pathlib import Path
5258
from tempfile import TemporaryDirectory
@@ -141,6 +147,8 @@ def locked_repository(self) -> LockfileRepository:
141147
installer.only_groups(self._activated_groups)
142148
installer.requires_synchronization()
143149

150+
installer.executor.enable_bytecode_compilation(self._compile)
151+
144152
return_code = installer.run()
145153
if return_code:
146154
self._write(

src/poetry_plugin_bundle/console/commands/bundle/venv.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class BundleVenvCommand(BundleCommand):
3737
"Clear the existing virtual environment if it exists. ",
3838
flag=True,
3939
),
40+
option(
41+
"compile",
42+
None,
43+
"Compile Python source files to bytecode."
44+
" (This option has no effect if modern-installation is disabled"
45+
" because the old installer always compiles.)",
46+
flag=True,
47+
),
4048
]
4149

4250
bundler_name = "venv"
@@ -45,4 +53,5 @@ def configure_bundler(self, bundler: VenvBundler) -> None: # type: ignore[overr
4553
bundler.set_path(Path(self.argument("path")))
4654
bundler.set_executable(self.option("python"))
4755
bundler.set_remove(self.option("clear"))
56+
bundler.set_compile(self.option("compile"))
4857
bundler.set_activated_groups(self.activated_groups)

tests/bundlers/test_venv_bundler.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,43 @@ def test_bundler_can_filter_dependency_groups(
271271
assert expected == io.fetch_output()
272272

273273

274+
@pytest.mark.parametrize("compile", [True, False])
275+
def test_bundler_passes_compile_flag(
276+
io: BufferedIO,
277+
tmp_venv: VirtualEnv,
278+
poetry: Poetry,
279+
mocker: MockerFixture,
280+
compile: bool,
281+
) -> None:
282+
mocker.patch("poetry.installation.executor.Executor._execute_operation")
283+
284+
bundler = VenvBundler()
285+
bundler.set_path(tmp_venv.path)
286+
bundler.set_remove(True)
287+
bundler.set_compile(compile)
288+
289+
# bundle passes the flag from set_compile to enable_bytecode_compilation method
290+
mocker = mocker.patch(
291+
"poetry.installation.executor.Executor.enable_bytecode_compilation"
292+
)
293+
294+
assert bundler.bundle(poetry, io)
295+
296+
mocker.assert_called_once_with(compile)
297+
298+
path = str(tmp_venv.path)
299+
python_version = ".".join(str(v) for v in sys.version_info[:3])
300+
expected = f"""\
301+
• Bundling simple-project (1.2.3) into {path}
302+
• Bundling simple-project (1.2.3) into {path}: Removing existing virtual environment
303+
• Bundling simple-project (1.2.3) into {path}: Creating a virtual environment using Python {python_version}
304+
• Bundling simple-project (1.2.3) into {path}: Installing dependencies
305+
• Bundling simple-project (1.2.3) into {path}: Installing simple-project (1.2.3)
306+
• Bundled simple-project (1.2.3) into {path}
307+
"""
308+
assert expected == io.fetch_output()
309+
310+
274311
def test_bundler_editable_deps(
275312
io: BufferedIO, tmpdir: str, poetry: Poetry, mocker: MockerFixture, config: Config
276313
) -> None:

tests/console/commands/bundle/test_venv.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ def test_venv_calls_venv_bundler(
1818
) -> None:
1919
mock = mocker.patch(
2020
"poetry_plugin_bundle.bundlers.venv_bundler.VenvBundler.bundle",
21-
side_effect=[True, False, False, False],
21+
side_effect=[True, False, False, False, False],
2222
)
2323
set_path = mocker.spy(VenvBundler, "set_path")
2424
set_executable = mocker.spy(VenvBundler, "set_executable")
2525
set_remove = mocker.spy(VenvBundler, "set_remove")
2626
set_activated_groups = mocker.spy(VenvBundler, "set_activated_groups")
27+
set_compile = mocker.spy(VenvBundler, "set_compile")
2728

2829
app_tester.application.catch_exceptions(False)
2930
assert app_tester.execute("bundle venv /foo") == 0
@@ -33,36 +34,50 @@ def test_venv_calls_venv_bundler(
3334
)
3435
assert app_tester.execute("bundle venv /foo --only dev") == 1
3536
assert app_tester.execute("bundle venv /foo --without main --with dev") == 1
37+
assert app_tester.execute("bundle venv /foo --compile") == 1
3638

3739
assert isinstance(app_tester.application, Application)
3840
assert [
3941
mocker.call(app_tester.application.poetry, mocker.ANY),
4042
mocker.call(app_tester.application.poetry, mocker.ANY),
4143
mocker.call(app_tester.application.poetry, mocker.ANY),
4244
mocker.call(app_tester.application.poetry, mocker.ANY),
45+
mocker.call(app_tester.application.poetry, mocker.ANY),
4346
] == mock.call_args_list
4447

4548
assert set_path.call_args_list == [
4649
mocker.call(mocker.ANY, Path("/foo")),
4750
mocker.call(mocker.ANY, Path("/foo")),
4851
mocker.call(mocker.ANY, Path("/foo")),
4952
mocker.call(mocker.ANY, Path("/foo")),
53+
mocker.call(mocker.ANY, Path("/foo")),
5054
]
5155
assert set_executable.call_args_list == [
5256
mocker.call(mocker.ANY, None),
5357
mocker.call(mocker.ANY, "python3.8"),
5458
mocker.call(mocker.ANY, None),
5559
mocker.call(mocker.ANY, None),
60+
mocker.call(mocker.ANY, None),
5661
]
5762
assert set_remove.call_args_list == [
5863
mocker.call(mocker.ANY, False),
5964
mocker.call(mocker.ANY, True),
6065
mocker.call(mocker.ANY, False),
6166
mocker.call(mocker.ANY, False),
67+
mocker.call(mocker.ANY, False),
6268
]
6369
assert set_activated_groups.call_args_list == [
6470
mocker.call(mocker.ANY, {"main"}),
6571
mocker.call(mocker.ANY, {"main", "dev"}),
6672
mocker.call(mocker.ANY, {"dev"}),
6773
mocker.call(mocker.ANY, {"dev"}),
74+
mocker.call(mocker.ANY, {"main"}),
75+
]
76+
77+
assert set_compile.call_args_list == [
78+
mocker.call(mocker.ANY, False),
79+
mocker.call(mocker.ANY, False),
80+
mocker.call(mocker.ANY, False),
81+
mocker.call(mocker.ANY, False),
82+
mocker.call(mocker.ANY, True),
6883
]

0 commit comments

Comments
 (0)