diff --git a/platformio/package/commands/exec.py b/platformio/package/commands/exec.py index 06b6167c..eefce96b 100644 --- a/platformio/package/commands/exec.py +++ b/platformio/package/commands/exec.py @@ -18,7 +18,7 @@ import subprocess import click from platformio.compat import IS_MACOS, IS_WINDOWS -from platformio.exception import UserSideException +from platformio.exception import ReturnErrorCode, UserSideException from platformio.package.manager.tool import ToolPackageManager from platformio.proc import get_pythonexe_path @@ -46,18 +46,24 @@ def package_exec_cmd(package, call, args): click.echo( "Using %s package" - % click.style("%s@%s" % (pkg.metadata.name, pkg.metadata.version), fg="green") + % click.style("%s@%s" % (pkg.metadata.name, pkg.metadata.version), fg="cyan") ) inject_pkg_to_environ(pkg) os.environ["PIO_PYTHON_EXE"] = get_pythonexe_path() + result = None try: - subprocess.run( # pylint: disable=subprocess-run-check - call or args, shell=call is not None, env=os.environ + result = subprocess.run( # pylint: disable=subprocess-run-check + call or args, + shell=call is not None, + env=os.environ, ) except Exception as exc: raise UserSideException(exc) + if result and result.returncode != 0: + raise ReturnErrorCode(result.returncode) + def find_pkg_by_executable(executable): exes = [executable] diff --git a/tests/commands/test_pkg.py b/tests/commands/test_pkg.py new file mode 100644 index 00000000..c596c6c8 --- /dev/null +++ b/tests/commands/test_pkg.py @@ -0,0 +1,49 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +from platformio.package.commands.exec import package_exec_cmd + + +def test_exec(clirunner, validate_cliresult, strip_ansi): + result = clirunner.invoke( + package_exec_cmd, + ["--", "openocd"], + ) + with pytest.raises( + AssertionError, + match=("Could not find a package with 'openocd' executable file"), + ): + validate_cliresult(result) + + # with install + result = clirunner.invoke( + package_exec_cmd, + ["-p", "platformio/tool-openocd", "--", "openocd", "--version"], + ) + validate_cliresult(result) + output = strip_ansi(result.output) + assert "Tool Manager: Installing platformio/tool-openocd" in output + + # unrecognized option + result = clirunner.invoke( + package_exec_cmd, + ["--", "openocd", "--test-unrecognized"], + ) + with pytest.raises( + AssertionError, + match=("Using tool-openocd"), + ): + validate_cliresult(result)