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