forked from platformio/platformio-core
Improve pio pkg exec
test
This commit is contained in:
@ -27,7 +27,8 @@ from platformio.proc import get_pythonexe_path
|
|||||||
@click.option("-p", "--package", metavar="<pkg>[@<version>]")
|
@click.option("-p", "--package", metavar="<pkg>[@<version>]")
|
||||||
@click.option("-c", "--call", metavar="<cmd> [args...]")
|
@click.option("-c", "--call", metavar="<cmd> [args...]")
|
||||||
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
|
@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:
|
if not call and not args:
|
||||||
raise click.BadArgumentUsage("Please provide command name")
|
raise click.BadArgumentUsage("Please provide command name")
|
||||||
pkg = None
|
pkg = None
|
||||||
@ -52,12 +53,16 @@ def package_exec_cmd(package, call, args):
|
|||||||
inject_pkg_to_environ(pkg)
|
inject_pkg_to_environ(pkg)
|
||||||
os.environ["PIO_PYTHON_EXE"] = get_pythonexe_path()
|
os.environ["PIO_PYTHON_EXE"] = get_pythonexe_path()
|
||||||
result = None
|
result = None
|
||||||
|
force_click_stream = (obj or {}).get("force_click_stream")
|
||||||
try:
|
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
|
result = subprocess.run( # pylint: disable=subprocess-run-check
|
||||||
call or args,
|
call or args, **run_options
|
||||||
shell=call is not None,
|
|
||||||
env=os.environ,
|
|
||||||
)
|
)
|
||||||
|
if force_click_stream:
|
||||||
|
click.echo(result.stdout.decode().strip(), err=result.returncode != 0)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise UserSideException(exc)
|
raise UserSideException(exc)
|
||||||
|
|
||||||
|
13
tests/commands/pkg/__init__.py
Normal file
13
tests/commands/pkg/__init__.py
Normal 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.
|
@ -12,12 +12,14 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from platformio.package.commands.exec import package_exec_cmd
|
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(
|
result = clirunner.invoke(
|
||||||
package_exec_cmd,
|
package_exec_cmd,
|
||||||
["--", "openocd"],
|
["--", "openocd"],
|
||||||
@ -28,22 +30,31 @@ def test_exec(clirunner, validate_cliresult, strip_ansi):
|
|||||||
):
|
):
|
||||||
validate_cliresult(result)
|
validate_cliresult(result)
|
||||||
|
|
||||||
|
|
||||||
|
def test_pkg_specified(clirunner, validate_cliresult, isolated_pio_core, strip_ansi):
|
||||||
# with install
|
# with install
|
||||||
result = clirunner.invoke(
|
result = clirunner.invoke(
|
||||||
package_exec_cmd,
|
package_exec_cmd,
|
||||||
["-p", "platformio/tool-openocd", "--", "openocd", "--version"],
|
["-p", "platformio/tool-openocd", "--", "openocd", "--version"],
|
||||||
|
obj=dict(force_click_stream=True),
|
||||||
)
|
)
|
||||||
validate_cliresult(result)
|
validate_cliresult(result)
|
||||||
output = strip_ansi(result.output)
|
output = strip_ansi(result.output)
|
||||||
assert "Tool Manager: Installing platformio/tool-openocd" in 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
|
# unrecognized option
|
||||||
result = clirunner.invoke(
|
result = clirunner.invoke(
|
||||||
package_exec_cmd,
|
package_exec_cmd,
|
||||||
["--", "openocd", "--test-unrecognized"],
|
["--", "openocd", "--test-unrecognized"],
|
||||||
|
obj=dict(force_click_stream=True),
|
||||||
)
|
)
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
AssertionError,
|
AssertionError,
|
||||||
match=("Using tool-openocd"),
|
match=("openocd: unrecognized option"),
|
||||||
):
|
):
|
||||||
validate_cliresult(result)
|
validate_cliresult(result)
|
Reference in New Issue
Block a user