From 4fef4ab4eaf6ce18ab9e836957a73533a1584863 Mon Sep 17 00:00:00 2001 From: Tobias Reiher Date: Tue, 12 Mar 2024 16:37:38 +0100 Subject: [PATCH] Add handling of errors during VS Code extension installation Ref. eng/recordflux/RecordFlux!1490 --- rflx/cli.py | 5 ++++- tests/unit/cli_test.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/rflx/cli.py b/rflx/cli.py index 8816d17e1..b58381e2a 100644 --- a/rflx/cli.py +++ b/rflx/cli.py @@ -683,7 +683,10 @@ def install(args: argparse.Namespace) -> None: elif args.ide is IDE.VSCODE: with importlib_resources.as_file(vscode_extension()) as extension: - subprocess.run(["code", "--install-extension", extension, "--force"], check=False) + try: + subprocess.run(["code", "--install-extension", extension, "--force"], check=True) + except (FileNotFoundError, subprocess.CalledProcessError) as e: + fail(f"installation of VS Code extension failed: {e}", Subsystem.CLI) else: assert_never(args.ide) # pragma: no cover diff --git a/tests/unit/cli_test.py b/tests/unit/cli_test.py index 0a912d15c..f2aab7be7 100644 --- a/tests/unit/cli_test.py +++ b/tests/unit/cli_test.py @@ -662,6 +662,18 @@ def test_install_vscode_extension(monkeypatch: pytest.MonkeyPatch, tmp_path: Pat assert run_called == [["code", "--install-extension", vscode_extension, "--force"]] + def run_mock(cmd: object, check: object) -> subprocess.CompletedProcess[object]: # noqa: ARG001 + raise FileNotFoundError("file not found") + + monkeypatch.setattr(subprocess, "run", run_mock) + + assert ( + str( + cli.main(["rflx", "install", "vscode"]), + ) + == "cli: error: installation of VS Code extension failed: file not found" + ) + def test_install_invalid() -> None: args = ["rflx", "install", "invalid"]