From 33da2af31ec5b18081140de0ccb04176c993fb6b Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Sat, 19 Feb 2022 19:22:40 +0200 Subject: [PATCH] Improve `pio pkg exec` test --- platformio/package/commands/exec.py | 13 +++++++++---- tests/commands/pkg/__init__.py | 13 +++++++++++++ tests/commands/{test_pkg.py => pkg/test_exec.py} | 15 +++++++++++++-- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 tests/commands/pkg/__init__.py rename tests/commands/{test_pkg.py => pkg/test_exec.py} (75%) diff --git a/platformio/package/commands/exec.py b/platformio/package/commands/exec.py index eefce96b..f26b0d17 100644 --- a/platformio/package/commands/exec.py +++ b/platformio/package/commands/exec.py @@ -27,7 +27,8 @@ from platformio.proc import get_pythonexe_path @click.option("-p", "--package", metavar="[@]") @click.option("-c", "--call", metavar=" [args...]") @click.argument("args", nargs=-1, type=click.UNPROCESSED) -def package_exec_cmd(package, call, args): +@click.pass_obj +def package_exec_cmd(obj, package, call, args): if not call and not args: raise click.BadArgumentUsage("Please provide command name") pkg = None @@ -52,12 +53,16 @@ def package_exec_cmd(package, call, args): inject_pkg_to_environ(pkg) os.environ["PIO_PYTHON_EXE"] = get_pythonexe_path() result = None + force_click_stream = (obj or {}).get("force_click_stream") try: + run_options = dict(shell=call is not None, env=os.environ) + if force_click_stream: + run_options.update(stdout=subprocess.PIPE, stderr=subprocess.STDOUT) result = subprocess.run( # pylint: disable=subprocess-run-check - call or args, - shell=call is not None, - env=os.environ, + call or args, **run_options ) + if force_click_stream: + click.echo(result.stdout.decode().strip(), err=result.returncode != 0) except Exception as exc: raise UserSideException(exc) diff --git a/tests/commands/pkg/__init__.py b/tests/commands/pkg/__init__.py new file mode 100644 index 00000000..b0514903 --- /dev/null +++ b/tests/commands/pkg/__init__.py @@ -0,0 +1,13 @@ +# 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. diff --git a/tests/commands/test_pkg.py b/tests/commands/pkg/test_exec.py similarity index 75% rename from tests/commands/test_pkg.py rename to tests/commands/pkg/test_exec.py index c596c6c8..86b6c8fd 100644 --- a/tests/commands/test_pkg.py +++ b/tests/commands/pkg/test_exec.py @@ -12,12 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +# pylint: disable=unused-argument + import pytest from platformio.package.commands.exec import package_exec_cmd -def test_exec(clirunner, validate_cliresult, strip_ansi): +def test_pkg_not_installed(clirunner, validate_cliresult, isolated_pio_core): result = clirunner.invoke( package_exec_cmd, ["--", "openocd"], @@ -28,22 +30,31 @@ def test_exec(clirunner, validate_cliresult, strip_ansi): ): validate_cliresult(result) + +def test_pkg_specified(clirunner, validate_cliresult, isolated_pio_core, strip_ansi): # with install result = clirunner.invoke( package_exec_cmd, ["-p", "platformio/tool-openocd", "--", "openocd", "--version"], + obj=dict(force_click_stream=True), ) validate_cliresult(result) output = strip_ansi(result.output) assert "Tool Manager: Installing platformio/tool-openocd" in output + assert "Open On-Chip Debugger" in output + +def test_unrecognized_options( + clirunner, validate_cliresult, isolated_pio_core, strip_ansi +): # unrecognized option result = clirunner.invoke( package_exec_cmd, ["--", "openocd", "--test-unrecognized"], + obj=dict(force_click_stream=True), ) with pytest.raises( AssertionError, - match=("Using tool-openocd"), + match=("openocd: unrecognized option"), ): validate_cliresult(result)