fix: escape directory to prevent \n on Windows directory name to fail on Pathlib + Tests (#4101)

* 📝 (utils.py): add format_directory_path function to properly escape and format directory paths for consistency and validity

* ✨ (test_format_directory_path.py): add unit tests for the format_directory_path function to ensure correct formatting of directory paths
📝 (test_format_directory_path.py): add documentation and examples for different types of directory paths in the unit tests to improve code readability and maintainability

* 🐛 (utils.py): fix the incorrect replacement of backslashes with newline characters in the format_directory_path function
📝 (test_rewrite_file_path.py): update test cases and function names to reflect the changes made in the format_directory_path function in utils.py

* 🐛 (test_format_directory_path.py): update parameter name from 'path' to 'input_path' for clarity and consistency
📝 (test_format_directory_path.py): improve test case descriptions and handle newline characters in paths correctly
This commit is contained in:
Cristhian Zanforlin Lousa 2024-10-14 13:54:05 -03:00 • committed by GitHub
commit 844efc1f73
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 86 additions and 32 deletions

View file

@ -44,6 +44,18 @@ def is_hidden(path: Path) -> bool:
return path.name.startswith(".")
def format_directory_path(path: str) -> str:
"""Format a directory path to ensure it's properly escaped and valid.
Args:
path (str): The input path string.
Returns:
str: A properly formatted path string.
"""
return path.replace("\n", "\\n")
def retrieve_file_paths(
path: str,
*,
@ -52,6 +64,7 @@ def retrieve_file_paths(
depth: int,
types: list[str] = TEXT_FILE_TYPES,
) -> list[str]:
path = format_directory_path(path)
path_obj = Path(path)
if not path_obj.exists() or not path_obj.is_dir():
msg = f"Path {path} must exist and be a directory."