test_post_hooks.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. from test.helper import get_params, is_download_test, try_rm
  8. import yt_dlp.YoutubeDL # isort: split
  9. from yt_dlp.utils import DownloadError
  10. class YoutubeDL(yt_dlp.YoutubeDL):
  11. def __init__(self, *args, **kwargs):
  12. super().__init__(*args, **kwargs)
  13. self.to_stderr = self.to_screen
  14. TEST_ID = 'gr51aVj-mLg'
  15. EXPECTED_NAME = 'gr51aVj-mLg'
  16. @is_download_test
  17. class TestPostHooks(unittest.TestCase):
  18. def setUp(self):
  19. self.stored_name_1 = None
  20. self.stored_name_2 = None
  21. self.params = get_params({
  22. 'skip_download': False,
  23. 'writeinfojson': False,
  24. 'quiet': True,
  25. 'verbose': False,
  26. 'cachedir': False,
  27. })
  28. self.files = []
  29. def test_post_hooks(self):
  30. self.params['post_hooks'] = [self.hook_one, self.hook_two]
  31. ydl = YoutubeDL(self.params)
  32. ydl.download([TEST_ID])
  33. self.assertEqual(self.stored_name_1, EXPECTED_NAME, 'Not the expected name from hook 1')
  34. self.assertEqual(self.stored_name_2, EXPECTED_NAME, 'Not the expected name from hook 2')
  35. def test_post_hook_exception(self):
  36. self.params['post_hooks'] = [self.hook_three]
  37. ydl = YoutubeDL(self.params)
  38. self.assertRaises(DownloadError, ydl.download, [TEST_ID])
  39. def hook_one(self, filename):
  40. self.stored_name_1, _ = os.path.splitext(os.path.basename(filename))
  41. self.files.append(filename)
  42. def hook_two(self, filename):
  43. self.stored_name_2, _ = os.path.splitext(os.path.basename(filename))
  44. self.files.append(filename)
  45. def hook_three(self, filename):
  46. self.files.append(filename)
  47. raise Exception(f'Test exception for \'{filename}\'')
  48. def tearDown(self):
  49. for f in self.files:
  50. try_rm(f)
  51. if __name__ == '__main__':
  52. unittest.main()