]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python3 | |
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 | ||
10 | from test.helper import get_params, try_rm | |
11 | import yt_dlp.YoutubeDL | |
12 | from yt_dlp.utils import DownloadError | |
13 | ||
14 | ||
15 | class YoutubeDL(yt_dlp.YoutubeDL): | |
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 | ||
25 | class TestPostHooks(unittest.TestCase): | |
26 | def setUp(self): | |
27 | self.stored_name_1 = None | |
28 | self.stored_name_2 = None | |
29 | self.params = get_params({ | |
30 | 'skip_download': False, | |
31 | 'writeinfojson': False, | |
32 | 'quiet': True, | |
33 | 'verbose': False, | |
34 | 'cachedir': False, | |
35 | }) | |
36 | self.files = [] | |
37 | ||
38 | def test_post_hooks(self): | |
39 | self.params['post_hooks'] = [self.hook_one, self.hook_two] | |
40 | ydl = YoutubeDL(self.params) | |
41 | ydl.download([TEST_ID]) | |
42 | self.assertEqual(self.stored_name_1, EXPECTED_NAME, 'Not the expected name from hook 1') | |
43 | self.assertEqual(self.stored_name_2, EXPECTED_NAME, 'Not the expected name from hook 2') | |
44 | ||
45 | def test_post_hook_exception(self): | |
46 | self.params['post_hooks'] = [self.hook_three] | |
47 | ydl = YoutubeDL(self.params) | |
48 | self.assertRaises(DownloadError, ydl.download, [TEST_ID]) | |
49 | ||
50 | def hook_one(self, filename): | |
51 | self.stored_name_1, _ = os.path.splitext(os.path.basename(filename)) | |
52 | self.files.append(filename) | |
53 | ||
54 | def hook_two(self, filename): | |
55 | self.stored_name_2, _ = os.path.splitext(os.path.basename(filename)) | |
56 | self.files.append(filename) | |
57 | ||
58 | def hook_three(self, filename): | |
59 | self.files.append(filename) | |
60 | raise Exception('Test exception for \'%s\'' % filename) | |
61 | ||
62 | def tearDown(self): | |
63 | for f in self.files: | |
64 | try_rm(f) | |
65 | ||
66 | ||
67 | if __name__ == '__main__': | |
68 | unittest.main() |