Release 2024.07.02
[yt-dlp.git] / test / test_update.py
blob63a21e445fc5f295a19ed98c35e24d9a8ebf180b
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 FakeYDL, report_warning
12 from yt_dlp.update import UpdateInfo, Updater
15 # XXX: Keep in sync with yt_dlp.update.UPDATE_SOURCES
16 TEST_UPDATE_SOURCES = {
17 'stable': 'yt-dlp/yt-dlp',
18 'nightly': 'yt-dlp/yt-dlp-nightly-builds',
19 'master': 'yt-dlp/yt-dlp-master-builds',
22 TEST_API_DATA = {
23 'yt-dlp/yt-dlp/latest': {
24 'tag_name': '2023.12.31',
25 'target_commitish': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
26 'name': 'yt-dlp 2023.12.31',
27 'body': 'BODY',
29 'yt-dlp/yt-dlp-nightly-builds/latest': {
30 'tag_name': '2023.12.31.123456',
31 'target_commitish': 'master',
32 'name': 'yt-dlp nightly 2023.12.31.123456',
33 'body': 'Generated from: https://github.com/yt-dlp/yt-dlp/commit/cccccccccccccccccccccccccccccccccccccccc',
35 'yt-dlp/yt-dlp-master-builds/latest': {
36 'tag_name': '2023.12.31.987654',
37 'target_commitish': 'master',
38 'name': 'yt-dlp master 2023.12.31.987654',
39 'body': 'Generated from: https://github.com/yt-dlp/yt-dlp/commit/dddddddddddddddddddddddddddddddddddddddd',
41 'yt-dlp/yt-dlp/tags/testing': {
42 'tag_name': 'testing',
43 'target_commitish': '9999999999999999999999999999999999999999',
44 'name': 'testing',
45 'body': 'BODY',
47 'fork/yt-dlp/latest': {
48 'tag_name': '2050.12.31',
49 'target_commitish': 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
50 'name': '2050.12.31',
51 'body': 'BODY',
53 'fork/yt-dlp/tags/pr0000': {
54 'tag_name': 'pr0000',
55 'target_commitish': 'ffffffffffffffffffffffffffffffffffffffff',
56 'name': 'pr1234 2023.11.11.000000',
57 'body': 'BODY',
59 'fork/yt-dlp/tags/pr1234': {
60 'tag_name': 'pr1234',
61 'target_commitish': '0000000000000000000000000000000000000000',
62 'name': 'pr1234 2023.12.31.555555',
63 'body': 'BODY',
65 'fork/yt-dlp/tags/pr9999': {
66 'tag_name': 'pr9999',
67 'target_commitish': '1111111111111111111111111111111111111111',
68 'name': 'pr9999',
69 'body': 'BODY',
71 'fork/yt-dlp-satellite/tags/pr987': {
72 'tag_name': 'pr987',
73 'target_commitish': 'master',
74 'name': 'pr987',
75 'body': 'Generated from: https://github.com/yt-dlp/yt-dlp/commit/2222222222222222222222222222222222222222',
79 TEST_LOCKFILE_COMMENT = '# This file is used for regulating self-update'
81 TEST_LOCKFILE_V1 = rf'''{TEST_LOCKFILE_COMMENT}
82 lock 2022.08.18.36 .+ Python 3\.6
83 lock 2023.11.16 (?!win_x86_exe).+ Python 3\.7
84 lock 2023.11.16 win_x86_exe .+ Windows-(?:Vista|2008Server)
85 '''
87 TEST_LOCKFILE_V2_TMPL = r'''%s
88 lockV2 yt-dlp/yt-dlp 2022.08.18.36 .+ Python 3\.6
89 lockV2 yt-dlp/yt-dlp 2023.11.16 (?!win_x86_exe).+ Python 3\.7
90 lockV2 yt-dlp/yt-dlp 2023.11.16 win_x86_exe .+ Windows-(?:Vista|2008Server)
91 lockV2 yt-dlp/yt-dlp-nightly-builds 2023.11.15.232826 (?!win_x86_exe).+ Python 3\.7
92 lockV2 yt-dlp/yt-dlp-nightly-builds 2023.11.15.232826 win_x86_exe .+ Windows-(?:Vista|2008Server)
93 lockV2 yt-dlp/yt-dlp-master-builds 2023.11.15.232812 (?!win_x86_exe).+ Python 3\.7
94 lockV2 yt-dlp/yt-dlp-master-builds 2023.11.15.232812 win_x86_exe .+ Windows-(?:Vista|2008Server)
95 '''
97 TEST_LOCKFILE_V2 = TEST_LOCKFILE_V2_TMPL % TEST_LOCKFILE_COMMENT
99 TEST_LOCKFILE_ACTUAL = TEST_LOCKFILE_V2_TMPL % TEST_LOCKFILE_V1.rstrip('\n')
101 TEST_LOCKFILE_FORK = rf'''{TEST_LOCKFILE_ACTUAL}# Test if a fork blocks updates to non-numeric tags
102 lockV2 fork/yt-dlp pr0000 .+ Python 3.6
103 lockV2 fork/yt-dlp pr1234 (?!win_x86_exe).+ Python 3\.7
104 lockV2 fork/yt-dlp pr1234 win_x86_exe .+ Windows-(?:Vista|2008Server)
105 lockV2 fork/yt-dlp pr9999 .+ Python 3.11
109 class FakeUpdater(Updater):
110 current_version = '2022.01.01'
111 current_commit = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
113 _channel = 'stable'
114 _origin = 'yt-dlp/yt-dlp'
115 _update_sources = TEST_UPDATE_SOURCES
117 def _download_update_spec(self, *args, **kwargs):
118 return TEST_LOCKFILE_ACTUAL
120 def _call_api(self, tag):
121 tag = f'tags/{tag}' if tag != 'latest' else tag
122 return TEST_API_DATA[f'{self.requested_repo}/{tag}']
124 def _report_error(self, msg, *args, **kwargs):
125 report_warning(msg)
128 class TestUpdate(unittest.TestCase):
129 maxDiff = None
131 def test_update_spec(self):
132 ydl = FakeYDL()
133 updater = FakeUpdater(ydl, 'stable')
135 def test(lockfile, identifier, input_tag, expect_tag, exact=False, repo='yt-dlp/yt-dlp'):
136 updater._identifier = identifier
137 updater._exact = exact
138 updater.requested_repo = repo
139 result = updater._process_update_spec(lockfile, input_tag)
140 self.assertEqual(
141 result, expect_tag,
142 f'{identifier!r} requesting {repo}@{input_tag} (exact={exact}) '
143 f'returned {result!r} instead of {expect_tag!r}')
145 for lockfile in (TEST_LOCKFILE_V1, TEST_LOCKFILE_V2, TEST_LOCKFILE_ACTUAL, TEST_LOCKFILE_FORK):
146 # Normal operation
147 test(lockfile, 'zip Python 3.12.0', '2023.12.31', '2023.12.31')
148 test(lockfile, 'zip stable Python 3.12.0', '2023.12.31', '2023.12.31', exact=True)
149 # Python 3.6 --update should update only to its lock
150 test(lockfile, 'zip Python 3.6.0', '2023.11.16', '2022.08.18.36')
151 # --update-to an exact version later than the lock should return None
152 test(lockfile, 'zip stable Python 3.6.0', '2023.11.16', None, exact=True)
153 # Python 3.7 should be able to update to its lock
154 test(lockfile, 'zip Python 3.7.0', '2023.11.16', '2023.11.16')
155 test(lockfile, 'zip stable Python 3.7.1', '2023.11.16', '2023.11.16', exact=True)
156 # Non-win_x86_exe builds on py3.7 must be locked
157 test(lockfile, 'zip Python 3.7.1', '2023.12.31', '2023.11.16')
158 test(lockfile, 'zip stable Python 3.7.1', '2023.12.31', None, exact=True)
159 test( # Windows Vista w/ win_x86_exe must be locked
160 lockfile, 'win_x86_exe stable Python 3.7.9 (CPython x86 32bit) - Windows-Vista-6.0.6003-SP2',
161 '2023.12.31', '2023.11.16')
162 test( # Windows 2008Server w/ win_x86_exe must be locked
163 lockfile, 'win_x86_exe Python 3.7.9 (CPython x86 32bit) - Windows-2008Server',
164 '2023.12.31', None, exact=True)
165 test( # Windows 7 w/ win_x86_exe py3.7 build should be able to update beyond lock
166 lockfile, 'win_x86_exe stable Python 3.7.9 (CPython x86 32bit) - Windows-7-6.1.7601-SP1',
167 '2023.12.31', '2023.12.31')
168 test( # Windows 8.1 w/ '2008Server' in platform string should be able to update beyond lock
169 lockfile, 'win_x86_exe Python 3.7.9 (CPython x86 32bit) - Windows-post2008Server-6.2.9200',
170 '2023.12.31', '2023.12.31', exact=True)
172 # Forks can block updates to non-numeric tags rather than lock
173 test(TEST_LOCKFILE_FORK, 'zip Python 3.6.3', 'pr0000', None, repo='fork/yt-dlp')
174 test(TEST_LOCKFILE_FORK, 'zip stable Python 3.7.4', 'pr0000', 'pr0000', repo='fork/yt-dlp')
175 test(TEST_LOCKFILE_FORK, 'zip stable Python 3.7.4', 'pr1234', None, repo='fork/yt-dlp')
176 test(TEST_LOCKFILE_FORK, 'zip Python 3.8.1', 'pr1234', 'pr1234', repo='fork/yt-dlp', exact=True)
177 test(
178 TEST_LOCKFILE_FORK, 'win_x86_exe stable Python 3.7.9 (CPython x86 32bit) - Windows-Vista-6.0.6003-SP2',
179 'pr1234', None, repo='fork/yt-dlp')
180 test(
181 TEST_LOCKFILE_FORK, 'win_x86_exe stable Python 3.7.9 (CPython x86 32bit) - Windows-7-6.1.7601-SP1',
182 '2023.12.31', '2023.12.31', repo='fork/yt-dlp')
183 test(TEST_LOCKFILE_FORK, 'zip Python 3.11.2', 'pr9999', None, repo='fork/yt-dlp', exact=True)
184 test(TEST_LOCKFILE_FORK, 'zip stable Python 3.12.0', 'pr9999', 'pr9999', repo='fork/yt-dlp')
186 def test_query_update(self):
187 ydl = FakeYDL()
189 def test(target, expected, current_version=None, current_commit=None, identifier=None):
190 updater = FakeUpdater(ydl, target)
191 if current_version:
192 updater.current_version = current_version
193 if current_commit:
194 updater.current_commit = current_commit
195 updater._identifier = identifier or 'zip'
196 update_info = updater.query_update(_output=True)
197 self.assertDictEqual(
198 update_info.__dict__ if update_info else {}, expected.__dict__ if expected else {})
200 test('yt-dlp/yt-dlp@latest', UpdateInfo(
201 '2023.12.31', version='2023.12.31', requested_version='2023.12.31', commit='b' * 40))
202 test('yt-dlp/yt-dlp-nightly-builds@latest', UpdateInfo(
203 '2023.12.31.123456', version='2023.12.31.123456', requested_version='2023.12.31.123456', commit='c' * 40))
204 test('yt-dlp/yt-dlp-master-builds@latest', UpdateInfo(
205 '2023.12.31.987654', version='2023.12.31.987654', requested_version='2023.12.31.987654', commit='d' * 40))
206 test('fork/yt-dlp@latest', UpdateInfo(
207 '2050.12.31', version='2050.12.31', requested_version='2050.12.31', commit='e' * 40))
208 test('fork/yt-dlp@pr0000', UpdateInfo(
209 'pr0000', version='2023.11.11.000000', requested_version='2023.11.11.000000', commit='f' * 40))
210 test('fork/yt-dlp@pr1234', UpdateInfo(
211 'pr1234', version='2023.12.31.555555', requested_version='2023.12.31.555555', commit='0' * 40))
212 test('fork/yt-dlp@pr9999', UpdateInfo(
213 'pr9999', version=None, requested_version=None, commit='1' * 40))
214 test('fork/yt-dlp-satellite@pr987', UpdateInfo(
215 'pr987', version=None, requested_version=None, commit='2' * 40))
216 test('yt-dlp/yt-dlp', None, current_version='2024.01.01')
217 test('stable', UpdateInfo(
218 '2023.12.31', version='2023.12.31', requested_version='2023.12.31', commit='b' * 40))
219 test('nightly', UpdateInfo(
220 '2023.12.31.123456', version='2023.12.31.123456', requested_version='2023.12.31.123456', commit='c' * 40))
221 test('master', UpdateInfo(
222 '2023.12.31.987654', version='2023.12.31.987654', requested_version='2023.12.31.987654', commit='d' * 40))
223 test('testing', None, current_commit='9' * 40)
224 test('testing', UpdateInfo('testing', commit='9' * 40))
227 if __name__ == '__main__':
228 unittest.main()