1import os.path 2import sys 3import pathlib 4import unittest 5 6from importlib import import_module 7from importlib.readers import MultiplexedPath, NamespaceReader 8 9 10class MultiplexedPathTest(unittest.TestCase): 11 @classmethod 12 def setUpClass(cls): 13 path = pathlib.Path(__file__).parent / 'namespacedata01' 14 cls.folder = str(path) 15 16 def test_init_no_paths(self): 17 with self.assertRaises(FileNotFoundError): 18 MultiplexedPath() 19 20 def test_init_file(self): 21 with self.assertRaises(NotADirectoryError): 22 MultiplexedPath(os.path.join(self.folder, 'binary.file')) 23 24 def test_iterdir(self): 25 contents = {path.name for path in MultiplexedPath(self.folder).iterdir()} 26 try: 27 contents.remove('__pycache__') 28 except (KeyError, ValueError): 29 pass 30 self.assertEqual(contents, {'binary.file', 'utf-16.file', 'utf-8.file'}) 31 32 def test_iterdir_duplicate(self): 33 data01 = os.path.abspath(os.path.join(__file__, '..', 'data01')) 34 contents = { 35 path.name for path in MultiplexedPath(self.folder, data01).iterdir() 36 } 37 for remove in ('__pycache__', '__init__.pyc'): 38 try: 39 contents.remove(remove) 40 except (KeyError, ValueError): 41 pass 42 self.assertEqual( 43 contents, 44 {'__init__.py', 'binary.file', 'subdirectory', 'utf-16.file', 'utf-8.file'}, 45 ) 46 47 def test_is_dir(self): 48 self.assertEqual(MultiplexedPath(self.folder).is_dir(), True) 49 50 def test_is_file(self): 51 self.assertEqual(MultiplexedPath(self.folder).is_file(), False) 52 53 def test_open_file(self): 54 path = MultiplexedPath(self.folder) 55 with self.assertRaises(FileNotFoundError): 56 path.read_bytes() 57 with self.assertRaises(FileNotFoundError): 58 path.read_text() 59 with self.assertRaises(FileNotFoundError): 60 path.open() 61 62 def test_join_path(self): 63 prefix = os.path.abspath(os.path.join(__file__, '..')) 64 data01 = os.path.join(prefix, 'data01') 65 path = MultiplexedPath(self.folder, data01) 66 self.assertEqual( 67 str(path.joinpath('binary.file'))[len(prefix) + 1 :], 68 os.path.join('namespacedata01', 'binary.file'), 69 ) 70 self.assertEqual( 71 str(path.joinpath('subdirectory'))[len(prefix) + 1 :], 72 os.path.join('data01', 'subdirectory'), 73 ) 74 self.assertEqual( 75 str(path.joinpath('imaginary'))[len(prefix) + 1 :], 76 os.path.join('namespacedata01', 'imaginary'), 77 ) 78 79 def test_repr(self): 80 self.assertEqual( 81 repr(MultiplexedPath(self.folder)), 82 f"MultiplexedPath('{self.folder}')", 83 ) 84 85 def test_name(self): 86 self.assertEqual( 87 MultiplexedPath(self.folder).name, 88 os.path.basename(self.folder), 89 ) 90 91 92class NamespaceReaderTest(unittest.TestCase): 93 site_dir = str(pathlib.Path(__file__).parent) 94 95 @classmethod 96 def setUpClass(cls): 97 sys.path.append(cls.site_dir) 98 99 @classmethod 100 def tearDownClass(cls): 101 sys.path.remove(cls.site_dir) 102 103 def test_init_error(self): 104 with self.assertRaises(ValueError): 105 NamespaceReader(['path1', 'path2']) 106 107 def test_resource_path(self): 108 namespacedata01 = import_module('namespacedata01') 109 reader = NamespaceReader(namespacedata01.__spec__.submodule_search_locations) 110 111 root = os.path.abspath(os.path.join(__file__, '..', 'namespacedata01')) 112 self.assertEqual( 113 reader.resource_path('binary.file'), os.path.join(root, 'binary.file') 114 ) 115 self.assertEqual( 116 reader.resource_path('imaginary'), os.path.join(root, 'imaginary') 117 ) 118 119 def test_files(self): 120 namespacedata01 = import_module('namespacedata01') 121 reader = NamespaceReader(namespacedata01.__spec__.submodule_search_locations) 122 root = os.path.abspath(os.path.join(__file__, '..', 'namespacedata01')) 123 self.assertIsInstance(reader.files(), MultiplexedPath) 124 self.assertEqual(repr(reader.files()), f"MultiplexedPath('{root}')") 125 126 127if __name__ == '__main__': 128 unittest.main() 129