Skip to content

Commit

Permalink
file.py: added encodings params so it can be tested
Browse files Browse the repository at this point in the history
test_file.py, extended TestSameContent TestCase with test_utf8_bom_content_decoding
  • Loading branch information
Mate Laszlo Valko committed Apr 27, 2024
1 parent 31e0238 commit 0469719
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 4 additions & 2 deletions frappe/core/doctype/file/file.py
Expand Up @@ -515,7 +515,7 @@ def unzip(self) -> list["File"]:
def exists_on_disk(self):
return os.path.exists(self.get_full_path())

def get_content(self) -> bytes:
def get_content(self, encodings=None) -> bytes | str:
if self.is_folder:
frappe.throw(_("Cannot get file contents of a Folder"))

Expand All @@ -531,10 +531,12 @@ def get_content(self) -> bytes:
self.validate_file_url()
file_path = self.get_full_path()

if encodings is None:
encodings = ["utf-8-sig", "utf-8", "windows-1250", "windows-1252"]
# read file with proper encoding
with open(file_path, mode="rb") as f:
self._content = f.read()
encodings = ["utf-8-sig", "utf-8", "windows-1250", "windows-1252"]

for encoding in encodings:
try:
# for plain text files
Expand Down
23 changes: 21 additions & 2 deletions frappe/core/doctype/file/test_file.py
Expand Up @@ -111,7 +111,7 @@ class TestBase64File(FrappeTestCase):
def setUp(self):
self.attached_to_doctype, self.attached_to_docname = make_test_doc()
self.test_content = base64.b64encode(test_content1.encode("utf-8"))
_file: "File" = frappe.get_doc(
_file: frappe.Document = frappe.get_doc(
{
"doctype": "File",
"file_name": "test_base64.txt",
Expand All @@ -125,7 +125,7 @@ def setUp(self):
self.saved_file_url = _file.file_url

def test_saved_content(self):
_file = frappe.get_doc("File", {"file_url": self.saved_file_url})
_file: frappe.Document = frappe.get_doc("File", {"file_url": self.saved_file_url})
content = _file.get_content()
self.assertEqual(content, test_content1)

Expand Down Expand Up @@ -255,6 +255,25 @@ def test_attachment_limit(self):
limit_property.delete()
frappe.clear_cache(doctype="ToDo")

def test_utf8_bom_content_decoding(self):
utf8_bom_content = test_content1.encode("utf-8-sig")
_file: frappe.Document = frappe.get_doc(
{
"doctype": "File",
"file_name": "utf8bom.txt",
"attached_to_doctype": self.attached_to_doctype1,
"attached_to_name": self.attached_to_docname1,
"content": utf8_bom_content,
"decode": False,
}
)
_file.save()
saved_file = frappe.get_doc("File", _file.name)
file_content_decoded = saved_file.get_content(encodings=["utf-8"])
self.assertEqual(file_content_decoded[0], "\ufeff")
file_content_properly_decoded = saved_file.get_content(encodings=["utf-8-sig", "utf-8"])
self.assertEqual(file_content_properly_decoded, test_content1)


class TestFile(FrappeTestCase):
def setUp(self):
Expand Down

0 comments on commit 0469719

Please sign in to comment.