• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2
3from importlib import resources
4from . import data01
5from . import util
6
7
8class CommonTests(util.CommonResourceTests, unittest.TestCase):
9    def execute(self, package, path):
10        with resources.path(package, path):
11            pass
12
13
14class PathTests:
15    def test_reading(self):
16        # Path should be readable.
17        # Test also implicitly verifies the returned object is a pathlib.Path
18        # instance.
19        with resources.path(self.data, 'utf-8.file') as path:
20            self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
21            # pathlib.Path.read_text() was introduced in Python 3.5.
22            with path.open('r', encoding='utf-8') as file:
23                text = file.read()
24            self.assertEqual('Hello, UTF-8 world!\n', text)
25
26
27class PathDiskTests(PathTests, unittest.TestCase):
28    data = data01
29
30
31class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
32    def test_remove_in_context_manager(self):
33        # It is not an error if the file that was temporarily stashed on the
34        # file system is removed inside the `with` stanza.
35        with resources.path(self.data, 'utf-8.file') as path:
36            path.unlink()
37
38
39if __name__ == '__main__':
40    unittest.main()
41