• 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            # pathlib.Path.read_text() was introduced in Python 3.5.
21            with path.open('r', encoding='utf-8') as file:
22                text = file.read()
23            self.assertEqual('Hello, UTF-8 world!\n', text)
24
25
26class PathDiskTests(PathTests, unittest.TestCase):
27    data = data01
28
29
30class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
31    def test_remove_in_context_manager(self):
32        # It is not an error if the file that was temporarily stashed on the
33        # file system is removed inside the `with` stanza.
34        with resources.path(self.data, 'utf-8.file') as path:
35            path.unlink()
36
37
38if __name__ == '__main__':
39    unittest.main()
40