From 79b2c4b620e353c3e08c831cd6cb2916958a3c84 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Sun, 27 Aug 2023 06:47:52 -0400 Subject: [PATCH] twister: support scanning for ztests in subdirectories Previously, only the main src/ directories was scanned for ZTest testsuite names and tests. This allows us to place test sources in subdirectories as well. (cherry picked from commit 20b81a865d38207cd67ba981a8aa8c38baba3470) Original-Signed-off-by: Christopher Friedt GitOrigin-RevId: 20b81a865d38207cd67ba981a8aa8c38baba3470 Change-Id: I71eeca8fbc6dca6cdf5b992c983509e00c691e40 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/4853667 Commit-Queue: Keith Short Tested-by: ChromeOS Prod (Robot) Reviewed-by: Fabio Baltieri Tested-by: Keith Short --- scripts/pylib/twister/twisterlib/testsuite.py | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/scripts/pylib/twister/twisterlib/testsuite.py b/scripts/pylib/twister/twisterlib/testsuite.py index 3f4a71e4a8c..bdd681699ff 100644 --- a/scripts/pylib/twister/twisterlib/testsuite.py +++ b/scripts/pylib/twister/twisterlib/testsuite.py @@ -256,6 +256,28 @@ def _find_ztest_testcases(search_area, testcase_regex): return testcase_names, warnings +def find_c_files_in(path: str, extensions: list = ['c', 'cpp', 'cxx', 'cc']) -> list: + """ + Find C or C++ sources in the directory specified by "path" + """ + if not os.path.isdir(path): + return [] + + # back up previous CWD + oldpwd = os.getcwd() + os.chdir(path) + + filenames = [] + for ext in extensions: + # glob.glob('**/*.c') does not pick up the base directory + filenames += [os.path.join(path, x) for x in glob.glob(f'*.{ext}')] + # glob matches in subdirectories too + filenames += [os.path.join(path, x) for x in glob.glob(f'**/*.{ext}')] + + # restore previous CWD + os.chdir(oldpwd) + + return filenames def scan_testsuite_path(testsuite_path): subcases = [] @@ -265,7 +287,7 @@ def scan_testsuite_path(testsuite_path): ztest_suite_names = [] src_dir_path = _find_src_dir_path(testsuite_path) - for filename in glob.glob(os.path.join(src_dir_path, "*.c*")): + for filename in find_c_files_in(src_dir_path): if os.stat(filename).st_size == 0: continue try: @@ -288,7 +310,7 @@ def scan_testsuite_path(testsuite_path): except ValueError as e: logger.error("%s: error parsing source file: %s" % (filename, e)) - for filename in glob.glob(os.path.join(testsuite_path, "*.c*")): + for filename in find_c_files_in(testsuite_path): try: result: ScanPathResult = scan_file(filename) if result.warnings: