Skip to content

Commit 66ae1c2

Browse files
committed
tests: add tests for CompatibiltyFiles
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 16821da commit 66ae1c2

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import io
2+
import unittest
3+
4+
import importlib_resources as resources
5+
6+
from importlib_resources._adapters import CompatibilityFiles
7+
8+
from . import util
9+
10+
11+
class CompatibilityFilesTests(unittest.TestCase):
12+
@property
13+
def package(self):
14+
bytes_data = io.BytesIO(b'Hello, world!')
15+
return util.create_package(
16+
file=bytes_data,
17+
path='some_path',
18+
contents=('a', 'b', 'c'),
19+
)
20+
21+
@property
22+
def files(self):
23+
return resources.files(self.package)
24+
25+
def test_spec_path_iter(self):
26+
self.assertEqual(
27+
sorted(path.name for path in self.files.iterdir()),
28+
['a', 'b', 'c'],
29+
)
30+
31+
def test_child_path_iter(self):
32+
self.assertEqual(list((self.files / 'a').iterdir()), [])
33+
34+
def test_orphan_path_iter(self):
35+
self.assertEqual(list((self.files / 'a' / 'a').iterdir()), [])
36+
self.assertEqual(list((self.files / 'a' / 'a' / 'a').iterdir()), [])
37+
38+
def test_spec_path_is(self):
39+
self.assertFalse(self.files.is_file())
40+
self.assertFalse(self.files.is_dir())
41+
42+
def test_child_path_is(self):
43+
self.assertTrue((self.files / 'a').is_file())
44+
self.assertFalse((self.files / 'a').is_dir())
45+
46+
def test_orphan_path_is(self):
47+
self.assertFalse((self.files / 'a' / 'a').is_file())
48+
self.assertFalse((self.files / 'a' / 'a').is_dir())
49+
self.assertFalse((self.files / 'a' / 'a' / 'a').is_file())
50+
self.assertFalse((self.files / 'a' / 'a' / 'a').is_dir())
51+
52+
def test_spec_path_name(self):
53+
self.assertEqual(self.files.name, 'testingpackage')
54+
55+
def test_child_path_name(self):
56+
self.assertEqual((self.files / 'a').name, 'a')
57+
58+
def test_orphan_path_name(self):
59+
self.assertEqual((self.files / 'a' / 'b').name, 'b')
60+
self.assertEqual((self.files / 'a' / 'b' / 'c').name, 'c')
61+
62+
def test_spec_path_open(self):
63+
self.assertEqual(self.files.read_bytes(), b'Hello, world!')
64+
self.assertEqual(self.files.read_text(), 'Hello, world!')
65+
66+
def test_child_path_open(self):
67+
self.assertEqual((self.files / 'a').read_bytes(), b'Hello, world!')
68+
self.assertEqual((self.files / 'a').read_text(), 'Hello, world!')
69+
70+
def test_orphan_path_open(self):
71+
with self.assertRaises(FileNotFoundError):
72+
(self.files / 'a' / 'b').read_bytes()
73+
with self.assertRaises(FileNotFoundError):
74+
(self.files / 'a' / 'b' / 'c').read_bytes()
75+
76+
def test_open_invalid_mode(self):
77+
with self.assertRaises(ValueError):
78+
self.files.open('0')
79+
80+
def test_orphan_path_invalid(self):
81+
with self.assertRaises(ValueError):
82+
CompatibilityFiles.OrphanPath()
83+
84+
85+
class CompatibilityFilesNoReaderTests(unittest.TestCase):
86+
@property
87+
def package(self):
88+
return util.create_package_from_loader(None)
89+
90+
@property
91+
def files(self):
92+
return resources.files(self.package)
93+
94+
def test_spec_path_joinpath(self):
95+
self.assertIsInstance(self.files / 'a', CompatibilityFiles.OrphanPath)

importlib_resources/tests/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,22 @@ def contents(self):
5151
yield from self._contents
5252

5353

54-
def create_package(file, path, is_package=True, contents=()):
54+
def create_package_from_loader(loader, is_package=True):
5555
name = 'testingpackage'
5656
module = types.ModuleType(name)
57-
loader = Reader(file=file, path=path, _contents=contents)
5857
spec = ModuleSpec(name, loader, origin='does-not-exist', is_package=is_package)
5958
module.__spec__ = spec
6059
module.__loader__ = loader
6160
return module
6261

6362

63+
def create_package(file=None, path=None, is_package=True, contents=()):
64+
return create_package_from_loader(
65+
Reader(file=file, path=path, _contents=contents),
66+
is_package,
67+
)
68+
69+
6470
class CommonTests(metaclass=abc.ABCMeta):
6571
"""
6672
Tests shared by test_open, test_path, and test_read.

0 commit comments

Comments
 (0)