Skip to content
Snippets Groups Projects
Commit e55a6b5c authored by Nicola Sparnacci's avatar Nicola Sparnacci
Browse files

[CORE] Generalize folder search function


Generalize the folder search function to allow searching for a
folder name or a sub-path. For both cases loop thorugh the starting
directory.

Add unit tests to test this function update.

Signed-off-by: default avatarNicola Sparnacci <nicola.sparnacci@seco.com>
parent 2d2279cf
No related branches found
No related tags found
1 merge request!36[FIX] fix function for path
Pipeline #273047 canceled with stages
in 29 seconds
......@@ -244,41 +244,41 @@ def find_files_with_pattern_in_children_dirs(start_dir, pattern):
matching_paths.append(os.path.join(root, file))
return matching_paths
## @brief Searches for a directory with a specific name in the child directories of a given parent directory.
# This function look for the path starting from the parent directory to the directory with the specified name,
# verifying that this exists.
# If the target_dir_name given is a path, this is used as path to be appended to parent_dir
# If found, it returns the path to the directory;
# otherwise, it returns `None`.
# @param parent_dir The directory from which to start searching.
# @param target_dir_name The name of the directory to search for within the child directories.
# @return The path to the directory if found, otherwise `None`.
# @note This function does not follow symbolic links to directories.
# @note The search is case-sensitive, meaning it will look for an exact match of `target_dir_name`.
## @brief Recursively searches for a directory (or relative path) within a parent directory.
#
# This function searches under the given parent directory for a directory matching the specified
# name or relative path. It supports both simple directory names (e.g., "build") and partial
# paths (e.g., "src/include"). The search checks each level under parent_dir to see if the full
# path exists.
#
# If the directory is found, the function returns its absolute path. If not, it returns `None`.
#
# @param parent_dir The root directory to start the search from.
# @param target_dir The directory name or relative path to search for.
# @return The full path to the directory if found, otherwise `None`.
#
# @note This function does not follow symbolic links.
# @note The search is case-sensitive.
#
# @example
# @code
# parent_dir = "/home/user/projects"
# target_dir_name = "example_project"
# found_dir = find_directory_in_children(parent_dir, target_dir_name)
# target_dir = "example_project"
# found_dir = find_directory_in_children(parent_dir, target_dir)
# if found_dir:
# print(f"Directory found: {found_dir}")
# else:
# print("Directory not found.")
# @endcode
def find_directory_in_children(parent_dir, target_dir):
target_dir = target_dir.strip(os.sep)
for root, _, _ in os.walk(parent_dir):
candidate = os.path.join(root, target_dir)
if os.path.isdir(candidate):
return candidate
def find_directory_in_children(parent_dir, target_dir_name):
target_dirs = target_dir_name.split("/")
if len(target_dirs) > 1 :
path = os.path.join(parent_dir,target_dir_name)
else:
for root, dirs, _ in os.walk(parent_dir):
if target_dir_name in dirs:
return os.path.join(root, target_dir_name)
return None
if os.path.exists(path):
return path
else:
return None
return None
## @brief Searches for a file in parent directories starting from the specified directory.
# This function traverses parent directories starting from `start_dir` and searches for the specified filename.
......
......@@ -404,27 +404,30 @@ class TestUtilsFindInChildrenClass:
# @param expected_result str The expected path of the found folder.
# @param tmp_path str The temporary path for creating the directory structure.
@pytest.mark.parametrize(
"folder_name, start_dir_structure, target_dir_structure, expected_result",
"folder_name, target_dir_structure, expected_result",
[
# Case 1: Folder in the start directory
("target_folder", ["start"], ["start/target_folder"], "start/target_folder"),
("target_folder", ["start/target_folder"], "start/target_folder"),
# Case 2: Folder in a subdirectory of the start directory
("target_folder", ["start/subdir"], ["start/subdir/target_folder"], "start/subdir/target_folder"),
("target_folder", ["start/subdir/target_folder"], "start/subdir/target_folder"),
# Case 3: Search subpath
("subdir1/target_folder", ["start/subdir1/target_folder"], "start/subdir1/target_folder"),
# Case 4: search subpath but the part of it doesn't exist. Not found
("wrong_subdir/target_folder", ["start/subdir1/target_folder"], None),
# Case 3: Folder not found
("non_existent_folder", ["start"], [], None),
# Case 5: Folder not found
("non_existent_folder", [], None),
# Case 4: Folder at the root directory
("target_folder", [], ["target_folder"], "target_folder"),
# Case 6: Folder at the root directory
("target_folder", ["target_folder"], "target_folder"),
]
)
def test_find_directory_in_children(self, tmp_path, folder_name, start_dir_structure, target_dir_structure, expected_result):
def test_find_directory_in_children(self, tmp_path, folder_name, target_dir_structure, expected_result):
"""Test finding directories with expected passing cases."""
# Set up the directory structure
for subdir in start_dir_structure:
os.makedirs(tmp_path / subdir, exist_ok=True)
for subdir in target_dir_structure:
os.makedirs(tmp_path / subdir, exist_ok=True)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment