Improve pio pkg exec test

This commit is contained in:
Ivan Kravets
2022-02-19 19:22:40 +02:00
parent bcb3678055
commit 33da2af31e
3 changed files with 35 additions and 6 deletions

View File

@ -27,7 +27,8 @@ from platformio.proc import get_pythonexe_path
@click.option("-p", "--package", metavar="<pkg>[@<version>]")
@click.option("-c", "--call", metavar="<cmd> [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)

View File

@ -0,0 +1,13 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# 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.

View File

@ -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)