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