Debian Codegraph @

Lib/test/test_importlib/resources/test_path.py

raw
1 import io
2 import pathlib
3 import unittest
4
5 from importlib import resources
6 from . import util
7
8
9 class CommonTests(util.CommonTests, unittest.TestCase):
10 def execute(self, package, path):
11 with resources.as_file(resources.files(package).joinpath(path)):
12 pass
13
14
15 class PathTests:
16 def test_reading(self):
17 """
18 Path should be readable and a pathlib.Path instance.
19 """
20 target = resources.files(self.data) / 'utf-8.file'
21 with resources.as_file(target) as path:
22 self.assertIsInstance(path, pathlib.Path)
23 self.assertEndsWith(path.name, "utf-8.file")
24 self.assertEqual('Hello, UTF-8 world!\n', path.read_text(encoding='utf-8'))
25
26
27 class PathDiskTests(PathTests, util.DiskSetup, unittest.TestCase):
28 def test_natural_path(self):
29 # Guarantee the internal implementation detail that
30 # file-system-backed resources do not get the tempdir
31 # treatment.
32 target = resources.files(self.data) / 'utf-8.file'
33 with resources.as_file(target) as path:
34 assert 'data' in str(path)
35
36
37 class PathMemoryTests(PathTests, unittest.TestCase):
38 def setUp(self):
39 file = io.BytesIO(b'Hello, UTF-8 world!\n')
40 self.addCleanup(file.close)
41 self.data = util.create_package(
42 file=file, path=FileNotFoundError("package exists only in memory")
43 )
44 self.data.__spec__.origin = None
45 self.data.__spec__.has_location = False
46
47
48 class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
49 def test_remove_in_context_manager(self):
50 """
51 It is not an error if the file that was temporarily stashed on the
52 file system is removed inside the `with` stanza.
53 """
54 target = resources.files(self.data) / 'utf-8.file'
55 with resources.as_file(target) as path:
56 path.unlink()
57
58
59 if __name__ == '__main__':
60 unittest.main()