Skip to content

Commit

Permalink
Merge pull request #406 from FuzTheCat/main
Browse files Browse the repository at this point in the history
Updated files.py Check path is a file before opening.
  • Loading branch information
LeonOstrez authored Dec 26, 2023
2 parents 72fd07a + 41af0e7 commit c6e9ead
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
19 changes: 13 additions & 6 deletions pilot/helpers/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,35 @@ def get_file_contents(
will be a Python string. If that fails, it will be treated as a
binary file and `content` will be a Python bytes object.
"""
# Normalize the path to avoid issues with different path separators
full_path = os.path.normpath(path)

try:
# Assume it's a text file using UTF-8 encoding
file_content = open(path, "r", encoding="utf-8").read()
with open(full_path, "r", encoding="utf-8") as file:
file_content = file.read()
except UnicodeDecodeError:
# If that fails, we'll treat it as a binary file
file_content = open(path, "rb").read()
with open(full_path, "rb") as file:
file_content = file.read()
except NotADirectoryError:
raise ValueError(f"Path is not a directory: {path}")
except FileNotFoundError:
raise ValueError(f"File not found: {path}")
raise ValueError(f"File not found: {full_path}")
except Exception as e:
raise ValueError(f"Exception in get_file_contents: {e}")

file_name = os.path.basename(path)
relative_path = str(Path(path).parent.relative_to(project_root_path))

if relative_path == ".":
relative_path = ""
if relative_path == '.':
relative_path = ''

return {
"name": file_name,
"path": relative_path,
"content": file_content,
"full_path": path,
"full_path": full_path,
}


Expand Down
15 changes: 2 additions & 13 deletions pilot/test/helpers/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def np(path: str) -> str:
return str(Path(path))

mock_os.path.join = os.path.join
mock_os.path.normpath = os.path.normpath
mock_os.path.basename = os.path.basename

mock_walk = mock_os.walk
Expand All @@ -103,7 +104,7 @@ def np(path: str) -> str:
(np("/fake/root/foo"), [], ["foo.txt"]),
(np("/fake/root/bar"), [], ["bar.txt"]),
]
mock_open.return_value.read.side_effect = [
mock_open.return_value.__enter__.return_value.read.side_effect = [
"file.txt",
"foo.txt - 無為",
UnicodeDecodeError("utf-8", b"\xff\xff\xff", 0, 1, "invalid start byte"),
Expand Down Expand Up @@ -132,18 +133,6 @@ def np(path: str) -> str:
},
]
mock_walk.assert_called_once_with(np("/fake/root"))
mock_open.assert_has_calls(
[
call(np("/fake/root/file.txt"), "r", encoding="utf-8"),
call().read(),
call(np("/fake/root/foo/foo.txt"), "r", encoding="utf-8"),
call().read(),
call(np("/fake/root/bar/bar.txt"), "r", encoding="utf-8"),
call().read(),
call(np("/fake/root/bar/bar.txt"), "rb"),
call().read(),
]
)


def test_get_directory_contents_live():
Expand Down

0 comments on commit c6e9ead

Please sign in to comment.