Skip to content

Commit 54c99cb

Browse files
committed
Return bytes from Contents.decoded for empty files
According to the docs, Contents.decoded should return bytes. However, in the case of empty files, it currently returns str. Make it return bytes also for empty files.
1 parent f642a27 commit 54c99cb

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/github3/repos/contents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _update_attributes(self, content):
8585
self.content = content.get("content")
8686
self.encoding = content.get("encoding")
8787
self.decoded = self.content
88-
if self.encoding == "base64" and self.content:
88+
if self.encoding == "base64" and self.content is not None:
8989
self.decoded = b64decode(self.content.encode())
9090
self.download_url = content["download_url"]
9191
self.git_url = content["git_url"]

tests/unit/test_repos_repo.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,30 @@ def test_file_contents(self):
582582
url_for("contents/path/to/file.txt"), params={"ref": "some-sha"}
583583
)
584584

585+
def test_file_contents_works_for_empty_file(self):
586+
"""Verify file_contents works for an empty file edge case."""
587+
mock_response = unittest.mock.MagicMock(status_code=200)
588+
mock_response.json.return_value = {
589+
"content": "",
590+
"download_url": "",
591+
"encoding": "base64",
592+
"git_url": "",
593+
"html_url": "",
594+
"name": "",
595+
"path": "",
596+
"sha": "",
597+
"size": "",
598+
"type": "",
599+
"url": "",
600+
"_links": "",
601+
}
602+
self.session.get.return_value = mock_response
603+
604+
file_contents = self.instance.file_contents(
605+
"path/to/empty_file.txt", ref="some-sha"
606+
)
607+
assert file_contents.decoded == b""
608+
585609
def test_git_commit_required_sha(self):
586610
"""Verify the request for retrieving a git commit from a repository."""
587611
self.instance.git_commit("")

0 commit comments

Comments
 (0)