Add test for pio pkg exec command

This commit is contained in:
Ivan Kravets
2022-02-18 21:03:12 +02:00
parent 28da2d245b
commit bcb3678055
2 changed files with 59 additions and 4 deletions

View File

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

View File

@ -0,0 +1,49 @@
# 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.
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)