Skip to content

Commit efe6f57

Browse files
authored
Add support for dependency groups (#26)
1 parent 71789cc commit efe6f57

File tree

11 files changed

+180
-37
lines changed

11 files changed

+180
-37
lines changed

poetry.lock

Lines changed: 47 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ include = [
1616

1717
[tool.poetry.dependencies]
1818
python = "^3.7"
19-
poetry = "^1.2.0rc1"
19+
poetry = "^1.2.0"
2020

2121
[tool.poetry.dev-dependencies]
2222
pre-commit = "^2.6"

src/poetry_plugin_bundle/bundlers/venv_bundler.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self) -> None:
2525
self._path: Path
2626
self._executable: str | None = None
2727
self._remove: bool = False
28+
self._activated_groups: set[str] | None = None
2829

2930
def set_path(self, path: Path) -> VenvBundler:
3031
self._path = path
@@ -36,6 +37,11 @@ def set_executable(self, executable: str) -> VenvBundler:
3637

3738
return self
3839

40+
def set_activated_groups(self, activated_groups: set[str]) -> VenvBundler:
41+
self._activated_groups = activated_groups
42+
43+
return self
44+
3945
def set_remove(self, remove: bool = True) -> VenvBundler:
4046
self._remove = remove
4147

@@ -111,10 +117,7 @@ def bundle(self, poetry: Poetry, io: IO) -> bool:
111117

112118
env = VirtualEnv(self._path)
113119

114-
self._write(
115-
io,
116-
f"{message}: <info>Installing dependencies</info>",
117-
)
120+
self._write(io, f"{message}: <info>Installing dependencies</info>")
118121

119122
installer = Installer(
120123
NullIO() if not io.is_debug() else io,
@@ -124,6 +127,8 @@ def bundle(self, poetry: Poetry, io: IO) -> bool:
124127
poetry.pool,
125128
poetry.config,
126129
)
130+
if self._activated_groups is not None:
131+
installer.only_groups(self._activated_groups)
127132
installer.requires_synchronization()
128133
installer.use_executor(poetry.config.get("experimental.new-installer", False))
129134

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
from typing import TYPE_CHECKING
44

5-
from poetry.console.commands.command import Command
5+
from poetry.console.commands.group_command import GroupCommand
66

77

88
if TYPE_CHECKING:
99
from poetry_plugin_bundle.bundlers.bundler import Bundler
1010
from poetry_plugin_bundle.bundlers.bundler_manager import BundlerManager
1111

1212

13-
class BundleCommand(Command):
13+
class BundleCommand(GroupCommand):
1414
"""
1515
Base class for all bundle commands.
1616
"""

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ class BundleVenvCommand(BundleCommand):
1818
description = "Bundle the current project into a virtual environment"
1919

2020
arguments = [
21-
argument("path", "The path to the virtual environment to bundle into."),
21+
argument("path", "The path to the virtual environment to bundle into.")
2222
]
2323

2424
options = [
25+
*BundleCommand._group_dependency_options(),
2526
option(
2627
"python",
2728
"p",
@@ -44,3 +45,4 @@ def configure_bundler(self, bundler: VenvBundler) -> None: # type: ignore[overr
4445
bundler.set_path(Path(self.argument("path")))
4546
bundler.set_executable(self.option("python"))
4647
bundler.set_remove(self.option("clear"))
48+
bundler.set_activated_groups(self.activated_groups)

tests/bundlers/test_venv_bundler.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from cleo.io.buffered_io import BufferedIO
1313
from poetry.core.packages.package import Package
1414
from poetry.factory import Factory
15+
from poetry.puzzle.exceptions import SolverProblemError
1516
from poetry.repositories.pool import Pool
1617
from poetry.repositories.repository import Repository
1718

@@ -221,3 +222,49 @@ def test_bundler_should_display_a_warning_for_projects_with_no_module(
221222
• The root package was not installed because no matching module or package was found.
222223
""" # noqa: E501
223224
assert expected == io.fetch_output()
225+
226+
227+
def test_bundler_can_filter_dependency_groups(
228+
io: BufferedIO, tmpdir: str, poetry: Poetry, mocker: MockerFixture, config: Config
229+
) -> None:
230+
poetry = Factory().create_poetry(
231+
Path(__file__).parent.parent / "fixtures" / "simple_project_with_dev_dep"
232+
)
233+
poetry.set_config(config)
234+
235+
# foo is in the main dependency group
236+
# bar is a dev dependency
237+
# add a repository for foo but not bar
238+
pool = Pool()
239+
repository = Repository("repo")
240+
repository.add_package(Package("foo", "1.0.0"))
241+
pool.add_repository(repository)
242+
poetry.set_pool(pool)
243+
244+
mocker.patch("poetry.installation.executor.Executor._execute_operation")
245+
246+
bundler = VenvBundler()
247+
bundler.set_path(Path(tmpdir))
248+
bundler.set_remove(True)
249+
250+
# This should fail because there is not repo for bar
251+
with pytest.raises(SolverProblemError):
252+
assert not bundler.bundle(poetry, io)
253+
254+
bundler.set_activated_groups({"main"})
255+
io.clear_output()
256+
257+
# This succeeds because the dev dependency group is filtered out
258+
assert bundler.bundle(poetry, io)
259+
260+
path = tmpdir
261+
python_version = ".".join(str(v) for v in sys.version_info[:3])
262+
expected = f"""\
263+
• Bundling simple-project (1.2.3) into {path}
264+
• Bundling simple-project (1.2.3) into {path}: Removing existing virtual environment
265+
• Bundling simple-project (1.2.3) into {path}: Creating a virtual environment using Python {python_version}
266+
• Bundling simple-project (1.2.3) into {path}: Installing dependencies
267+
• Bundling simple-project (1.2.3) into {path}: Installing simple-project (1.2.3)
268+
• Bundled simple-project (1.2.3) into {path}
269+
""" # noqa: E501
270+
assert expected == io.fetch_output()

0 commit comments

Comments
 (0)