Skip to content

Commit 6f35cd3

Browse files
committed
Port test_read to traversable API.
1 parent d87a8dd commit 6f35cd3

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

importlib_resources/tests/test_read.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,36 @@
88

99
class CommonBinaryTests(util.CommonTests, unittest.TestCase):
1010
def execute(self, package, path):
11-
with util.suppress_known_deprecation():
12-
resources.read_binary(package, path)
11+
resources.files(package).joinpath(path).read_bytes()
1312

1413

1514
class CommonTextTests(util.CommonTests, unittest.TestCase):
1615
def execute(self, package, path):
17-
with util.suppress_known_deprecation():
18-
resources.read_text(package, path)
16+
resources.files(package).joinpath(path).read_text()
1917

2018

2119
class ReadTests:
22-
def test_read_binary(self):
23-
with util.suppress_known_deprecation():
24-
result = resources.read_binary(self.data, 'binary.file')
20+
def test_read_bytes(self):
21+
result = resources.files(self.data).joinpath('binary.file').read_bytes()
2522
self.assertEqual(result, b'\0\1\2\3')
2623

2724
def test_read_text_default_encoding(self):
28-
with util.suppress_known_deprecation():
29-
result = resources.read_text(self.data, 'utf-8.file')
25+
result = resources.files(self.data).joinpath('utf-8.file').read_text()
3026
self.assertEqual(result, 'Hello, UTF-8 world!\n')
3127

3228
def test_read_text_given_encoding(self):
33-
with util.suppress_known_deprecation():
34-
result = resources.read_text(self.data, 'utf-16.file', encoding='utf-16')
29+
result = (
30+
resources.files(self.data)
31+
.joinpath('utf-16.file')
32+
.read_text(encoding='utf-16')
33+
)
3534
self.assertEqual(result, 'Hello, UTF-16 world!\n')
3635

3736
def test_read_text_with_errors(self):
3837
# Raises UnicodeError without the 'errors' argument.
39-
with util.suppress_known_deprecation():
40-
self.assertRaises(
41-
UnicodeError, resources.read_text, self.data, 'utf-16.file'
42-
)
43-
with util.suppress_known_deprecation():
44-
result = resources.read_text(self.data, 'utf-16.file', errors='ignore')
38+
target = resources.files(self.data) / 'utf-16.file'
39+
self.assertRaises(UnicodeError, target.read_text, encoding='utf-8')
40+
result = target.read_text(encoding='utf-8', errors='ignore')
4541
self.assertEqual(
4642
result,
4743
'H\x00e\x00l\x00l\x00o\x00,\x00 '
@@ -57,13 +53,15 @@ class ReadDiskTests(ReadTests, unittest.TestCase):
5753
class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
5854
def test_read_submodule_resource(self):
5955
submodule = import_module('ziptestdata.subdirectory')
60-
with util.suppress_known_deprecation():
61-
result = resources.read_binary(submodule, 'binary.file')
56+
result = resources.files(submodule).joinpath('binary.file').read_bytes()
6257
self.assertEqual(result, b'\0\1\2\3')
6358

6459
def test_read_submodule_resource_by_name(self):
65-
with util.suppress_known_deprecation():
66-
result = resources.read_binary('ziptestdata.subdirectory', 'binary.file')
60+
result = (
61+
resources.files('ziptestdata.subdirectory')
62+
.joinpath('binary.file')
63+
.read_bytes()
64+
)
6765
self.assertEqual(result, b'\0\1\2\3')
6866

6967

0 commit comments

Comments
 (0)