Release 2023.12.30
[yt-dlp.git] / test / test_plugins.py
blob6cde579e1e9c1bbba2c159a19330a78eaed6e7c6
1 import importlib
2 import os
3 import shutil
4 import sys
5 import unittest
6 from pathlib import Path
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()
13 from yt_dlp.plugins import PACKAGE_NAME, directories, load_plugins
16 class TestPlugins(unittest.TestCase):
18 TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME
20 def test_directories_containing_plugins(self):
21 self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories()))
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')
29 self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
30 self.assertIn('NormalPluginIE', plugins_ie.keys())
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())
38 # Don't load extractors with underscore prefix
39 self.assertNotIn('_IgnoreUnderscorePluginIE', plugins_ie.keys())
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())
45 def test_postprocessor_classes(self):
46 plugins_pp = load_plugins('postprocessor', 'PP')
47 self.assertIn('NormalPluginPP', plugins_pp.keys())
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
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__))
60 plugins_ie = load_plugins('extractor', 'IE')
61 self.assertIn('ZippedPluginIE', plugins_ie.keys())
63 plugins_pp = load_plugins('postprocessor', 'PP')
64 self.assertIn('ZippedPluginPP', plugins_pp.keys())
66 finally:
67 sys.path.remove(str(zip_path))
68 os.remove(zip_path)
69 importlib.invalidate_caches() # reset the import caches
72 if __name__ == '__main__':
73 unittest.main()