Skip to content

Commit

Permalink
Add --all-extras flag for poetry install
Browse files Browse the repository at this point in the history
  • Loading branch information
kroeschl committed Apr 14, 2022
1 parent 819d6d4 commit 3799111
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class InstallCommand(InstallerCommand):
flag=False,
multiple=True,
),
option("all-extras", None, "Install all extra dependencies."),
]

help = """The <info>install</info> command reads the <comment>poetry.lock</> file from
Expand Down Expand Up @@ -79,12 +80,20 @@ def handle(self) -> int:
self.poetry.config.get("experimental.new-installer", False)
)

extras = []
for extra in self.option("extras"):
if " " in extra:
extras += [e.strip() for e in extra.split(" ")]
else:
extras.append(extra)
if self.option("extras") and self.option("all-extras"):
raise ValueError(
"You cannot specify explicit extras while installing all extras"
)

if self.option("all-extras"):
extras = list(self.poetry.package.extras.keys())
else:
extras = []
for extra in self.option("extras"):
if " " in extra:
extras += [e.strip() for e in extra.split(" ")]
else:
extras.append(extra)

self._installer.extras(extras)

Expand Down
44 changes: 44 additions & 0 deletions tests/console/commands/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
[tool.poetry.dependencies]
python = "~2.7 || ^3.4"
fizz = { version = "^1.0", optional = true }
buzz = { version = "^2.0", optional = true }
[tool.poetry.group.foo.dependencies]
foo = "^1.0"
Expand All @@ -47,6 +49,10 @@
[tool.poetry.group.bam.dependencies]
bam = "^1.4"
[tool.poetry.extras]
extras_a = [ "fizz" ]
extras_b = [ "buzz" ]
"""


Expand Down Expand Up @@ -130,3 +136,41 @@ def test_sync_option_is_passed_to_the_installer(
tester.execute("--sync")

assert tester.command.installer._requires_synchronization


def test_no_all_extras_doesnt_populate_installer(
tester: CommandTester, mocker: MockerFixture
):
"""
Not passing --all-extras means the installer doesn't see any extras.
"""
mocker.patch.object(tester.command.installer, "run", return_value=1)

tester.execute()

assert not tester.command.installer._extras


def test_all_extras_populates_installer(
tester: CommandTester, mocker: MockerFixture
):
"""
The --all-extras option results in extras passed to the installer.
"""
mocker.patch.object(tester.command.installer, "run", return_value=1)

tester.execute("--all-extras")

assert tester.command.installer._extras == ["extras_a", "extras_b"]


def test_extras_conlicts_all_extras(
tester: CommandTester, mocker: MockerFixture
):
"""
The --extras doesn't make sense with --all-extras.
"""
mocker.patch.object(tester.command.installer, "run", return_value=1)

with pytest.raises(ValueError):
tester.execute("--extras foo --all-extras")

0 comments on commit 3799111

Please sign in to comment.