2024-01-23 04:32:14 -05:00
|
|
|
# SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
2023-07-31 00:49:08 -04:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2023-12-18 09:29:58 -05:00
|
|
|
import fnmatch
|
2023-07-31 00:49:08 -04:00
|
|
|
import io
|
2023-12-18 09:29:58 -05:00
|
|
|
import logging
|
|
|
|
import os.path
|
2023-07-31 00:49:08 -04:00
|
|
|
import typing as t
|
|
|
|
from contextlib import redirect_stdout
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from _pytest.config import ExitCode
|
2024-01-23 04:32:14 -05:00
|
|
|
from idf_build_apps import App
|
|
|
|
from idf_build_apps import find_apps
|
|
|
|
from idf_build_apps.constants import BuildStatus
|
|
|
|
from idf_build_apps.constants import SUPPORTED_TARGETS
|
2023-12-18 09:29:58 -05:00
|
|
|
from idf_ci.app import IdfCMakeApp
|
2024-01-23 04:32:14 -05:00
|
|
|
from idf_ci_utils import get_all_manifest_files
|
|
|
|
from idf_ci_utils import IDF_PATH
|
|
|
|
from idf_ci_utils import idf_relpath
|
|
|
|
from idf_ci_utils import to_list
|
2023-07-31 00:49:08 -04:00
|
|
|
from idf_py_actions.constants import PREVIEW_TARGETS as TOOLS_PREVIEW_TARGETS
|
|
|
|
from idf_py_actions.constants import SUPPORTED_TARGETS as TOOLS_SUPPORTED_TARGETS
|
|
|
|
|
2024-01-23 04:32:14 -05:00
|
|
|
from .constants import CollectMode
|
|
|
|
from .constants import DEFAULT_BUILD_LOG_FILENAME
|
|
|
|
from .constants import DEFAULT_CONFIG_RULES_STR
|
|
|
|
from .constants import PytestCase
|
2023-06-13 05:12:55 -04:00
|
|
|
from .plugin import IdfPytestEmbedded
|
2023-07-31 00:49:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_pytest_files(paths: t.List[str]) -> t.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: t.Set[str] = set()
|
|
|
|
for p in paths:
|
|
|
|
path = Path(p)
|
|
|
|
pytest_scripts.update(str(_p) for _p in path.glob('**/pytest_*.py') if 'managed_components' not in _p.parts)
|
|
|
|
|
|
|
|
return list(pytest_scripts)
|
|
|
|
|
|
|
|
|
|
|
|
def get_pytest_cases(
|
|
|
|
paths: t.Union[str, t.List[str]],
|
2023-11-28 08:38:47 -05:00
|
|
|
target: str = CollectMode.ALL,
|
2023-12-18 09:29:58 -05:00
|
|
|
*,
|
2024-03-12 06:04:37 -04:00
|
|
|
config_name: t.Optional[str] = None,
|
2023-07-31 00:49:08 -04:00
|
|
|
marker_expr: t.Optional[str] = None,
|
|
|
|
filter_expr: t.Optional[str] = None,
|
2023-12-18 09:29:58 -05:00
|
|
|
apps: t.Optional[t.List[App]] = None,
|
2023-07-31 00:49:08 -04:00
|
|
|
) -> t.List[PytestCase]:
|
2023-11-28 08:38:47 -05:00
|
|
|
"""
|
2024-01-26 08:09:33 -05:00
|
|
|
Return the list of test cases
|
|
|
|
|
2023-11-28 08:38:47 -05:00
|
|
|
For single-dut test cases, `target` could be
|
|
|
|
- [TARGET], e.g. `esp32`, to get the test cases for the given target
|
|
|
|
- or `single_all`, to get all single-dut test cases
|
2023-07-31 00:49:08 -04:00
|
|
|
|
2023-11-28 08:38:47 -05:00
|
|
|
For multi-dut test cases, `target` could be
|
|
|
|
- [TARGET,[TARGET...]], e.g. `esp32,esp32s2`, to get the test cases for the given targets
|
|
|
|
- or `multi_all`, to get all multi-dut test cases
|
|
|
|
|
|
|
|
:param paths: paths to search for pytest scripts
|
2023-12-18 09:29:58 -05:00
|
|
|
:param target: target or keywords to get test cases for, detailed above
|
2024-03-12 06:04:37 -04:00
|
|
|
:param config_name: sdkconfig name
|
2023-11-28 08:38:47 -05:00
|
|
|
:param marker_expr: pytest marker expression, `-m`
|
|
|
|
:param filter_expr: pytest filter expression, `-k`
|
2023-12-18 09:29:58 -05:00
|
|
|
:param apps: built app list, skip the tests required by apps not in the list
|
2023-11-28 08:38:47 -05:00
|
|
|
:return: list of test cases
|
|
|
|
"""
|
2023-07-31 00:49:08 -04:00
|
|
|
paths = to_list(paths)
|
|
|
|
|
|
|
|
cases: t.List[PytestCase] = []
|
|
|
|
pytest_scripts = get_pytest_files(paths) # type: ignore
|
|
|
|
if not pytest_scripts:
|
|
|
|
print(f'WARNING: no pytest scripts found for target {target} under paths {", ".join(paths)}')
|
|
|
|
return cases
|
|
|
|
|
2023-11-28 08:38:47 -05:00
|
|
|
def _get_pytest_cases(_target: str, _single_target_duplicate_mode: bool = False) -> t.List[PytestCase]:
|
2024-03-12 06:04:37 -04:00
|
|
|
collector = IdfPytestEmbedded(_target, config_name=config_name, single_target_duplicate_mode=_single_target_duplicate_mode, apps=apps)
|
2023-07-31 00:49:08 -04:00
|
|
|
|
|
|
|
with io.StringIO() as buf:
|
|
|
|
with redirect_stdout(buf):
|
2023-11-28 08:38:47 -05:00
|
|
|
cmd = ['--collect-only', *pytest_scripts, '--target', _target, '-q']
|
2023-07-31 00:49:08 -04:00
|
|
|
if marker_expr:
|
|
|
|
cmd.extend(['-m', marker_expr])
|
|
|
|
if filter_expr:
|
|
|
|
cmd.extend(['-k', filter_expr])
|
|
|
|
res = pytest.main(cmd, plugins=[collector])
|
|
|
|
|
2023-09-05 09:12:35 -04:00
|
|
|
if res.value != ExitCode.OK:
|
|
|
|
if res.value == ExitCode.NO_TESTS_COLLECTED:
|
2023-11-28 08:38:47 -05:00
|
|
|
print(f'WARNING: no pytest app found for target {_target} under paths {", ".join(paths)}')
|
2023-09-05 09:12:35 -04:00
|
|
|
else:
|
|
|
|
print(buf.getvalue())
|
2023-11-28 08:38:47 -05:00
|
|
|
raise RuntimeError(
|
|
|
|
f'pytest collection failed at {", ".join(paths)} with command \"{" ".join(cmd)}\"'
|
|
|
|
)
|
|
|
|
|
|
|
|
return collector.cases # type: ignore
|
|
|
|
|
|
|
|
if target == CollectMode.ALL:
|
|
|
|
targets = TOOLS_SUPPORTED_TARGETS + TOOLS_PREVIEW_TARGETS + [CollectMode.MULTI_ALL_WITH_PARAM]
|
|
|
|
else:
|
|
|
|
targets = [target]
|
2023-07-31 00:49:08 -04:00
|
|
|
|
2023-11-28 08:38:47 -05:00
|
|
|
for _target in targets:
|
|
|
|
if target == CollectMode.ALL:
|
|
|
|
cases.extend(_get_pytest_cases(_target, _single_target_duplicate_mode=True))
|
|
|
|
else:
|
|
|
|
cases.extend(_get_pytest_cases(_target))
|
2023-07-31 00:49:08 -04:00
|
|
|
|
2023-11-28 08:38:47 -05:00
|
|
|
return sorted(cases, key=lambda x: (x.path, x.name, str(x.targets)))
|
2023-12-18 09:29:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_all_apps(
|
|
|
|
paths: t.List[str],
|
|
|
|
target: str = CollectMode.ALL,
|
|
|
|
*,
|
|
|
|
marker_expr: t.Optional[str] = None,
|
|
|
|
filter_expr: t.Optional[str] = None,
|
|
|
|
config_rules_str: t.Optional[t.List[str]] = None,
|
|
|
|
preserve_all: bool = False,
|
|
|
|
extra_default_build_targets: t.Optional[t.List[str]] = None,
|
|
|
|
modified_components: t.Optional[t.List[str]] = None,
|
|
|
|
modified_files: t.Optional[t.List[str]] = None,
|
|
|
|
ignore_app_dependencies_filepatterns: t.Optional[t.List[str]] = None,
|
|
|
|
) -> t.Tuple[t.Set[App], t.Set[App]]:
|
|
|
|
"""
|
|
|
|
Return the tuple of test-required apps and non-test-related apps
|
|
|
|
|
|
|
|
:param paths: paths to search for pytest scripts
|
|
|
|
:param target: target or keywords to get test cases for, explained in `get_pytest_cases`
|
|
|
|
:param marker_expr: pytest marker expression, `-m`
|
|
|
|
:param filter_expr: pytest filter expression, `-k`
|
|
|
|
:param config_rules_str: config rules string
|
|
|
|
:param preserve_all: preserve all apps
|
|
|
|
:param extra_default_build_targets: extra default build targets
|
|
|
|
:param modified_components: modified components
|
|
|
|
:param modified_files: modified files
|
|
|
|
:param ignore_app_dependencies_filepatterns: ignore app dependencies filepatterns
|
|
|
|
:return: tuple of test-required apps and non-test-related apps
|
|
|
|
"""
|
2024-01-26 08:09:33 -05:00
|
|
|
# target could be comma separated list
|
|
|
|
all_apps: t.List[App] = []
|
|
|
|
for _t in set(target.split(',')):
|
|
|
|
all_apps.extend(find_apps(
|
|
|
|
paths,
|
|
|
|
_t,
|
|
|
|
build_system=IdfCMakeApp,
|
|
|
|
recursive=True,
|
|
|
|
build_dir='build_@t_@w',
|
|
|
|
config_rules_str=config_rules_str or DEFAULT_CONFIG_RULES_STR,
|
|
|
|
build_log_filename=DEFAULT_BUILD_LOG_FILENAME,
|
|
|
|
size_json_filename='size.json',
|
|
|
|
check_warnings=True,
|
|
|
|
manifest_rootpath=IDF_PATH,
|
|
|
|
manifest_files=get_all_manifest_files(),
|
|
|
|
default_build_targets=SUPPORTED_TARGETS + (extra_default_build_targets or []),
|
|
|
|
modified_components=modified_components,
|
|
|
|
modified_files=modified_files,
|
|
|
|
ignore_app_dependencies_filepatterns=ignore_app_dependencies_filepatterns,
|
|
|
|
include_skipped_apps=True,
|
|
|
|
))
|
2023-12-18 09:29:58 -05:00
|
|
|
|
|
|
|
pytest_cases = get_pytest_cases(
|
|
|
|
paths,
|
|
|
|
target,
|
|
|
|
marker_expr=marker_expr,
|
|
|
|
filter_expr=filter_expr,
|
|
|
|
)
|
|
|
|
|
|
|
|
modified_pytest_cases = []
|
|
|
|
if modified_files:
|
|
|
|
modified_pytest_scripts = [
|
|
|
|
os.path.dirname(f) for f in modified_files if fnmatch.fnmatch(os.path.basename(f), 'pytest_*.py')
|
|
|
|
]
|
|
|
|
if modified_pytest_scripts:
|
|
|
|
modified_pytest_cases = get_pytest_cases(
|
|
|
|
modified_pytest_scripts,
|
|
|
|
target,
|
|
|
|
marker_expr=marker_expr,
|
|
|
|
filter_expr=filter_expr,
|
|
|
|
)
|
|
|
|
|
|
|
|
# app_path, target, config
|
2024-01-23 04:32:14 -05:00
|
|
|
pytest_app_path_tuple_dict: t.Dict[t.Tuple[str, str, str], PytestCase] = {}
|
2023-12-18 09:29:58 -05:00
|
|
|
for case in pytest_cases:
|
|
|
|
for app in case.apps:
|
2024-01-23 04:32:14 -05:00
|
|
|
pytest_app_path_tuple_dict[(app.path, app.target, app.config)] = case
|
2023-12-18 09:29:58 -05:00
|
|
|
|
2024-01-23 04:32:14 -05:00
|
|
|
modified_pytest_app_path_tuple_dict: t.Dict[t.Tuple[str, str, str], PytestCase] = {}
|
2023-12-18 09:29:58 -05:00
|
|
|
for case in modified_pytest_cases:
|
|
|
|
for app in case.apps:
|
2024-01-23 04:32:14 -05:00
|
|
|
modified_pytest_app_path_tuple_dict[(app.path, app.target, app.config)] = case
|
2023-12-18 09:29:58 -05:00
|
|
|
|
|
|
|
test_related_apps: t.Set[App] = set()
|
|
|
|
non_test_related_apps: t.Set[App] = set()
|
|
|
|
for app in all_apps:
|
2024-01-23 04:32:14 -05:00
|
|
|
# PytestCase.app.path is idf_relpath
|
|
|
|
app_path = idf_relpath(app.app_dir)
|
|
|
|
|
2023-12-18 09:29:58 -05:00
|
|
|
# override build_status if test script got modified
|
2024-01-23 04:32:14 -05:00
|
|
|
if case := modified_pytest_app_path_tuple_dict.get((app_path, app.target, app.config_name)):
|
2023-12-18 09:29:58 -05:00
|
|
|
test_related_apps.add(app)
|
|
|
|
app.build_status = BuildStatus.SHOULD_BE_BUILT
|
|
|
|
app.preserve = True
|
|
|
|
logging.debug('Found app: %s - required by modified test case %s', app, case.path)
|
|
|
|
elif app.build_status != BuildStatus.SKIPPED:
|
2024-01-23 04:32:14 -05:00
|
|
|
if case := pytest_app_path_tuple_dict.get((app_path, app.target, app.config_name)):
|
2023-12-18 09:29:58 -05:00
|
|
|
test_related_apps.add(app)
|
2024-02-16 03:32:19 -05:00
|
|
|
# build or not should be decided by the build stage
|
2023-12-18 09:29:58 -05:00
|
|
|
app.preserve = True
|
|
|
|
logging.debug('Found test-related app: %s - required by %s', app, case.path)
|
|
|
|
else:
|
|
|
|
non_test_related_apps.add(app)
|
|
|
|
app.preserve = preserve_all
|
|
|
|
logging.debug('Found non-test-related app: %s', app)
|
|
|
|
|
|
|
|
print(f'Found {len(test_related_apps)} test-related apps')
|
|
|
|
print(f'Found {len(non_test_related_apps)} non-test-related apps')
|
|
|
|
|
|
|
|
return test_related_apps, non_test_related_apps
|