2020-10-21 11:30:49 +00:00
|
|
|
# internal use only for CI
|
|
|
|
# some CI related util functions
|
|
|
|
#
|
2022-04-29 04:19:32 +00:00
|
|
|
# SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
|
2021-10-27 06:20:49 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2020-10-21 11:30:49 +00:00
|
|
|
#
|
2022-04-29 04:19:32 +00:00
|
|
|
|
2022-01-28 07:21:12 +00:00
|
|
|
import io
|
2020-10-21 11:30:49 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
2020-11-12 06:11:54 +00:00
|
|
|
import sys
|
2022-01-28 07:21:12 +00:00
|
|
|
from contextlib import redirect_stdout
|
2022-04-29 04:19:32 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import TYPE_CHECKING, Any, List, Set
|
2020-10-21 11:30:49 +00:00
|
|
|
|
2022-01-28 07:21:12 +00:00
|
|
|
if TYPE_CHECKING:
|
2022-04-29 04:19:32 +00:00
|
|
|
from _pytest.python import Function
|
|
|
|
|
2022-01-28 07:21:12 +00:00
|
|
|
|
|
|
|
IDF_PATH = os.path.abspath(
|
|
|
|
os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..'))
|
|
|
|
)
|
2020-10-21 11:30:49 +00:00
|
|
|
|
|
|
|
|
2021-11-19 08:11:47 +00:00
|
|
|
def get_submodule_dirs(full_path: bool = False) -> List:
|
2020-10-21 11:30:49 +00:00
|
|
|
"""
|
|
|
|
To avoid issue could be introduced by multi-os or additional dependency,
|
|
|
|
we use python and git to get this output
|
|
|
|
:return: List of submodule dirs
|
|
|
|
"""
|
|
|
|
dirs = []
|
|
|
|
try:
|
2022-01-28 07:21:12 +00:00
|
|
|
lines = (
|
|
|
|
subprocess.check_output(
|
|
|
|
[
|
|
|
|
'git',
|
|
|
|
'config',
|
|
|
|
'--file',
|
|
|
|
os.path.realpath(os.path.join(IDF_PATH, '.gitmodules')),
|
|
|
|
'--get-regexp',
|
|
|
|
'path',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.decode('utf8')
|
|
|
|
.strip()
|
|
|
|
.split('\n')
|
|
|
|
)
|
2020-10-21 11:30:49 +00:00
|
|
|
for line in lines:
|
|
|
|
_, path = line.split(' ')
|
2020-10-30 09:28:24 +00:00
|
|
|
if full_path:
|
|
|
|
dirs.append(os.path.join(IDF_PATH, path))
|
|
|
|
else:
|
|
|
|
dirs.append(path)
|
2021-02-02 02:53:40 +00:00
|
|
|
except Exception as e: # pylint: disable=W0703
|
2020-10-21 11:30:49 +00:00
|
|
|
logging.warning(str(e))
|
|
|
|
|
|
|
|
return dirs
|
2020-11-12 06:11:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _check_git_filemode(full_path): # type: (str) -> bool
|
|
|
|
try:
|
2022-01-28 07:21:12 +00:00
|
|
|
stdout = (
|
|
|
|
subprocess.check_output(['git', 'ls-files', '--stage', full_path])
|
|
|
|
.strip()
|
|
|
|
.decode('utf-8')
|
|
|
|
)
|
2020-11-12 06:11:54 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return True
|
|
|
|
|
|
|
|
mode = stdout.split(' ', 1)[0] # e.g. 100644 for a rw-r--r--
|
|
|
|
if any([int(i, 8) & 1 for i in mode[-3:]]):
|
2021-04-26 07:46:44 +00:00
|
|
|
return True
|
|
|
|
return False
|
2020-11-12 06:11:54 +00:00
|
|
|
|
|
|
|
|
2021-11-19 08:11:47 +00:00
|
|
|
def is_executable(full_path: str) -> bool:
|
2020-11-12 06:11:54 +00:00
|
|
|
"""
|
|
|
|
os.X_OK will always return true on windows. Use git to check file mode.
|
|
|
|
:param full_path: file full path
|
|
|
|
:return: True is it's an executable file
|
|
|
|
"""
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
return _check_git_filemode(full_path)
|
2021-02-02 02:53:40 +00:00
|
|
|
return os.access(full_path, os.X_OK)
|
|
|
|
|
|
|
|
|
2021-11-19 08:11:47 +00:00
|
|
|
def get_git_files(path: str = IDF_PATH, full_path: bool = False) -> List[str]:
|
2021-02-02 02:53:40 +00:00
|
|
|
"""
|
|
|
|
Get the result of git ls-files
|
|
|
|
:param path: path to run git ls-files
|
|
|
|
:param full_path: return full path if set to True
|
|
|
|
:return: list of file paths
|
|
|
|
"""
|
|
|
|
try:
|
2021-10-27 06:20:49 +00:00
|
|
|
# this is a workaround when using under worktree
|
|
|
|
# if you're using worktree, when running git commit a new environment variable GIT_DIR would be declared,
|
|
|
|
# the value should be <origin_repo_path>/.git/worktrees/<worktree name>
|
|
|
|
# This would effect the return value of `git ls-files`, unset this would use the `cwd`value or its parent
|
|
|
|
# folder if no `.git` folder found in `cwd`.
|
|
|
|
workaround_env = os.environ.copy()
|
|
|
|
workaround_env.pop('GIT_DIR', None)
|
2022-01-28 07:21:12 +00:00
|
|
|
files = (
|
|
|
|
subprocess.check_output(['git', 'ls-files'], cwd=path, env=workaround_env)
|
|
|
|
.decode('utf8')
|
|
|
|
.strip()
|
|
|
|
.split('\n')
|
|
|
|
)
|
2021-02-02 02:53:40 +00:00
|
|
|
except Exception as e: # pylint: disable=W0703
|
|
|
|
logging.warning(str(e))
|
|
|
|
files = []
|
|
|
|
return [os.path.join(path, f) for f in files] if full_path else files
|
|
|
|
|
|
|
|
|
2021-11-19 08:11:47 +00:00
|
|
|
def is_in_directory(file_path: str, folder: str) -> bool:
|
|
|
|
return os.path.realpath(file_path).startswith(os.path.realpath(folder) + os.sep)
|
|
|
|
|
|
|
|
|
2022-04-29 04:19:32 +00:00
|
|
|
def to_list(s: Any) -> List[Any]:
|
2022-05-07 04:18:56 +00:00
|
|
|
if isinstance(s, (set, tuple)):
|
2022-04-29 04:19:32 +00:00
|
|
|
return list(s)
|
2022-05-07 04:18:56 +00:00
|
|
|
|
|
|
|
if isinstance(s, list):
|
2022-04-29 04:19:32 +00:00
|
|
|
return s
|
2022-05-07 04:18:56 +00:00
|
|
|
|
|
|
|
return [s]
|
2022-04-29 04:19:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class PytestApp:
|
|
|
|
path: str
|
|
|
|
target: str
|
|
|
|
config: str
|
|
|
|
|
|
|
|
def __hash__(self) -> int:
|
|
|
|
return hash((self.path, self.target, self.config))
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2022-01-28 07:21:12 +00:00
|
|
|
class PytestCase:
|
2022-04-29 04:19:32 +00:00
|
|
|
path: str
|
|
|
|
name: str
|
|
|
|
apps: Set[PytestApp]
|
2021-11-19 08:11:47 +00:00
|
|
|
|
2022-04-29 04:19:32 +00:00
|
|
|
def __hash__(self) -> int:
|
|
|
|
return hash((self.path, self.name, self.apps))
|
2021-11-19 08:11:47 +00:00
|
|
|
|
|
|
|
|
2022-01-28 07:21:12 +00:00
|
|
|
class PytestCollectPlugin:
|
|
|
|
def __init__(self, target: str) -> None:
|
|
|
|
self.target = target
|
2022-04-29 04:19:32 +00:00
|
|
|
self.cases: List[PytestCase] = []
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_param(item: 'Function', key: str, default: Any = None) -> Any:
|
|
|
|
if not hasattr(item, 'callspec'):
|
|
|
|
raise ValueError(f'Function {item} does not have params')
|
|
|
|
|
|
|
|
return item.callspec.params.get(key, default) or default
|
2021-11-19 08:11:47 +00:00
|
|
|
|
2022-01-28 07:21:12 +00:00
|
|
|
def pytest_collection_modifyitems(self, items: List['Function']) -> None:
|
2022-04-29 04:19:32 +00:00
|
|
|
from pytest_embedded.plugin import parse_multi_dut_args
|
2021-11-19 08:11:47 +00:00
|
|
|
|
2022-04-29 04:19:32 +00:00
|
|
|
for item in items:
|
|
|
|
count = 1
|
|
|
|
case_path = str(item.path)
|
|
|
|
case_name = item.originalname
|
2022-01-28 07:21:12 +00:00
|
|
|
target = self.target
|
2022-04-29 04:19:32 +00:00
|
|
|
# funcargs is not calculated while collection
|
2022-01-28 07:21:12 +00:00
|
|
|
if hasattr(item, 'callspec'):
|
2022-04-29 04:19:32 +00:00
|
|
|
count = item.callspec.params.get('count', 1)
|
|
|
|
app_paths = to_list(
|
|
|
|
parse_multi_dut_args(
|
|
|
|
count,
|
|
|
|
self.get_param(item, 'app_path', os.path.dirname(case_path)),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
configs = to_list(
|
|
|
|
parse_multi_dut_args(
|
|
|
|
count, self.get_param(item, 'config', 'default')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
targets = to_list(
|
|
|
|
parse_multi_dut_args(count, self.get_param(item, 'target', target))
|
|
|
|
)
|
2022-01-28 07:21:12 +00:00
|
|
|
else:
|
2022-04-29 04:19:32 +00:00
|
|
|
app_paths = [os.path.dirname(case_path)]
|
|
|
|
configs = ['default']
|
|
|
|
targets = [target]
|
2022-02-04 14:44:29 +00:00
|
|
|
|
2022-04-29 04:19:32 +00:00
|
|
|
case_apps = set()
|
|
|
|
for i in range(count):
|
|
|
|
case_apps.add(PytestApp(app_paths[i], targets[i], configs[i]))
|
|
|
|
|
|
|
|
self.cases.append(PytestCase(case_path, case_name, case_apps))
|
2022-01-28 07:21:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_pytest_cases(folder: str, target: str) -> List[PytestCase]:
|
|
|
|
import pytest
|
|
|
|
from _pytest.config import ExitCode
|
|
|
|
|
|
|
|
collector = PytestCollectPlugin(target)
|
|
|
|
|
|
|
|
with io.StringIO() as buf:
|
|
|
|
with redirect_stdout(buf):
|
2022-04-29 04:19:32 +00:00
|
|
|
res = pytest.main(
|
|
|
|
['--collect-only', folder, '-q', '--target', target],
|
|
|
|
plugins=[collector],
|
|
|
|
)
|
2022-01-28 07:21:12 +00:00
|
|
|
if res.value != ExitCode.OK:
|
|
|
|
if res.value == ExitCode.NO_TESTS_COLLECTED:
|
2022-04-29 04:19:32 +00:00
|
|
|
print(
|
|
|
|
f'WARNING: no pytest app found for target {target} under folder {folder}'
|
|
|
|
)
|
2022-01-28 07:21:12 +00:00
|
|
|
else:
|
|
|
|
print(buf.getvalue())
|
|
|
|
raise RuntimeError('pytest collection failed')
|
|
|
|
|
2022-04-29 04:19:32 +00:00
|
|
|
return collector.cases
|
2022-01-28 07:21:12 +00:00
|
|
|
|
|
|
|
|
2022-04-29 04:19:32 +00:00
|
|
|
def get_pytest_app_paths(folder: str, target: str) -> Set[str]:
|
|
|
|
cases = get_pytest_cases(folder, target)
|
2021-11-19 08:11:47 +00:00
|
|
|
|
2022-04-29 04:19:32 +00:00
|
|
|
return set({app.path for case in cases for app in case.apps})
|