Skip to content

Commit c66c2af

Browse files
authored
fix lpm to return non-zero exit code if package installation fails (localstack#5038)
1 parent 5abe83b commit c66c2af

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

localstack/cli/lpm.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ def _do_install(pkg):
4141
console.print(f"[green]installed[/green] [bold]{pkg}[/bold]")
4242
except Exception as e:
4343
console.print(f"[red]error[/red] installing {pkg}: {e}")
44+
raise e
4445

4546

4647
@cli.command()
4748
@click.argument("package", nargs=-1, required=True)
4849
@click.option(
4950
"--parallel",
5051
type=int,
51-
default=0,
52+
default=1,
5253
required=False,
5354
help="how many installers to run in parallel processes",
5455
)
@@ -63,21 +64,15 @@ def install(package, parallel):
6364
if pkg not in installers:
6465
raise ClickException(f"unable to locate installer for package {pkg}")
6566

66-
if not parallel:
67-
# install one by one
68-
for pkg in package:
69-
console.print(f"installing [bold]{pkg}[/bold]")
70-
with console.status("installing..."):
71-
try:
72-
installers[pkg]()
73-
except Exception as e:
74-
console.print(f"[red]error[/red]: {e}")
75-
else:
67+
if parallel > 1:
7668
console.print(f"install {parallel} packages in parallel:")
77-
# collect installers and install in parallel:
7869

70+
# collect installers and install in parallel:
71+
try:
7972
with Pool(processes=parallel) as pool:
8073
pool.map(_do_install, package)
74+
except Exception:
75+
raise ClickException("one or more package installations failed.")
8176

8277

8378
@cli.command(name="list")

tests/unit/cli/test_lpm.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os.path
2+
from typing import List
23

34
import pytest
45
from click.testing import CliRunner
56

67
from localstack.cli.lpm import cli, console
8+
from localstack.services.install import CommunityInstallerRepository, Installer
79

810

911
@pytest.fixture
@@ -22,7 +24,27 @@ def test_list(runner, monkeypatch):
2224
def test_install_with_non_existing_package_fails(runner):
2325
result = runner.invoke(cli, ["install", "elasticmq", "funny"])
2426
assert result.exit_code == 1
25-
assert "unable to locate installer for package funny"
27+
assert "unable to locate installer for package funny" in result.output
28+
29+
30+
def test_install_failure_returns_non_zero_exit_code(runner, monkeypatch):
31+
def failing_installer():
32+
raise Exception("failing installer")
33+
34+
def successful_installer():
35+
pass
36+
37+
def patched_get_installer(self) -> List[Installer]:
38+
return [
39+
("failing-installer", failing_installer),
40+
("successful-installer", successful_installer),
41+
]
42+
43+
monkeypatch.setattr(CommunityInstallerRepository, "get_installer", patched_get_installer)
44+
45+
result = runner.invoke(cli, ["install", "successful-installer", "failing-installer"])
46+
assert result.exit_code == 1
47+
assert "one or more package installations failed." in result.output
2648

2749

2850
def test_install_with_package(runner):

0 commit comments

Comments
 (0)