test_plugins.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.plugins import PACKAGE_NAME, directories, load_plugins
  12. class TestPlugins(unittest.TestCase):
  13. TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME
  14. def test_directories_containing_plugins(self):
  15. self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories()))
  16. def test_extractor_classes(self):
  17. for module_name in tuple(sys.modules):
  18. if module_name.startswith(f'{PACKAGE_NAME}.extractor'):
  19. del sys.modules[module_name]
  20. plugins_ie = load_plugins('extractor', 'IE')
  21. self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
  22. self.assertIn('NormalPluginIE', plugins_ie.keys())
  23. # don't load modules with underscore prefix
  24. self.assertFalse(
  25. f'{PACKAGE_NAME}.extractor._ignore' in sys.modules,
  26. 'loaded module beginning with underscore')
  27. self.assertNotIn('IgnorePluginIE', plugins_ie.keys())
  28. # Don't load extractors with underscore prefix
  29. self.assertNotIn('_IgnoreUnderscorePluginIE', plugins_ie.keys())
  30. # Don't load extractors not specified in __all__ (if supplied)
  31. self.assertNotIn('IgnoreNotInAllPluginIE', plugins_ie.keys())
  32. self.assertIn('InAllPluginIE', plugins_ie.keys())
  33. def test_postprocessor_classes(self):
  34. plugins_pp = load_plugins('postprocessor', 'PP')
  35. self.assertIn('NormalPluginPP', plugins_pp.keys())
  36. def test_importing_zipped_module(self):
  37. zip_path = TEST_DATA_DIR / 'zipped_plugins.zip'
  38. shutil.make_archive(str(zip_path)[:-4], 'zip', str(zip_path)[:-4])
  39. sys.path.append(str(zip_path)) # add zip to search paths
  40. importlib.invalidate_caches() # reset the import caches
  41. try:
  42. for plugin_type in ('extractor', 'postprocessor'):
  43. package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}')
  44. self.assertIn(zip_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__))
  45. plugins_ie = load_plugins('extractor', 'IE')
  46. self.assertIn('ZippedPluginIE', plugins_ie.keys())
  47. plugins_pp = load_plugins('postprocessor', 'PP')
  48. self.assertIn('ZippedPluginPP', plugins_pp.keys())
  49. finally:
  50. sys.path.remove(str(zip_path))
  51. os.remove(zip_path)
  52. importlib.invalidate_caches() # reset the import caches
  53. if __name__ == '__main__':
  54. unittest.main()