From cd490eeeab5e9bf6a102d5253591cac78565f4ef Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Sat, 30 Nov 2024 14:15:19 +1300 Subject: [PATCH] Improve error message when invalid directory provided --- test/test_plugins.py | 46 ++++++++++++++++++++++++++++++++------------ yt_dlp/plugins.py | 5 ++++- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/test/test_plugins.py b/test/test_plugins.py index 2ced31520..7d01cd542 100644 --- a/test/test_plugins.py +++ b/test/test_plugins.py @@ -21,6 +21,7 @@ from yt_dlp.plugins import ( set_plugin_dirs, disable_plugins, add_plugin_dirs, + get_plugin_spec, ) from yt_dlp._globals import ( @@ -50,23 +51,31 @@ POSTPROCESSOR_PLUGIN_SPEC = PluginSpec( ) +def reset_plugins(): + plugin_ies.value = {} + plugin_pps.value = {} + plugin_dirs.value = ['external'] + plugin_specs.value = {} + all_plugins_loaded.value = False + plugins_enabled.value = True + # Clearing override plugins is probably difficult + for module_name in tuple(sys.modules): + for plugin_type in ('extractor', 'postprocessor'): + if module_name.startswith(f'{PACKAGE_NAME}.{plugin_type}.'): + del sys.modules[module_name] + + importlib.invalidate_caches() + + class TestPlugins(unittest.TestCase): TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME def setUp(self): - plugin_ies.value = {} - plugin_pps.value = {} - plugin_dirs.value = ['external'] - plugin_specs.value = {} - all_plugins_loaded.value = False - plugins_enabled.value = True - importlib.invalidate_caches() - # Clearing override plugins is probably difficult - for module_name in tuple(sys.modules): - for plugin_type in ('extractor', 'postprocessor'): - if module_name.startswith(f'{PACKAGE_NAME}.{plugin_type}.'): - del sys.modules[module_name] + reset_plugins() + + def tearDown(self): + reset_plugins() def test_directories_containing_plugins(self): self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories())) @@ -207,6 +216,11 @@ class TestPlugins(unittest.TestCase): self.assertIn(f'{PACKAGE_NAME}.extractor.package', sys.modules.keys()) self.assertIn('PackagePluginIE', plugin_ies.value) + def test_invalid_plugin_dir(self): + set_plugin_dirs('invalid_dir') + with self.assertRaises(ValueError): + load_plugins(EXTRACTOR_PLUGIN_SPEC) + def test_add_plugin_dirs(self): custom_plugin_dir = str(TEST_DATA_DIR / 'plugin_packages') @@ -244,6 +258,14 @@ class TestPlugins(unittest.TestCase): ies = load_plugins(EXTRACTOR_PLUGIN_SPEC) self.assertIn('NormalPluginIE', ies) + def test_get_plugin_spec(self): + register_plugin_spec(EXTRACTOR_PLUGIN_SPEC) + register_plugin_spec(POSTPROCESSOR_PLUGIN_SPEC) + + self.assertEqual(get_plugin_spec('extractor'), EXTRACTOR_PLUGIN_SPEC) + self.assertEqual(get_plugin_spec('postprocessor'), POSTPROCESSOR_PLUGIN_SPEC) + self.assertIsNone(get_plugin_spec('invalid')) + if __name__ == '__main__': unittest.main() diff --git a/yt_dlp/plugins.py b/yt_dlp/plugins.py index 2e88579e1..327aa2669 100644 --- a/yt_dlp/plugins.py +++ b/yt_dlp/plugins.py @@ -118,7 +118,10 @@ def internal_plugin_paths(): def candidate_plugin_paths(candidate): - yield from Path(candidate).iterdir() + candidate_path = Path(candidate) + if not candidate_path.is_dir(): + raise ValueError(f'Invalid plugin directory: {candidate_path}') + yield from candidate_path.iterdir() yield from internal_plugin_paths()