Home | History | Annotate | Download | only in test_importlib
      1 import unittest
      2 
      3 from importlib import resources
      4 from . import data01
      5 from . import util
      6 
      7 
      8 class CommonTests(util.CommonResourceTests, unittest.TestCase):
      9     def execute(self, package, path):
     10         with resources.path(package, path):
     11             pass
     12 
     13 
     14 class 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 
     26 class PathDiskTests(PathTests, unittest.TestCase):
     27     data = data01
     28 
     29 
     30 class 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 
     38 if __name__ == '__main__':
     39     unittest.main()
     40