2020-10-21 07:30:49 -04:00
|
|
|
# internal use only for CI
|
|
|
|
# some CI related util functions
|
|
|
|
#
|
2022-04-29 00:19:32 -04:00
|
|
|
# SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
|
2021-10-27 02:20:49 -04:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2020-10-21 07:30:49 -04:00
|
|
|
#
|
2022-07-12 22:34:02 -04:00
|
|
|
import contextlib
|
2022-01-28 02:21:12 -05:00
|
|
|
import io
|
2020-10-21 07:30:49 -04:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
2020-11-12 01:11:54 -05:00
|
|
|
import sys
|
2022-01-28 02:21:12 -05:00
|
|
|
from contextlib import redirect_stdout
|
2022-04-29 00:19:32 -04:00
|
|
|
from dataclasses import dataclass
|
2022-11-29 04:36:56 -05:00
|
|
|
from pathlib import Path
|
2022-07-12 22:34:02 -04:00
|
|
|
from typing import TYPE_CHECKING, Any, List, Optional, Set, Union
|
|
|
|
|
|
|
|
try:
|
|
|
|
from idf_py_actions.constants import PREVIEW_TARGETS, SUPPORTED_TARGETS
|
|
|
|
except ImportError:
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
|
|
|
|
from idf_py_actions.constants import PREVIEW_TARGETS, SUPPORTED_TARGETS
|
2020-10-21 07:30:49 -04:00
|
|
|
|
2022-01-28 02:21:12 -05:00
|
|
|
if TYPE_CHECKING:
|
2022-04-29 00:19:32 -04:00
|
|
|
from _pytest.python import Function
|
|
|
|
|
2022-11-29 03:14:26 -05:00
|
|
|
IDF_PATH = os.path.abspath(os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..')))
|
2020-10-21 07:30:49 -04:00
|
|
|
|
|
|
|
|
2022-11-29 04:36:56 -05:00
|
|
|
def get_submodule_dirs(full_path: bool = False) -> List[str]:
|
2020-10-21 07:30:49 -04: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 02:21:12 -05: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 07:30:49 -04:00
|
|
|
for line in lines:
|
|
|
|
_, path = line.split(' ')
|
2020-10-30 05:28:24 -04:00
|
|
|
if full_path:
|
|
|
|
dirs.append(os.path.join(IDF_PATH, path))
|
|
|
|
else:
|
|
|
|
dirs.append(path)
|
2021-02-01 21:53:40 -05:00
|
|
|
except Exception as e: # pylint: disable=W0703
|
2020-10-21 07:30:49 -04:00
|
|
|
logging.warning(str(e))
|
|
|
|
|
|
|
|
return dirs
|
2020-11-12 01:11:54 -05:00
|
|
|
|
|
|
|
|
2022-11-29 04:36:56 -05:00
|
|
|
def _check_git_filemode(full_path: str) -> bool:
|
2020-11-12 01:11:54 -05:00
|
|
|
try:
|
2022-11-29 03:14:26 -05:00
|
|
|
stdout = subprocess.check_output(['git', 'ls-files', '--stage', full_path]).strip().decode('utf-8')
|
2020-11-12 01:11:54 -05: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 03:46:44 -04:00
|
|
|
return True
|
|
|
|
return False
|
2020-11-12 01:11:54 -05:00
|
|
|
|
|
|
|
|
2021-11-19 03:11:47 -05:00
|
|
|
def is_executable(full_path: str) -> bool:
|
2020-11-12 01:11:54 -05: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-01 21:53:40 -05:00
|
|
|
return os.access(full_path, os.X_OK)
|
|
|
|
|
|
|
|
|
2021-11-19 03:11:47 -05:00
|
|
|
def get_git_files(path: str = IDF_PATH, full_path: bool = False) -> List[str]:
|
2021-02-01 21:53:40 -05: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 02:20:49 -04: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>
|
2022-07-17 22:17:54 -04:00
|
|
|
# This would affect the return value of `git ls-files`, unset this would use the `cwd`value or its parent
|
2021-10-27 02:20:49 -04:00
|
|
|
# folder if no `.git` folder found in `cwd`.
|
|
|
|
workaround_env = os.environ.copy()
|
|
|
|
workaround_env.pop('GIT_DIR', None)
|
2022-01-28 02:21:12 -05:00
|
|
|
files = (
|
|
|
|
subprocess.check_output(['git', 'ls-files'], cwd=path, env=workaround_env)
|
|
|
|
.decode('utf8')
|
|
|
|
.strip()
|
|
|
|
.split('\n')
|
|
|
|
)
|
2021-02-01 21:53:40 -05: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 03:11:47 -05: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 00:19:32 -04:00
|
|
|
def to_list(s: Any) -> List[Any]:
|
2022-05-07 00:18:56 -04:00
|
|
|
if isinstance(s, (set, tuple)):
|
2022-04-29 00:19:32 -04:00
|
|
|
return list(s)
|
2022-05-07 00:18:56 -04:00
|
|
|
|
|
|
|
if isinstance(s, list):
|
2022-04-29 00:19:32 -04:00
|
|
|
return s
|
2022-05-07 00:18:56 -04:00
|
|
|
|
|
|
|
return [s]
|
2022-04-29 00:19:32 -04:00
|
|
|
|
|
|
|
|
2022-07-12 22:34:02 -04:00
|
|
|
####################
|
|
|
|
# Pytest Utilities #
|
|
|
|
####################
|
2022-04-29 00:19:32 -04: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 02:21:12 -05:00
|
|
|
class PytestCase:
|
2022-04-29 00:19:32 -04:00
|
|
|
path: str
|
|
|
|
name: str
|
|
|
|
apps: Set[PytestApp]
|
2022-11-29 03:14:26 -05:00
|
|
|
|
2022-07-17 22:17:54 -04:00
|
|
|
nightly_run: bool
|
2021-11-19 03:11:47 -05:00
|
|
|
|
2022-04-29 00:19:32 -04:00
|
|
|
def __hash__(self) -> int:
|
2022-07-17 22:17:54 -04:00
|
|
|
return hash((self.path, self.name, self.apps, self.nightly_run))
|
2021-11-19 03:11:47 -05:00
|
|
|
|
|
|
|
|
2022-01-28 02:21:12 -05:00
|
|
|
class PytestCollectPlugin:
|
|
|
|
def __init__(self, target: str) -> None:
|
|
|
|
self.target = target
|
2022-04-29 00:19:32 -04: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 03:11:47 -05:00
|
|
|
|
2022-05-18 02:59:34 -04:00
|
|
|
def pytest_report_collectionfinish(self, items: List['Function']) -> None:
|
2022-04-29 00:19:32 -04:00
|
|
|
from pytest_embedded.plugin import parse_multi_dut_args
|
2021-11-19 03:11:47 -05:00
|
|
|
|
2022-04-29 00:19:32 -04:00
|
|
|
for item in items:
|
|
|
|
count = 1
|
|
|
|
case_path = str(item.path)
|
|
|
|
case_name = item.originalname
|
2022-01-28 02:21:12 -05:00
|
|
|
target = self.target
|
2022-04-29 00:19:32 -04:00
|
|
|
# funcargs is not calculated while collection
|
2022-01-28 02:21:12 -05:00
|
|
|
if hasattr(item, 'callspec'):
|
2022-04-29 00:19:32 -04: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)),
|
|
|
|
)
|
|
|
|
)
|
2022-11-29 03:14:26 -05:00
|
|
|
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 02:21:12 -05:00
|
|
|
else:
|
2022-04-29 00:19:32 -04:00
|
|
|
app_paths = [os.path.dirname(case_path)]
|
|
|
|
configs = ['default']
|
|
|
|
targets = [target]
|
2022-02-04 09:44:29 -05:00
|
|
|
|
2022-04-29 00:19:32 -04:00
|
|
|
case_apps = set()
|
|
|
|
for i in range(count):
|
|
|
|
case_apps.add(PytestApp(app_paths[i], targets[i], configs[i]))
|
|
|
|
|
2022-07-17 22:17:54 -04:00
|
|
|
self.cases.append(
|
|
|
|
PytestCase(
|
|
|
|
case_path,
|
|
|
|
case_name,
|
|
|
|
case_apps,
|
|
|
|
'nightly_run' in [marker.name for marker in item.iter_markers()],
|
|
|
|
)
|
|
|
|
)
|
2022-01-28 02:21:12 -05:00
|
|
|
|
|
|
|
|
2022-11-29 04:36:56 -05:00
|
|
|
def get_pytest_files(paths: List[str]) -> List[str]:
|
|
|
|
# this is a workaround to solve pytest collector super slow issue
|
|
|
|
# benchmark with
|
|
|
|
# - time pytest -m esp32 --collect-only
|
|
|
|
# user=15.57s system=1.35s cpu=95% total=17.741
|
|
|
|
# - time { find -name 'pytest_*.py'; } | xargs pytest -m esp32 --collect-only
|
|
|
|
# user=0.11s system=0.63s cpu=36% total=2.044
|
|
|
|
# user=1.76s system=0.22s cpu=43% total=4.539
|
|
|
|
# use glob.glob would also save a bunch of time
|
|
|
|
pytest_scripts: Set[str] = set()
|
|
|
|
for p in paths:
|
|
|
|
path = Path(p)
|
|
|
|
pytest_scripts.update(str(_p) for _p in path.glob('**/pytest_*.py'))
|
|
|
|
|
|
|
|
return list(pytest_scripts)
|
|
|
|
|
|
|
|
|
2022-05-18 02:59:34 -04:00
|
|
|
def get_pytest_cases(
|
2022-11-29 03:14:26 -05:00
|
|
|
paths: Union[str, List[str]],
|
|
|
|
target: str = 'all',
|
|
|
|
marker_expr: Optional[str] = None,
|
|
|
|
filter_expr: Optional[str] = None,
|
2022-05-18 02:59:34 -04:00
|
|
|
) -> List[PytestCase]:
|
2022-01-28 02:21:12 -05:00
|
|
|
import pytest
|
|
|
|
from _pytest.config import ExitCode
|
|
|
|
|
2022-07-12 22:34:02 -04:00
|
|
|
if target == 'all':
|
|
|
|
targets = SUPPORTED_TARGETS + PREVIEW_TARGETS
|
2022-05-18 02:59:34 -04:00
|
|
|
else:
|
2022-07-12 22:34:02 -04:00
|
|
|
targets = [target]
|
|
|
|
|
2022-07-17 22:17:54 -04:00
|
|
|
paths = to_list(paths)
|
|
|
|
|
|
|
|
origin_include_nightly_run_env = os.getenv('INCLUDE_NIGHTLY_RUN')
|
|
|
|
origin_nightly_run_env = os.getenv('NIGHTLY_RUN')
|
|
|
|
|
|
|
|
# disable the env vars to get all test cases
|
|
|
|
if 'INCLUDE_NIGHTLY_RUN' in os.environ:
|
|
|
|
os.environ.pop('INCLUDE_NIGHTLY_RUN')
|
|
|
|
|
|
|
|
if 'NIGHTLY_RUN' in os.environ:
|
|
|
|
os.environ.pop('NIGHTLY_RUN')
|
|
|
|
|
|
|
|
# collect all cases
|
|
|
|
os.environ['INCLUDE_NIGHTLY_RUN'] = '1'
|
|
|
|
|
2022-07-12 22:34:02 -04:00
|
|
|
cases = []
|
2022-11-29 04:36:56 -05:00
|
|
|
for target in targets:
|
|
|
|
collector = PytestCollectPlugin(target)
|
|
|
|
|
|
|
|
with io.StringIO() as buf:
|
|
|
|
with redirect_stdout(buf):
|
2022-11-29 04:48:36 -05:00
|
|
|
cmd = ['--collect-only', *get_pytest_files(paths), '--target', target, '-q']
|
|
|
|
if marker_expr:
|
|
|
|
cmd.extend(['-m', marker_expr])
|
2022-11-29 04:36:56 -05:00
|
|
|
if filter_expr:
|
|
|
|
cmd.extend(['-k', filter_expr])
|
|
|
|
res = pytest.main(cmd, plugins=[collector])
|
|
|
|
if res.value != ExitCode.OK:
|
|
|
|
if res.value == ExitCode.NO_TESTS_COLLECTED:
|
|
|
|
print(f'WARNING: no pytest app found for target {target} under paths {", ".join(paths)}')
|
|
|
|
else:
|
|
|
|
print(buf.getvalue())
|
|
|
|
raise RuntimeError(
|
|
|
|
f'pytest collection failed at {", ".join(paths)} with command \"{" ".join(cmd)}\"'
|
|
|
|
)
|
2022-07-12 22:34:02 -04:00
|
|
|
|
|
|
|
cases.extend(collector.cases)
|
|
|
|
|
2022-07-17 22:17:54 -04:00
|
|
|
# revert back the env vars
|
|
|
|
if origin_include_nightly_run_env is not None:
|
|
|
|
os.environ['INCLUDE_NIGHTLY_RUN'] = origin_include_nightly_run_env
|
|
|
|
|
|
|
|
if origin_nightly_run_env is not None:
|
|
|
|
os.environ['NIGHTLY_RUN'] = origin_nightly_run_env
|
|
|
|
|
2022-07-12 22:34:02 -04:00
|
|
|
return cases
|
2022-01-28 02:21:12 -05:00
|
|
|
|
|
|
|
|
2022-07-12 22:34:02 -04:00
|
|
|
##################
|
|
|
|
# TTFW Utilities #
|
|
|
|
##################
|
|
|
|
def get_ttfw_cases(paths: Union[str, List[str]]) -> List[Any]:
|
|
|
|
"""
|
|
|
|
Get the test cases from ttfw_idf under the given paths
|
|
|
|
|
|
|
|
:param paths: list of paths to search
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
from ttfw_idf.IDFAssignTest import IDFAssignTest
|
|
|
|
except ImportError:
|
|
|
|
sys.path.append(os.path.join(IDF_PATH, 'tools', 'ci', 'python_packages'))
|
|
|
|
|
|
|
|
from ttfw_idf.IDFAssignTest import IDFAssignTest
|
|
|
|
|
|
|
|
# mock CI_JOB_ID if not exists
|
|
|
|
if not os.environ.get('CI_JOB_ID'):
|
|
|
|
os.environ['CI_JOB_ID'] = '1'
|
|
|
|
|
|
|
|
cases = []
|
|
|
|
for path in to_list(paths):
|
2022-11-29 03:14:26 -05:00
|
|
|
assign = IDFAssignTest(path, os.path.join(IDF_PATH, '.gitlab', 'ci', 'target-test.yml'))
|
2022-07-12 22:34:02 -04:00
|
|
|
with contextlib.redirect_stdout(None): # swallow stdout
|
|
|
|
try:
|
|
|
|
cases += assign.search_cases()
|
|
|
|
except ImportError as e:
|
|
|
|
logging.error(str(e))
|
|
|
|
|
|
|
|
return cases
|
|
|
|
|
|
|
|
|
2022-11-29 03:14:26 -05:00
|
|
|
def get_ttfw_app_paths(paths: Union[str, List[str]], target: Optional[str] = None) -> Set[str]:
|
2022-07-12 22:34:02 -04:00
|
|
|
"""
|
|
|
|
Get the app paths from ttfw_idf under the given paths
|
|
|
|
"""
|
|
|
|
from idf_build_apps import CMakeApp
|
|
|
|
|
|
|
|
cases = get_ttfw_cases(paths)
|
|
|
|
res: Set[str] = set()
|
|
|
|
for case in cases:
|
|
|
|
if not target or target == case.case_info['target'].lower():
|
|
|
|
# ttfw has no good way to detect the app path for master-slave tests
|
|
|
|
# the apps real location may be the sub folder of the test script path
|
|
|
|
# check if the current folder is an app, if it's not, add all its subfolders if they are apps
|
|
|
|
# only one level down
|
|
|
|
_app_dir = case.case_info['app_dir']
|
|
|
|
if CMakeApp.is_app(_app_dir):
|
|
|
|
res.add(_app_dir)
|
|
|
|
else:
|
|
|
|
for child in os.listdir(_app_dir):
|
|
|
|
sub_path = os.path.join(_app_dir, child)
|
|
|
|
if os.path.isdir(sub_path) and CMakeApp.is_app(sub_path):
|
|
|
|
res.add(sub_path)
|
|
|
|
|
|
|
|
return res
|