]> jfr.im git - yt-dlp.git/blame - test/test_post_hooks.py
[cleanup] Upgrade syntax
[yt-dlp.git] / test / test_post_hooks.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
ab8e5e51
AM
2import os
3import sys
4import unittest
5sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
6
060ac762 7from test.helper import get_params, try_rm, is_download_test
7a5c1cfe
P
8import yt_dlp.YoutubeDL
9from yt_dlp.utils import DownloadError
ab8e5e51
AM
10
11
7a5c1cfe 12class YoutubeDL(yt_dlp.YoutubeDL):
ab8e5e51 13 def __init__(self, *args, **kwargs):
86e5f3ed 14 super().__init__(*args, **kwargs)
ab8e5e51
AM
15 self.to_stderr = self.to_screen
16
17
18TEST_ID = 'gr51aVj-mLg'
19EXPECTED_NAME = 'gr51aVj-mLg'
20
21
060ac762 22@is_download_test
ab8e5e51
AM
23class TestPostHooks(unittest.TestCase):
24 def setUp(self):
25 self.stored_name_1 = None
26 self.stored_name_2 = None
27 self.params = get_params({
28 'skip_download': False,
29 'writeinfojson': False,
30 'quiet': True,
31 'verbose': False,
32 'cachedir': False,
33 })
34 self.files = []
35
36 def test_post_hooks(self):
37 self.params['post_hooks'] = [self.hook_one, self.hook_two]
38 ydl = YoutubeDL(self.params)
39 ydl.download([TEST_ID])
40 self.assertEqual(self.stored_name_1, EXPECTED_NAME, 'Not the expected name from hook 1')
41 self.assertEqual(self.stored_name_2, EXPECTED_NAME, 'Not the expected name from hook 2')
42
43 def test_post_hook_exception(self):
44 self.params['post_hooks'] = [self.hook_three]
45 ydl = YoutubeDL(self.params)
46 self.assertRaises(DownloadError, ydl.download, [TEST_ID])
47
48 def hook_one(self, filename):
49 self.stored_name_1, _ = os.path.splitext(os.path.basename(filename))
50 self.files.append(filename)
51
52 def hook_two(self, filename):
53 self.stored_name_2, _ = os.path.splitext(os.path.basename(filename))
54 self.files.append(filename)
55
56 def hook_three(self, filename):
57 self.files.append(filename)
58 raise Exception('Test exception for \'%s\'' % filename)
59
60 def tearDown(self):
61 for f in self.files:
62 try_rm(f)
63
64
65if __name__ == '__main__':
66 unittest.main()