]>
Commit | Line | Data |
---|---|---|
8e40b9d1 M |
1 | import importlib |
2 | import os | |
3 | import shutil | |
4 | import sys | |
5 | import unittest | |
6 | from pathlib import Path | |
7 | ||
8 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
9 | TEST_DATA_DIR = Path(os.path.dirname(os.path.abspath(__file__)), 'testdata') | |
10 | sys.path.append(str(TEST_DATA_DIR)) | |
11 | importlib.invalidate_caches() | |
12 | ||
13 | from yt_dlp.plugins import PACKAGE_NAME, directories, load_plugins | |
14 | ||
15 | ||
16 | class TestPlugins(unittest.TestCase): | |
17 | ||
18 | TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME | |
19 | ||
20 | def test_directories_containing_plugins(self): | |
21 | self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories())) | |
22 | ||
23 | def test_extractor_classes(self): | |
24 | for module_name in tuple(sys.modules): | |
25 | if module_name.startswith(f'{PACKAGE_NAME}.extractor'): | |
26 | del sys.modules[module_name] | |
27 | plugins_ie = load_plugins('extractor', 'IE') | |
28 | ||
29 | self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys()) | |
30 | self.assertIn('NormalPluginIE', plugins_ie.keys()) | |
31 | ||
32 | # don't load modules with underscore prefix | |
33 | self.assertFalse( | |
34 | f'{PACKAGE_NAME}.extractor._ignore' in sys.modules.keys(), | |
35 | 'loaded module beginning with underscore') | |
36 | self.assertNotIn('IgnorePluginIE', plugins_ie.keys()) | |
37 | ||
38 | # Don't load extractors with underscore prefix | |
39 | self.assertNotIn('_IgnoreUnderscorePluginIE', plugins_ie.keys()) | |
40 | ||
41 | # Don't load extractors not specified in __all__ (if supplied) | |
42 | self.assertNotIn('IgnoreNotInAllPluginIE', plugins_ie.keys()) | |
43 | self.assertIn('InAllPluginIE', plugins_ie.keys()) | |
44 | ||
45 | def test_postprocessor_classes(self): | |
46 | plugins_pp = load_plugins('postprocessor', 'PP') | |
47 | self.assertIn('NormalPluginPP', plugins_pp.keys()) | |
48 | ||
49 | def test_importing_zipped_module(self): | |
50 | zip_path = TEST_DATA_DIR / 'zipped_plugins.zip' | |
51 | shutil.make_archive(str(zip_path)[:-4], 'zip', str(zip_path)[:-4]) | |
52 | sys.path.append(str(zip_path)) # add zip to search paths | |
53 | importlib.invalidate_caches() # reset the import caches | |
54 | ||
55 | try: | |
56 | for plugin_type in ('extractor', 'postprocessor'): | |
57 | package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}') | |
58 | self.assertIn(zip_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__)) | |
59 | ||
60 | plugins_ie = load_plugins('extractor', 'IE') | |
61 | self.assertIn('ZippedPluginIE', plugins_ie.keys()) | |
62 | ||
63 | plugins_pp = load_plugins('postprocessor', 'PP') | |
64 | self.assertIn('ZippedPluginPP', plugins_pp.keys()) | |
65 | ||
66 | finally: | |
67 | sys.path.remove(str(zip_path)) | |
68 | os.remove(zip_path) | |
69 | importlib.invalidate_caches() # reset the import caches | |
70 | ||
71 | ||
72 | if __name__ == '__main__': | |
73 | unittest.main() |