codekingpro/portable-devtools
114k
1import io2import unittest3 4import importlib_resources as resources5from importlib_resources._adapters import (6 CompatibilityFiles,7 wrap_spec,8)9 10from . import util11 12 13class CompatibilityFilesTests(unittest.TestCase):14 @property15 def package(self):16 bytes_data = io.BytesIO(b'Hello, world!')17 return util.create_package(18 file=bytes_data,19 path='some_path',20 contents=('a', 'b', 'c'),21 )22 23 @property24 def files(self):25 return resources.files(self.package)26 27 def test_spec_path_iter(self):28 assert sorted(path.name for path in self.files.iterdir()) == ['a', 'b', 'c']29 30 def test_child_path_iter(self):31 assert list((self.files / 'a').iterdir()) == []32 33 def test_orphan_path_iter(self):34 assert list((self.files / 'a' / 'a').iterdir()) == []35 assert list((self.files / 'a' / 'a' / 'a').iterdir()) == []36 37 def test_spec_path_is(self):38 assert not self.files.is_file()39 assert not self.files.is_dir()40 41 def test_child_path_is(self):42 assert (self.files / 'a').is_file()43 assert not (self.files / 'a').is_dir()44 45 def test_orphan_path_is(self):46 assert not (self.files / 'a' / 'a').is_file()47 assert not (self.files / 'a' / 'a').is_dir()48 assert not (self.files / 'a' / 'a' / 'a').is_file()49 assert not (self.files / 'a' / 'a' / 'a').is_dir()50 51 def test_spec_path_name(self):52 assert self.files.name == 'testingpackage'53 54 def test_child_path_name(self):55 assert (self.files / 'a').name == 'a'56 57 def test_orphan_path_name(self):58 assert (self.files / 'a' / 'b').name == 'b'59 assert (self.files / 'a' / 'b' / 'c').name == 'c'60 61 def test_spec_path_open(self):62 assert self.files.read_bytes() == b'Hello, world!'63 assert self.files.read_text(encoding='utf-8') == 'Hello, world!'64 65 def test_child_path_open(self):66 assert (self.files / 'a').read_bytes() == b'Hello, world!'67 assert (self.files / 'a').read_text(encoding='utf-8') == 'Hello, world!'68 69 def test_orphan_path_open(self):70 with self.assertRaises(FileNotFoundError):71 (self.files / 'a' / 'b').read_bytes()72 with self.assertRaises(FileNotFoundError):73 (self.files / 'a' / 'b' / 'c').read_bytes()74 75 def test_open_invalid_mode(self):76 with self.assertRaises(ValueError):77 self.files.open('0')78 79 def test_orphan_path_invalid(self):80 with self.assertRaises(ValueError):81 CompatibilityFiles.OrphanPath()82 83 def test_wrap_spec(self):84 spec = wrap_spec(self.package)85 assert isinstance(spec.loader.get_resource_reader(None), CompatibilityFiles)86 87 88class CompatibilityFilesNoReaderTests(unittest.TestCase):89 @property90 def package(self):91 return util.create_package_from_loader(None)92 93 @property94 def files(self):95 return resources.files(self.package)96 97 def test_spec_path_joinpath(self):98 assert isinstance(self.files / 'a', CompatibilityFiles.OrphanPath)99 