Skip to content

Commit bc6f0d1

Browse files
authored
Merge pull request #239 from python/feature/test-no-legacy
Replace tests of legacy API with comparable tests of traversable API.
2 parents 0ffdeed + 15073ca commit bc6f0d1

File tree

8 files changed

+136
-189
lines changed

8 files changed

+136
-189
lines changed

.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ omit =
33
# leading `*/` for pytest-dev/pytest-cov#456
44
*/.tox/*
55
*/_itertools.py
6+
*/_legacy.py
67

78
[report]
89
show_missing = True

CHANGES.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v5.4.0
2+
======
3+
4+
* *80: Test suite now relies entirely on the traversable
5+
API.
6+
17
v5.3.0
28
======
39

importlib_resources/tests/test_contents.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class ContentsTests:
1515
}
1616

1717
def test_contents(self):
18-
with util.suppress_known_deprecation():
19-
assert self.expected <= set(resources.contents(self.data))
18+
contents = {path.name for path in resources.files(self.data).iterdir()}
19+
assert self.expected <= contents
2020

2121

2222
class ContentsDiskTests(ContentsTests, unittest.TestCase):

importlib_resources/tests/test_open.py

+25-32
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,44 @@
77

88
class CommonBinaryTests(util.CommonTests, unittest.TestCase):
99
def execute(self, package, path):
10-
with util.suppress_known_deprecation():
11-
with resources.open_binary(package, path):
12-
pass
10+
target = resources.files(package).joinpath(path)
11+
with target.open('rb'):
12+
pass
1313

1414

1515
class CommonTextTests(util.CommonTests, unittest.TestCase):
1616
def execute(self, package, path):
17-
with util.suppress_known_deprecation():
18-
with resources.open_text(package, path):
19-
pass
17+
target = resources.files(package).joinpath(path)
18+
with target.open():
19+
pass
2020

2121

2222
class OpenTests:
2323
def test_open_binary(self):
24-
with util.suppress_known_deprecation():
25-
with resources.open_binary(self.data, 'binary.file') as fp:
26-
result = fp.read()
27-
self.assertEqual(result, b'\x00\x01\x02\x03')
24+
target = resources.files(self.data) / 'binary.file'
25+
with target.open('rb') as fp:
26+
result = fp.read()
27+
self.assertEqual(result, b'\x00\x01\x02\x03')
2828

2929
def test_open_text_default_encoding(self):
30-
with util.suppress_known_deprecation():
31-
with resources.open_text(self.data, 'utf-8.file') as fp:
32-
result = fp.read()
30+
target = resources.files(self.data) / 'utf-8.file'
31+
with target.open() as fp:
32+
result = fp.read()
3333
self.assertEqual(result, 'Hello, UTF-8 world!\n')
3434

3535
def test_open_text_given_encoding(self):
36-
with util.suppress_known_deprecation():
37-
with resources.open_text(
38-
self.data, 'utf-16.file', 'utf-16', 'strict'
39-
) as fp:
40-
result = fp.read()
36+
target = resources.files(self.data) / 'utf-16.file'
37+
with target.open(encoding='utf-16', errors='strict') as fp:
38+
result = fp.read()
4139
self.assertEqual(result, 'Hello, UTF-16 world!\n')
4240

4341
def test_open_text_with_errors(self):
4442
# Raises UnicodeError without the 'errors' argument.
45-
with util.suppress_known_deprecation():
46-
with resources.open_text(self.data, 'utf-16.file', 'utf-8', 'strict') as fp:
47-
self.assertRaises(UnicodeError, fp.read)
48-
with util.suppress_known_deprecation():
49-
with resources.open_text(self.data, 'utf-16.file', 'utf-8', 'ignore') as fp:
50-
result = fp.read()
43+
target = resources.files(self.data) / 'utf-16.file'
44+
with target.open(encoding='utf-8', errors='strict') as fp:
45+
self.assertRaises(UnicodeError, fp.read)
46+
with target.open(encoding='utf-8', errors='ignore') as fp:
47+
result = fp.read()
5148
self.assertEqual(
5249
result,
5350
'H\x00e\x00l\x00l\x00o\x00,\x00 '
@@ -56,16 +53,12 @@ def test_open_text_with_errors(self):
5653
)
5754

5855
def test_open_binary_FileNotFoundError(self):
59-
with util.suppress_known_deprecation():
60-
self.assertRaises(
61-
FileNotFoundError, resources.open_binary, self.data, 'does-not-exist'
62-
)
56+
target = resources.files(self.data) / 'does-not-exist'
57+
self.assertRaises(FileNotFoundError, target.open, 'rb')
6358

6459
def test_open_text_FileNotFoundError(self):
65-
with util.suppress_known_deprecation():
66-
self.assertRaises(
67-
FileNotFoundError, resources.open_text, self.data, 'does-not-exist'
68-
)
60+
target = resources.files(self.data) / 'does-not-exist'
61+
self.assertRaises(FileNotFoundError, target.open)
6962

7063

7164
class OpenDiskTests(OpenTests, unittest.TestCase):

importlib_resources/tests/test_path.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@
88

99
class CommonTests(util.CommonTests, unittest.TestCase):
1010
def execute(self, package, path):
11-
with util.suppress_known_deprecation():
12-
with resources.path(package, path):
13-
pass
11+
with resources.as_file(resources.files(package).joinpath(path)):
12+
pass
1413

1514

1615
class PathTests:
1716
def test_reading(self):
1817
# Path should be readable.
1918
# Test also implicitly verifies the returned object is a pathlib.Path
2019
# instance.
21-
with util.suppress_known_deprecation():
22-
with resources.path(self.data, 'utf-8.file') as path:
23-
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
24-
# pathlib.Path.read_text() was introduced in Python 3.5.
25-
with path.open('r', encoding='utf-8') as file:
26-
text = file.read()
27-
self.assertEqual('Hello, UTF-8 world!\n', text)
20+
target = resources.files(self.data) / 'utf-8.file'
21+
with resources.as_file(target) as path:
22+
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
23+
# pathlib.Path.read_text() was introduced in Python 3.5.
24+
with path.open('r', encoding='utf-8') as file:
25+
text = file.read()
26+
self.assertEqual('Hello, UTF-8 world!\n', text)
2827

2928

3029
class PathDiskTests(PathTests, unittest.TestCase):
@@ -36,9 +35,9 @@ def test_natural_path(self):
3635
file-system-backed resources do not get the tempdir
3736
treatment.
3837
"""
39-
with util.suppress_known_deprecation():
40-
with resources.path(self.data, 'utf-8.file') as path:
41-
assert 'data' in str(path)
38+
target = resources.files(self.data) / 'utf-8.file'
39+
with resources.as_file(target) as path:
40+
assert 'data' in str(path)
4241

4342

4443
class PathMemoryTests(PathTests, unittest.TestCase):
@@ -56,9 +55,9 @@ class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
5655
def test_remove_in_context_manager(self):
5756
# It is not an error if the file that was temporarily stashed on the
5857
# file system is removed inside the `with` stanza.
59-
with util.suppress_known_deprecation():
60-
with resources.path(self.data, 'utf-8.file') as path:
61-
path.unlink()
58+
target = resources.files(self.data) / 'utf-8.file'
59+
with resources.as_file(target) as path:
60+
path.unlink()
6261

6362

6463
if __name__ == '__main__':

importlib_resources/tests/test_read.py

+19-21
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)