]> jfr.im git - yt-dlp.git/blame - test/test_update.py
[test:download] Raise on network errors (#10283)
[yt-dlp.git] / test / test_update.py
CommitLineData
87264d4f 1#!/usr/bin/env python3
2
3# Allow direct execution
4import os
5import sys
6import unittest
7
8sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10
11from test.helper import FakeYDL, report_warning
f9fb3ce8 12from yt_dlp.update import UpdateInfo, Updater
87264d4f 13
632b8ee5 14
15# XXX: Keep in sync with yt_dlp.update.UPDATE_SOURCES
16TEST_UPDATE_SOURCES = {
17 'stable': 'yt-dlp/yt-dlp',
18 'nightly': 'yt-dlp/yt-dlp-nightly-builds',
19 'master': 'yt-dlp/yt-dlp-master-builds',
20}
21
87264d4f 22TEST_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',
28 },
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',
34 },
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',
40 },
41 'yt-dlp/yt-dlp/tags/testing': {
42 'tag_name': 'testing',
43 'target_commitish': '9999999999999999999999999999999999999999',
44 'name': 'testing',
45 'body': 'BODY',
46 },
47 'fork/yt-dlp/latest': {
48 'tag_name': '2050.12.31',
49 'target_commitish': 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
50 'name': '2050.12.31',
51 'body': 'BODY',
52 },
53 'fork/yt-dlp/tags/pr0000': {
54 'tag_name': 'pr0000',
55 'target_commitish': 'ffffffffffffffffffffffffffffffffffffffff',
56 'name': 'pr1234 2023.11.11.000000',
57 'body': 'BODY',
58 },
59 'fork/yt-dlp/tags/pr1234': {
60 'tag_name': 'pr1234',
61 'target_commitish': '0000000000000000000000000000000000000000',
62 'name': 'pr1234 2023.12.31.555555',
63 'body': 'BODY',
64 },
65 'fork/yt-dlp/tags/pr9999': {
66 'tag_name': 'pr9999',
67 'target_commitish': '1111111111111111111111111111111111111111',
68 'name': 'pr9999',
69 'body': 'BODY',
70 },
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',
76 },
77}
78
f4b95aca 79TEST_LOCKFILE_COMMENT = '# This file is used for regulating self-update'
80
add96eb9 81TEST_LOCKFILE_V1 = rf'''{TEST_LOCKFILE_COMMENT}
f4b95aca 82lock 2022.08.18.36 .+ Python 3\.6
83lock 2023.11.16 (?!win_x86_exe).+ Python 3\.7
84lock 2023.11.16 win_x86_exe .+ Windows-(?:Vista|2008Server)
add96eb9 85'''
f4b95aca 86
87TEST_LOCKFILE_V2_TMPL = r'''%s
88lockV2 yt-dlp/yt-dlp 2022.08.18.36 .+ Python 3\.6
89lockV2 yt-dlp/yt-dlp 2023.11.16 (?!win_x86_exe).+ Python 3\.7
90lockV2 yt-dlp/yt-dlp 2023.11.16 win_x86_exe .+ Windows-(?:Vista|2008Server)
91lockV2 yt-dlp/yt-dlp-nightly-builds 2023.11.15.232826 (?!win_x86_exe).+ Python 3\.7
92lockV2 yt-dlp/yt-dlp-nightly-builds 2023.11.15.232826 win_x86_exe .+ Windows-(?:Vista|2008Server)
93lockV2 yt-dlp/yt-dlp-master-builds 2023.11.15.232812 (?!win_x86_exe).+ Python 3\.7
94lockV2 yt-dlp/yt-dlp-master-builds 2023.11.15.232812 win_x86_exe .+ Windows-(?:Vista|2008Server)
87264d4f 95'''
96
f4b95aca 97TEST_LOCKFILE_V2 = TEST_LOCKFILE_V2_TMPL % TEST_LOCKFILE_COMMENT
98
99TEST_LOCKFILE_ACTUAL = TEST_LOCKFILE_V2_TMPL % TEST_LOCKFILE_V1.rstrip('\n')
87264d4f 100
add96eb9 101TEST_LOCKFILE_FORK = rf'''{TEST_LOCKFILE_ACTUAL}# Test if a fork blocks updates to non-numeric tags
87264d4f 102lockV2 fork/yt-dlp pr0000 .+ Python 3.6
f4b95aca 103lockV2 fork/yt-dlp pr1234 (?!win_x86_exe).+ Python 3\.7
104lockV2 fork/yt-dlp pr1234 win_x86_exe .+ Windows-(?:Vista|2008Server)
87264d4f 105lockV2 fork/yt-dlp pr9999 .+ Python 3.11
add96eb9 106'''
87264d4f 107
108
109class FakeUpdater(Updater):
110 current_version = '2022.01.01'
111 current_commit = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
112
113 _channel = 'stable'
114 _origin = 'yt-dlp/yt-dlp'
632b8ee5 115 _update_sources = TEST_UPDATE_SOURCES
87264d4f 116
117 def _download_update_spec(self, *args, **kwargs):
f4b95aca 118 return TEST_LOCKFILE_ACTUAL
87264d4f 119
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}']
123
124 def _report_error(self, msg, *args, **kwargs):
125 report_warning(msg)
126
127
128class TestUpdate(unittest.TestCase):
129 maxDiff = None
130
131 def test_update_spec(self):
132 ydl = FakeYDL()
f4b95aca 133 updater = FakeUpdater(ydl, 'stable')
87264d4f 134
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}')
144
f4b95aca 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)
171
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')
87264d4f 185
186 def test_query_update(self):
187 ydl = FakeYDL()
188
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 {})
199
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))
225
226
227if __name__ == '__main__':
228 unittest.main()