test_plugins.py 3.5 KB

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