-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add file_utils module #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||
| """File utility helpers.""" | ||||||
| import os | ||||||
|
|
||||||
|
|
||||||
| def read_file(path: str) -> str: | ||||||
| with open(path) as f: | ||||||
| return f.read() | ||||||
|
|
||||||
|
|
||||||
| def write_file(path: str, content: str) -> None: | ||||||
| with open(path, 'w') as f: | ||||||
| f.write(content) | ||||||
|
|
||||||
|
|
||||||
| def file_exists(path: str) -> bool: | ||||||
| return os.path.isfile(path) | ||||||
|
|
||||||
|
|
||||||
| def get_extension(path: str) -> str: | ||||||
| _, ext = os.path.splitext(path) | ||||||
| return ext.lstrip('.') | ||||||
|
|
||||||
|
|
||||||
| def list_files(directory: str, extension: str = None) -> list[str]: | ||||||
| files = [] | ||||||
| for entry in os.listdir(directory): | ||||||
| full_path = os.path.join(directory, entry) | ||||||
| if os.path.isfile(full_path): | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: If a caller passes Given that
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic duplication: if extension is None or get_extension(entry) == extension.lstrip('.'):
|
||||||
| if extension is None or entry.endswith(f'.{extension}'): | ||||||
| files.append(full_path) | ||||||
| return sorted(files) | ||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test coverage:
list_fileshas three distinct branches — no extension filter, extension filter matching, and extension filter not matching — plus theos.path.isfileguard that silently skips subdirectories. None of these paths are tested. A unit test using pytest'stmp_pathfixture would catch regressions in the filtering logic, including the leading-dot bug noted above.