Skip to content

Commit 84b6055

Browse files
committed
Fix search methods not working correctly with relative paths.
1 parent 7121d4a commit 84b6055

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/fsutil/operations.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ def _search_paths(path: PathIn, pattern: str) -> list[str]:
518518
pathname = os.path.join(path, pattern)
519519
paths = glob.glob(pathname, recursive=True)
520520
# normalize paths to use OS-specific separators
521-
paths = [os.path.normpath(path_result) for path_result in paths]
521+
paths = [
522+
os.path.relpath(os.path.normpath(path_result), path) for path_result in paths
523+
]
522524
return paths
523525

524526

tests/test_operations.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,24 @@ def test_search_files(temp_path):
518518
assert results == expected_results
519519

520520

521+
def test_search_files_with_relative_path(temp_path, monkeypatch):
522+
"""
523+
Regression test: search_files with a relative path must not return an empty
524+
list due to the double-prefix bug in _filter_paths.
525+
"""
526+
fsutil.create_file(temp_path("a/b/c/IMG_1002.png"))
527+
fsutil.create_file(temp_path("a/x/c/IMG_1005.png"))
528+
fsutil.create_file(temp_path("a/b/c/IMG_1000.jpg"))
529+
530+
# change cwd to the parent of "a/" so that the relative path is valid
531+
monkeypatch.chdir(temp_path(""))
532+
533+
results = fsutil.search_files("./a/", "**/c/IMG_*.png")
534+
535+
assert len(results) == 2
536+
assert all(fsutil.is_file(result) for result in results)
537+
538+
521539
def test_search_dirs(temp_path):
522540
fsutil.create_file(temp_path("a/b/c/IMG_1000.jpg"))
523541
fsutil.create_file(temp_path("x/y/z/c/IMG_1001.jpg"))

0 commit comments

Comments
 (0)