Repository: yt-dlp/yt-dlp
File: test/test_plugins.py (Python)

Code before:
        zip_path = TEST_DATA_DIR / 'zipped_plugins.zip'
        shutil.make_archive(str(zip_path)[:-4], 'zip', str(zip_path)[:-4])
        sys.path.append(str(zip_path))  # add zip to search paths
        importlib.invalidate_caches()  # reset the import caches

        try:
            for plugin_type in ('extractor', 'postprocessor'):
                package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}')
                self.assertIn(zip_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__))

            plugins_ie = load_plugins('extractor', 'IE')
            self.assertIn('ZippedPluginIE', plugins_ie.keys())

            plugins_pp = load_plugins('postprocessor', 'PP')
            self.assertIn('ZippedPluginPP', plugins_pp.keys())

        finally:
            sys.path.remove(str(zip_path))
            os.remove(zip_path)
            importlib.invalidate_caches()  # reset the import caches

    def test_plugin_locations(self):
        # Internal plugin locations hack for CLI --plugin-dirs
        # To be replaced with proper system later
        custom_plugin_locations_path = TEST_DATA_DIR / 'plugin_packages'
        Config._plugin_dirs = [str(custom_plugin_locations_path)]
        importlib.invalidate_caches()  # reset the import caches

        try:
            package = importlib.import_module(f'{PACKAGE_NAME}.extractor')
            self.assertIn(custom_plugin_locations_path / 'testpackage' / PACKAGE_NAME / 'extractor', map(Path, package.__path__))

            plugins_ie = load_plugins('extractor', 'IE')
            self.assertIn('PackagePluginIE', plugins_ie.keys())

        finally:
            Config._plugin_dirs = []
            importlib.invalidate_caches()  # reset the import caches


if __name__ == '__main__':
    unittest.main()


Review comment (suggestion):
```suggestion
        # Internal plugin dirs hack for CLI --plugin-dirs
        # To be replaced with proper system later
        custom_plugin_dir = TEST_DATA_DIR / 'plugin_packages'
        Config._plugin_dirs = [str(custom_plugin_dir)]
```

Code after:
    def test_importing_zipped_module(self):
        zip_path = TEST_DATA_DIR / 'zipped_plugins.zip'
        shutil.make_archive(str(zip_path)[:-4], 'zip', str(zip_path)[:-4])
        sys.path.append(str(zip_path))  # add zip to search paths
        importlib.invalidate_caches()  # reset the import caches

        try:
            for plugin_type in ('extractor', 'postprocessor'):
                package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}')
                self.assertIn(zip_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__))

            plugins_ie = load_plugins('extractor', 'IE')
            self.assertIn('ZippedPluginIE', plugins_ie.keys())

            plugins_pp = load_plugins('postprocessor', 'PP')
            self.assertIn('ZippedPluginPP', plugins_pp.keys())

        finally:
            sys.path.remove(str(zip_path))
            os.remove(zip_path)
            importlib.invalidate_caches()  # reset the import caches

    def test_plugin_dirs(self):
        # Internal plugin dirs hack for CLI --plugin-dirs
        # To be replaced with proper system later
        custom_plugin_dir = TEST_DATA_DIR / 'plugin_packages'
        Config._plugin_dirs = [str(custom_plugin_dir)]
        importlib.invalidate_caches()  # reset the import caches

        try:
            package = importlib.import_module(f'{PACKAGE_NAME}.extractor')
            self.assertIn(custom_plugin_dir / 'testpackage' / PACKAGE_NAME / 'extractor', map(Path, package.__path__))

            plugins_ie = load_plugins('extractor', 'IE')
            self.assertIn('PackagePluginIE', plugins_ie.keys())

        finally:
            Config._plugin_dirs = []
            importlib.invalidate_caches()  # reset the import caches


if __name__ == '__main__':
    unittest.main()