Skip to content

Commit

Permalink
Validate that the file content has type bytes (pantsbuild#20261)
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnan-chandra authored Dec 4, 2023
1 parent 4741b38 commit 215cedb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/python/pants/engine/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class FileContent:
content: bytes
is_executable: bool = False

def __post_init__(self):
if not isinstance(self.content, bytes):
raise TypeError(
f"Expected 'content' to be bytes, but got {type(self.content).__name__}"
)

def __repr__(self) -> str:
return (
f"FileContent(path={self.path}, content=(len:{len(self.content)}), "
Expand Down
12 changes: 12 additions & 0 deletions src/python/pants/engine/fs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ def try_with_backoff(assertion_fn: Callable[[], bool], count: int = 4) -> bool:
return False


# -----------------------------------------------------------------------------------------------
# `FileContent`
# -----------------------------------------------------------------------------------------------


def test_file_content_non_bytes():
with pytest.raises(TypeError) as exc:
FileContent(path="4.txt", content="four")

assert str(exc.value) == "Expected 'content' to be bytes, but got str"


# -----------------------------------------------------------------------------------------------
# `PathGlobs`, including `GlobMatchErrorBehavior` and symlink handling
# -----------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 215cedb

Please sign in to comment.