Skip to content

Commit 935b189

Browse files
committed
Replace UTF-16 BOM handling with a more direct helper.
Avoids mypy errors when TestCase.assertEndsWith begins to exist with a different signature. Closes #334.
1 parent 4049971 commit 935b189

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

importlib_resources/tests/test_functional.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ def _gen_resourcetxt_path_parts(self):
3838
with self.subTest(path_parts=path_parts):
3939
yield path_parts
4040

41-
def assertEndsWith(self, string, suffix):
42-
"""Assert that `string` ends with `suffix`.
41+
@staticmethod
42+
def remove_utf16_bom(string):
43+
"""Remove an architecture-specific UTF-16 BOM prefix when present.
4344
44-
Used to ignore an architecture-specific UTF-16 byte-order mark."""
45-
self.assertEqual(string[-len(suffix) :], suffix)
45+
Some platforms surface UTF-16 BOM bytes as escaped text when the
46+
fixture is intentionally decoded as UTF-8 with ``errors='backslashreplace'``.
47+
Strip that prefix so assertions validate content consistently."""
48+
for bom in ('\\xff\\xfe', '\\xfe\\xff', '\ufeff'):
49+
if string.startswith(bom):
50+
return string[len(bom) :]
51+
return string
4652

4753
def test_read_text(self):
4854
self.assertEqual(
@@ -84,11 +90,13 @@ def test_read_text(self):
8490
),
8591
'\x00\x01\x02\x03',
8692
)
87-
self.assertEndsWith( # ignore the BOM
88-
resources.read_text(
89-
self.anchor01,
90-
'utf-16.file',
91-
errors='backslashreplace',
93+
self.assertEqual(
94+
self.remove_utf16_bom(
95+
resources.read_text(
96+
self.anchor01,
97+
'utf-16.file',
98+
errors='backslashreplace',
99+
),
92100
),
93101
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
94102
errors='backslashreplace',
@@ -136,8 +144,8 @@ def test_open_text(self):
136144
'utf-16.file',
137145
errors='backslashreplace',
138146
) as f:
139-
self.assertEndsWith( # ignore the BOM
140-
f.read(),
147+
self.assertEqual(
148+
self.remove_utf16_bom(f.read()),
141149
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
142150
errors='backslashreplace',
143151
),

0 commit comments

Comments
 (0)