3 # Allow direct execution
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'
27 class TestPostHooks(unittest
.TestCase
):
29 self
.stored_name_1
= None
30 self
.stored_name_2
= None
31 self
.params
= get_params({
32 'skip_download': False,
33 'writeinfojson': False,
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}\'')
69 if __name__
== '__main__':