mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
ci: build_pytest_app will now remove the non-test apps
simplify the cli as well
This commit is contained in:
parent
e6c0d1e481
commit
153433d47d
@ -42,63 +42,56 @@ build_pytest_examples_esp32:
|
|||||||
- .build_pytest_template
|
- .build_pytest_template
|
||||||
- .rules:build:example_test-esp32
|
- .rules:build:example_test-esp32
|
||||||
script:
|
script:
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir examples --target esp32 --size-info $SIZE_INFO_LOCATION -vv
|
- run_cmd python tools/ci/build_pytest_apps.py examples --target esp32 --size-info $SIZE_INFO_LOCATION -vv
|
||||||
|
|
||||||
build_pytest_examples_esp32s2:
|
build_pytest_examples_esp32s2:
|
||||||
extends:
|
extends:
|
||||||
- .build_pytest_template
|
- .build_pytest_template
|
||||||
- .rules:build:example_test-esp32s2
|
- .rules:build:example_test-esp32s2
|
||||||
script:
|
script:
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir examples --target esp32s2 --size-info $SIZE_INFO_LOCATION -vv
|
- run_cmd python tools/ci/build_pytest_apps.py examples --target esp32s2 --size-info $SIZE_INFO_LOCATION -vv
|
||||||
|
|
||||||
build_pytest_examples_esp32s3:
|
build_pytest_examples_esp32s3:
|
||||||
extends:
|
extends:
|
||||||
- .build_pytest_template
|
- .build_pytest_template
|
||||||
- .rules:build:example_test-esp32s3
|
- .rules:build:example_test-esp32s3
|
||||||
script:
|
script:
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir examples --target esp32s3 --size-info $SIZE_INFO_LOCATION -vv
|
- run_cmd python tools/ci/build_pytest_apps.py examples --target esp32s3 --size-info $SIZE_INFO_LOCATION -vv
|
||||||
|
|
||||||
build_pytest_examples_esp32c3:
|
build_pytest_examples_esp32c3:
|
||||||
extends:
|
extends:
|
||||||
- .build_pytest_template
|
- .build_pytest_template
|
||||||
- .rules:build:example_test-esp32c3
|
- .rules:build:example_test-esp32c3
|
||||||
script:
|
script:
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir examples --target esp32c3 --size-info $SIZE_INFO_LOCATION -vv
|
- run_cmd python tools/ci/build_pytest_apps.py examples --target esp32c3 --size-info $SIZE_INFO_LOCATION -vv
|
||||||
|
|
||||||
build_pytest_components_esp32:
|
build_pytest_components_esp32:
|
||||||
extends:
|
extends:
|
||||||
- .build_pytest_template
|
- .build_pytest_template
|
||||||
- .rules:build:component_ut-esp32
|
- .rules:build:component_ut-esp32
|
||||||
script:
|
script:
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32 --size-info $SIZE_INFO_LOCATION -vv
|
- run_cmd python tools/ci/build_pytest_apps.py components --target esp32 --size-info $SIZE_INFO_LOCATION -vv
|
||||||
|
|
||||||
build_pytest_components_esp32s2:
|
build_pytest_components_esp32s2:
|
||||||
extends:
|
extends:
|
||||||
- .build_pytest_template
|
- .build_pytest_template
|
||||||
- .rules:build:component_ut-esp32s2
|
- .rules:build:component_ut-esp32s2
|
||||||
script:
|
script:
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32s2 --size-info $SIZE_INFO_LOCATION -vv
|
- run_cmd python tools/ci/build_pytest_apps.py components --target esp32s2 --size-info $SIZE_INFO_LOCATION -vv
|
||||||
|
|
||||||
build_pytest_components_esp32s3:
|
build_pytest_components_esp32s3:
|
||||||
extends:
|
extends:
|
||||||
- .build_pytest_template
|
- .build_pytest_template
|
||||||
- .rules:build:component_ut-esp32s3
|
- .rules:build:component_ut-esp32s3
|
||||||
script:
|
script:
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32s3 --size-info $SIZE_INFO_LOCATION -vv
|
- run_cmd python tools/ci/build_pytest_apps.py components --target esp32s3 --size-info $SIZE_INFO_LOCATION -vv
|
||||||
|
|
||||||
build_pytest_components_esp32c2:
|
|
||||||
extends:
|
|
||||||
- .build_pytest_template
|
|
||||||
- .rules:build:component_ut-esp32c2
|
|
||||||
script:
|
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32c2 --size-info $SIZE_INFO_LOCATION -vv
|
|
||||||
|
|
||||||
build_pytest_components_esp32c3:
|
build_pytest_components_esp32c3:
|
||||||
extends:
|
extends:
|
||||||
- .build_pytest_template
|
- .build_pytest_template
|
||||||
- .rules:build:component_ut-esp32c3
|
- .rules:build:component_ut-esp32c3
|
||||||
script:
|
script:
|
||||||
- python tools/ci/build_pytest_apps.py --all-pytest-apps --under-dir components --target esp32c3 --size-info $SIZE_INFO_LOCATION -vv
|
- run_cmd python tools/ci/build_pytest_apps.py components --target esp32c3 --size-info $SIZE_INFO_LOCATION -vv
|
||||||
|
|
||||||
.build_template_app_template:
|
.build_template_app_template:
|
||||||
extends: .build_template
|
extends: .build_template
|
||||||
|
@ -9,39 +9,39 @@ import argparse
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from collections import defaultdict
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from idf_ci_utils import IDF_PATH, get_pytest_dirs
|
from idf_ci_utils import IDF_PATH, get_pytest_cases
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from build_apps import build_apps
|
from build_apps import build_apps
|
||||||
from find_apps import find_apps, find_builds_for_app
|
from find_apps import find_builds_for_app
|
||||||
from find_build_apps import BuildItem, CMakeBuildSystem, config_rules_from_str, setup_logging
|
from find_build_apps import BuildItem, config_rules_from_str, setup_logging
|
||||||
except ImportError:
|
except ImportError:
|
||||||
sys.path.append(os.path.join(IDF_PATH, 'tools'))
|
sys.path.append(os.path.join(IDF_PATH, 'tools'))
|
||||||
|
|
||||||
from build_apps import build_apps
|
from build_apps import build_apps
|
||||||
from find_apps import find_apps, find_builds_for_app
|
from find_apps import find_builds_for_app
|
||||||
from find_build_apps import BuildItem, CMakeBuildSystem, config_rules_from_str, setup_logging
|
from find_build_apps import BuildItem, config_rules_from_str, setup_logging
|
||||||
|
|
||||||
|
|
||||||
def main(args: argparse.Namespace) -> None:
|
def main(args: argparse.Namespace) -> None:
|
||||||
if args.all_pytest_apps:
|
pytest_cases = []
|
||||||
paths = get_pytest_dirs(args.under_dir)
|
for path in args.paths:
|
||||||
args.recursive = True
|
pytest_cases += get_pytest_cases(path, args.target)
|
||||||
elif args.paths is None:
|
|
||||||
paths = [os.getcwd()]
|
|
||||||
else:
|
|
||||||
paths = args.paths
|
|
||||||
|
|
||||||
app_dirs = []
|
paths = set()
|
||||||
for path in paths:
|
app_configs = defaultdict(set)
|
||||||
app_dirs += find_apps(CMakeBuildSystem, path, args.recursive, [], args.target)
|
for case in pytest_cases:
|
||||||
|
paths.add(case.app_path)
|
||||||
|
app_configs[case.app_path].add(case.config)
|
||||||
|
|
||||||
|
app_dirs = list(paths)
|
||||||
if not app_dirs:
|
if not app_dirs:
|
||||||
logging.error('No apps found')
|
raise RuntimeError('No apps found')
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
logging.info('Found {} apps'.format(len(app_dirs)))
|
logging.info(f'Found {len(app_dirs)} apps')
|
||||||
app_dirs.sort()
|
app_dirs.sort()
|
||||||
|
|
||||||
# Find compatible configurations of each app, collect them as BuildItems
|
# Find compatible configurations of each app, collect them as BuildItems
|
||||||
@ -50,61 +50,58 @@ def main(args: argparse.Namespace) -> None:
|
|||||||
for app_dir in app_dirs:
|
for app_dir in app_dirs:
|
||||||
app_dir = os.path.realpath(app_dir)
|
app_dir = os.path.realpath(app_dir)
|
||||||
build_items += find_builds_for_app(
|
build_items += find_builds_for_app(
|
||||||
app_dir,
|
app_path=app_dir,
|
||||||
app_dir,
|
work_dir=app_dir,
|
||||||
'build_@t_@w',
|
build_dir='build_@t_@w',
|
||||||
f'{app_dir}/build_@t_@w/build.log',
|
build_log=f'{app_dir}/build_@t_@w/build.log',
|
||||||
args.target,
|
target_arg=args.target,
|
||||||
'cmake',
|
build_system='cmake',
|
||||||
config_rules,
|
config_rules=config_rules,
|
||||||
True,
|
|
||||||
)
|
)
|
||||||
logging.info('Found {} builds'.format(len(build_items)))
|
logging.info(f'Found {len(build_items)} builds')
|
||||||
build_items.sort(key=lambda x: x.build_path) # type: ignore
|
build_items.sort(key=lambda x: x.build_path) # type: ignore
|
||||||
|
|
||||||
build_apps(build_items, args.parallel_count, args.parallel_index, False, args.build_verbose, True, None,
|
# auto clean up the binaries if no flag --preserve-all
|
||||||
args.size_info)
|
if args.preserve_all is False:
|
||||||
|
for item in build_items:
|
||||||
|
if item.config_name not in app_configs[item.app_dir]:
|
||||||
|
item.preserve = False
|
||||||
|
|
||||||
|
build_apps(
|
||||||
|
build_items=build_items,
|
||||||
|
parallel_count=args.parallel_count,
|
||||||
|
parallel_index=args.parallel_index,
|
||||||
|
dry_run=False,
|
||||||
|
build_verbose=args.build_verbose,
|
||||||
|
keep_going=True,
|
||||||
|
output_build_list=None,
|
||||||
|
size_info=args.size_info,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(description='Tool to generate build steps for IDF apps')
|
parser = argparse.ArgumentParser(
|
||||||
parser.add_argument(
|
description='Build all the pytest apps under specified paths. Will auto remove those non-test apps binaries'
|
||||||
'--recursive',
|
|
||||||
action='store_true',
|
|
||||||
help='Look for apps in the specified directories recursively.',
|
|
||||||
)
|
)
|
||||||
parser.add_argument('--target', required=True, help='Build apps for given target.')
|
parser.add_argument('--target', required=True, help='Build apps for given target.')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--config',
|
'--config',
|
||||||
default=['sdkconfig.ci=default', 'sdkconfig.ci.*=', '=default'],
|
default=['sdkconfig.ci=default', 'sdkconfig.ci.*=', '=default'],
|
||||||
action='append',
|
action='append',
|
||||||
help='Adds configurations (sdkconfig file names) to build. This can either be ' +
|
help='Adds configurations (sdkconfig file names) to build. This can either be '
|
||||||
'FILENAME[=NAME] or FILEPATTERN. FILENAME is the name of the sdkconfig file, ' +
|
+ 'FILENAME[=NAME] or FILEPATTERN. FILENAME is the name of the sdkconfig file, '
|
||||||
'relative to the project directory, to be used. Optional NAME can be specified, ' +
|
+ 'relative to the project directory, to be used. Optional NAME can be specified, '
|
||||||
'which can be used as a name of this configuration. FILEPATTERN is the name of ' +
|
+ 'which can be used as a name of this configuration. FILEPATTERN is the name of '
|
||||||
'the sdkconfig file, relative to the project directory, with at most one wildcard. ' +
|
+ 'the sdkconfig file, relative to the project directory, with at most one wildcard. '
|
||||||
'The part captured by the wildcard is used as the name of the configuration.',
|
+ 'The part captured by the wildcard is used as the name of the configuration.',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-p', '--paths',
|
'paths',
|
||||||
nargs='*',
|
nargs='+',
|
||||||
help='One or more app paths. Will use the current path if not specified.'
|
help='One or more app paths. Will use the current path if not specified.',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--all-pytest-apps',
|
'--parallel-count', default=1, type=int, help='Number of parallel build jobs.'
|
||||||
action='store_true',
|
|
||||||
help='Look for all pytest apps. "--paths" would be ignored if specify this flag.'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--under-dir',
|
|
||||||
help='Build only the pytest apps under this directory if specified. '
|
|
||||||
'Would be ignored if "--all-pytest-apps" is unflagged.'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--parallel-count',
|
|
||||||
default=1,
|
|
||||||
type=int,
|
|
||||||
help='Number of parallel build jobs.'
|
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--parallel-index',
|
'--parallel-index',
|
||||||
@ -115,7 +112,7 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--size-info',
|
'--size-info',
|
||||||
type=argparse.FileType('a'),
|
type=argparse.FileType('a'),
|
||||||
help='If specified, the test case name and size info json will be written to this file'
|
help='If specified, the test case name and size info json will be written to this file',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-v',
|
'-v',
|
||||||
@ -128,6 +125,11 @@ if __name__ == '__main__':
|
|||||||
action='store_true',
|
action='store_true',
|
||||||
help='Enable verbose output from build system.',
|
help='Enable verbose output from build system.',
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--preserve-all',
|
||||||
|
action='store_true',
|
||||||
|
help='add this flag to preserve the binaries for all apps',
|
||||||
|
)
|
||||||
arguments = parser.parse_args()
|
arguments = parser.parse_args()
|
||||||
setup_logging(arguments)
|
setup_logging(arguments)
|
||||||
main(arguments)
|
main(arguments)
|
||||||
|
@ -4,13 +4,20 @@
|
|||||||
# SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
|
# SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
|
import io
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from typing import List
|
from contextlib import redirect_stdout
|
||||||
|
from typing import TYPE_CHECKING, List
|
||||||
|
|
||||||
IDF_PATH = os.path.abspath(os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..')))
|
if TYPE_CHECKING:
|
||||||
|
from _pytest.nodes import Function
|
||||||
|
|
||||||
|
IDF_PATH = os.path.abspath(
|
||||||
|
os.getenv('IDF_PATH', os.path.join(os.path.dirname(__file__), '..', '..'))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_submodule_dirs(full_path: bool = False) -> List:
|
def get_submodule_dirs(full_path: bool = False) -> List:
|
||||||
@ -21,9 +28,21 @@ def get_submodule_dirs(full_path: bool = False) -> List:
|
|||||||
"""
|
"""
|
||||||
dirs = []
|
dirs = []
|
||||||
try:
|
try:
|
||||||
lines = subprocess.check_output(
|
lines = (
|
||||||
['git', 'config', '--file', os.path.realpath(os.path.join(IDF_PATH, '.gitmodules')),
|
subprocess.check_output(
|
||||||
'--get-regexp', 'path']).decode('utf8').strip().split('\n')
|
[
|
||||||
|
'git',
|
||||||
|
'config',
|
||||||
|
'--file',
|
||||||
|
os.path.realpath(os.path.join(IDF_PATH, '.gitmodules')),
|
||||||
|
'--get-regexp',
|
||||||
|
'path',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.decode('utf8')
|
||||||
|
.strip()
|
||||||
|
.split('\n')
|
||||||
|
)
|
||||||
for line in lines:
|
for line in lines:
|
||||||
_, path = line.split(' ')
|
_, path = line.split(' ')
|
||||||
if full_path:
|
if full_path:
|
||||||
@ -38,7 +57,11 @@ def get_submodule_dirs(full_path: bool = False) -> List:
|
|||||||
|
|
||||||
def _check_git_filemode(full_path): # type: (str) -> bool
|
def _check_git_filemode(full_path): # type: (str) -> bool
|
||||||
try:
|
try:
|
||||||
stdout = subprocess.check_output(['git', 'ls-files', '--stage', full_path]).strip().decode('utf-8')
|
stdout = (
|
||||||
|
subprocess.check_output(['git', 'ls-files', '--stage', full_path])
|
||||||
|
.strip()
|
||||||
|
.decode('utf-8')
|
||||||
|
)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -74,8 +97,12 @@ def get_git_files(path: str = IDF_PATH, full_path: bool = False) -> List[str]:
|
|||||||
# folder if no `.git` folder found in `cwd`.
|
# folder if no `.git` folder found in `cwd`.
|
||||||
workaround_env = os.environ.copy()
|
workaround_env = os.environ.copy()
|
||||||
workaround_env.pop('GIT_DIR', None)
|
workaround_env.pop('GIT_DIR', None)
|
||||||
files = subprocess.check_output(['git', 'ls-files'], cwd=path, env=workaround_env) \
|
files = (
|
||||||
.decode('utf8').strip().split('\n')
|
subprocess.check_output(['git', 'ls-files'], cwd=path, env=workaround_env)
|
||||||
|
.decode('utf8')
|
||||||
|
.strip()
|
||||||
|
.split('\n')
|
||||||
|
)
|
||||||
except Exception as e: # pylint: disable=W0703
|
except Exception as e: # pylint: disable=W0703
|
||||||
logging.warning(str(e))
|
logging.warning(str(e))
|
||||||
files = []
|
files = []
|
||||||
@ -86,30 +113,61 @@ def is_in_directory(file_path: str, folder: str) -> bool:
|
|||||||
return os.path.realpath(file_path).startswith(os.path.realpath(folder) + os.sep)
|
return os.path.realpath(file_path).startswith(os.path.realpath(folder) + os.sep)
|
||||||
|
|
||||||
|
|
||||||
def get_pytest_dirs(folder: str) -> List[str]:
|
class PytestCase:
|
||||||
import pytest
|
def __init__(self, test_path: str, target: str, config: str, case: str):
|
||||||
from _pytest.nodes import Item
|
self.app_path = os.path.dirname(test_path)
|
||||||
|
self.test_path = test_path
|
||||||
|
self.target = target
|
||||||
|
self.config = config
|
||||||
|
self.case = case
|
||||||
|
|
||||||
class CollectPlugin:
|
def __repr__(self) -> str:
|
||||||
def __init__(self) -> None:
|
return f'{self.test_path}: {self.target}.{self.config}.{self.case}'
|
||||||
self.nodes: List[Item] = []
|
|
||||||
|
|
||||||
def pytest_collection_modifyitems(self, items: List[Item]) -> None:
|
|
||||||
|
class PytestCollectPlugin:
|
||||||
|
def __init__(self, target: str) -> None:
|
||||||
|
self.target = target
|
||||||
|
self.nodes: List[PytestCase] = []
|
||||||
|
|
||||||
|
def pytest_collection_modifyitems(self, items: List['Function']) -> None:
|
||||||
for item in items:
|
for item in items:
|
||||||
self.nodes.append(item)
|
|
||||||
|
|
||||||
collector = CollectPlugin()
|
|
||||||
|
|
||||||
res = pytest.main(['--collect-only', '-q', folder], plugins=[collector])
|
|
||||||
if res.value != 0:
|
|
||||||
raise RuntimeError('pytest collection failed')
|
|
||||||
|
|
||||||
sys.stdout.flush() # print instantly
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
test_file_paths = set(node.path for node in collector.nodes)
|
file_path = str(item.path)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# pytest 6.x
|
# pytest 6.x
|
||||||
test_file_paths = set(node.fspath for node in collector.nodes)
|
file_path = item.fspath
|
||||||
|
|
||||||
return [os.path.dirname(file) for file in test_file_paths]
|
target = self.target
|
||||||
|
if hasattr(item, 'callspec'):
|
||||||
|
config = item.callspec.params.get('config', 'default')
|
||||||
|
else:
|
||||||
|
config = 'default'
|
||||||
|
case_name = item.originalname
|
||||||
|
|
||||||
|
self.nodes.append(PytestCase(file_path, target, config, case_name))
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
res = pytest.main(['--collect-only', folder, '-q', '--target', target], 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 folder {folder}')
|
||||||
|
else:
|
||||||
|
print(buf.getvalue())
|
||||||
|
raise RuntimeError('pytest collection failed')
|
||||||
|
|
||||||
|
return collector.nodes
|
||||||
|
|
||||||
|
|
||||||
|
def get_pytest_app_paths(folder: str, target: str) -> List[str]:
|
||||||
|
nodes = get_pytest_cases(folder, target)
|
||||||
|
|
||||||
|
return list({node.app_path for node in nodes})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user