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