Skip to content

Commit

Permalink
twister: support scanning for ztests in subdirectories
Browse files Browse the repository at this point in the history
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 20b81a8)

Original-Signed-off-by: Christopher Friedt <cfriedt@meta.com>
GitOrigin-RevId: 20b81a8
Change-Id: I71eeca8fbc6dca6cdf5b992c983509e00c691e40
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/4853667
Commit-Queue: Keith Short <keithshort@chromium.org>
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com>
Tested-by: Keith Short <keithshort@chromium.org>
  • Loading branch information
cfriedt authored and Chromeos LUCI committed Sep 8, 2023
1 parent 4754340 commit 79b2c4b
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions scripts/pylib/twister/twisterlib/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 79b2c4b

Please sign in to comment.