[ie/soundcloud] Various fixes (#11820)
[yt-dlp.git] / test / test_plugins.py
blob77545d136ceb16249e709e91cc960d0d0ffcf5f3
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.utils import Config
14 from yt_dlp.plugins import PACKAGE_NAME, directories, load_plugins
17 class TestPlugins(unittest.TestCase):
19 TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME
21 def test_directories_containing_plugins(self):
22 self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories()))
24 def test_extractor_classes(self):
25 for module_name in tuple(sys.modules):
26 if module_name.startswith(f'{PACKAGE_NAME}.extractor'):
27 del sys.modules[module_name]
28 plugins_ie = load_plugins('extractor', 'IE')
30 self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
31 self.assertIn('NormalPluginIE', plugins_ie.keys())
33 # don't load modules with underscore prefix
34 self.assertFalse(
35 f'{PACKAGE_NAME}.extractor._ignore' in sys.modules,
36 'loaded module beginning with underscore')
37 self.assertNotIn('IgnorePluginIE', plugins_ie.keys())
39 # Don't load extractors with underscore prefix
40 self.assertNotIn('_IgnoreUnderscorePluginIE', plugins_ie.keys())
42 # Don't load extractors not specified in __all__ (if supplied)
43 self.assertNotIn('IgnoreNotInAllPluginIE', plugins_ie.keys())
44 self.assertIn('InAllPluginIE', plugins_ie.keys())
46 def test_postprocessor_classes(self):
47 plugins_pp = load_plugins('postprocessor', 'PP')
48 self.assertIn('NormalPluginPP', plugins_pp.keys())
50 def test_importing_zipped_module(self):
51 zip_path = TEST_DATA_DIR / 'zipped_plugins.zip'
52 shutil.make_archive(str(zip_path)[:-4], 'zip', str(zip_path)[:-4])
53 sys.path.append(str(zip_path)) # add zip to search paths
54 importlib.invalidate_caches() # reset the import caches
56 try:
57 for plugin_type in ('extractor', 'postprocessor'):
58 package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}')
59 self.assertIn(zip_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__))
61 plugins_ie = load_plugins('extractor', 'IE')
62 self.assertIn('ZippedPluginIE', plugins_ie.keys())
64 plugins_pp = load_plugins('postprocessor', 'PP')
65 self.assertIn('ZippedPluginPP', plugins_pp.keys())
67 finally:
68 sys.path.remove(str(zip_path))
69 os.remove(zip_path)
70 importlib.invalidate_caches() # reset the import caches
72 def test_plugin_dirs(self):
73 # Internal plugin dirs hack for CLI --plugin-dirs
74 # To be replaced with proper system later
75 custom_plugin_dir = TEST_DATA_DIR / 'plugin_packages'
76 Config._plugin_dirs = [str(custom_plugin_dir)]
77 importlib.invalidate_caches() # reset the import caches
79 try:
80 package = importlib.import_module(f'{PACKAGE_NAME}.extractor')
81 self.assertIn(custom_plugin_dir / 'testpackage' / PACKAGE_NAME / 'extractor', map(Path, package.__path__))
83 plugins_ie = load_plugins('extractor', 'IE')
84 self.assertIn('PackagePluginIE', plugins_ie.keys())
86 finally:
87 Config._plugin_dirs = []
88 importlib.invalidate_caches() # reset the import caches
91 if __name__ == '__main__':
92 unittest.main()