codekingpro/portable-devtools
115k
1import io2import pathlib3import unittest4 5import importlib_resources as resources6 7from . import util8 9 10class CommonTests(util.CommonTests, unittest.TestCase):11 def execute(self, package, path):12 with resources.as_file(resources.files(package).joinpath(path)):13 pass14 15 16class PathTests:17 def test_reading(self):18 """19 Path should be readable and a pathlib.Path instance.20 """21 target = resources.files(self.data) / 'utf-8.file'22 with resources.as_file(target) as path:23 assert isinstance(path, pathlib.Path)24 assert path.name.endswith("utf-8.file"), repr(path)25 assert 'Hello, UTF-8 world!\n' == path.read_text(encoding='utf-8')26 27 28class PathDiskTests(PathTests, util.DiskSetup, unittest.TestCase):29 def test_natural_path(self):30 """31 Guarantee the internal implementation detail that32 file-system-backed resources do not get the tempdir33 treatment.34 """35 target = resources.files(self.data) / 'utf-8.file'36 with resources.as_file(target) as path:37 assert 'data' in str(path)38 39 40class PathMemoryTests(PathTests, unittest.TestCase):41 def setUp(self):42 file = io.BytesIO(b'Hello, UTF-8 world!\n')43 self.addCleanup(file.close)44 self.data = util.create_package(45 file=file, path=FileNotFoundError("package exists only in memory")46 )47 self.data.__spec__.origin = None48 self.data.__spec__.has_location = False49 50 51class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):52 def test_remove_in_context_manager(self):53 """54 It is not an error if the file that was temporarily stashed on the55 file system is removed inside the `with` stanza.56 """57 target = resources.files(self.data) / 'utf-8.file'58 with resources.as_file(target) as path:59 path.unlink()60 61 62if __name__ == '__main__':63 unittest.main()64 