[test:update] Implement simple updater unit tests
[yt-dlp3.git] / test / test_update.py
blob134424a31a384d6b6fb1f2fed7e607f482e62f23
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 Updater, UpdateInfo
14 TEST_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',
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',
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',
33 'yt-dlp/yt-dlp/tags/testing': {
34 'tag_name': 'testing',
35 'target_commitish': '9999999999999999999999999999999999999999',
36 'name': 'testing',
37 'body': 'BODY',
39 'fork/yt-dlp/latest': {
40 'tag_name': '2050.12.31',
41 'target_commitish': 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
42 'name': '2050.12.31',
43 'body': 'BODY',
45 'fork/yt-dlp/tags/pr0000': {
46 'tag_name': 'pr0000',
47 'target_commitish': 'ffffffffffffffffffffffffffffffffffffffff',
48 'name': 'pr1234 2023.11.11.000000',
49 'body': 'BODY',
51 'fork/yt-dlp/tags/pr1234': {
52 'tag_name': 'pr1234',
53 'target_commitish': '0000000000000000000000000000000000000000',
54 'name': 'pr1234 2023.12.31.555555',
55 'body': 'BODY',
57 'fork/yt-dlp/tags/pr9999': {
58 'tag_name': 'pr9999',
59 'target_commitish': '1111111111111111111111111111111111111111',
60 'name': 'pr9999',
61 'body': 'BODY',
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',
71 TEST_LOCKFILE_V1 = '''# This file is used for regulating self-update
72 lock 2022.08.18.36 .+ Python 3.6
73 lock 2023.11.13 .+ Python 3.7
74 '''
76 TEST_LOCKFILE_V2 = '''# This file is used for regulating self-update
77 lockV2 yt-dlp/yt-dlp 2022.08.18.36 .+ Python 3.6
78 lockV2 yt-dlp/yt-dlp 2023.11.13 .+ Python 3.7
79 '''
81 TEST_LOCKFILE_V1_V2 = '''# This file is used for regulating self-update
82 lock 2022.08.18.36 .+ Python 3.6
83 lock 2023.11.13 .+ Python 3.7
84 lockV2 yt-dlp/yt-dlp 2022.08.18.36 .+ Python 3.6
85 lockV2 yt-dlp/yt-dlp 2023.11.13 .+ Python 3.7
86 lockV2 fork/yt-dlp pr0000 .+ Python 3.6
87 lockV2 fork/yt-dlp pr1234 .+ Python 3.7
88 lockV2 fork/yt-dlp pr9999 .+ Python 3.11
89 '''
92 class FakeUpdater(Updater):
93 current_version = '2022.01.01'
94 current_commit = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
96 _channel = 'stable'
97 _origin = 'yt-dlp/yt-dlp'
99 def _download_update_spec(self, *args, **kwargs):
100 return TEST_LOCKFILE_V1_V2
102 def _call_api(self, tag):
103 tag = f'tags/{tag}' if tag != 'latest' else tag
104 return TEST_API_DATA[f'{self.requested_repo}/{tag}']
106 def _report_error(self, msg, *args, **kwargs):
107 report_warning(msg)
110 class TestUpdate(unittest.TestCase):
111 maxDiff = None
113 def test_update_spec(self):
114 ydl = FakeYDL()
115 updater = FakeUpdater(ydl, 'stable@latest')
117 def test(lockfile, identifier, input_tag, expect_tag, exact=False, repo='yt-dlp/yt-dlp'):
118 updater._identifier = identifier
119 updater._exact = exact
120 updater.requested_repo = repo
121 result = updater._process_update_spec(lockfile, input_tag)
122 self.assertEqual(
123 result, expect_tag,
124 f'{identifier!r} requesting {repo}@{input_tag} (exact={exact}) '
125 f'returned {result!r} instead of {expect_tag!r}')
127 test(TEST_LOCKFILE_V1, 'zip Python 3.11.0', '2023.11.13', '2023.11.13')
128 test(TEST_LOCKFILE_V1, 'zip stable Python 3.11.0', '2023.11.13', '2023.11.13', exact=True)
129 test(TEST_LOCKFILE_V1, 'zip Python 3.6.0', '2023.11.13', '2022.08.18.36')
130 test(TEST_LOCKFILE_V1, 'zip stable Python 3.6.0', '2023.11.13', None, exact=True)
131 test(TEST_LOCKFILE_V1, 'zip Python 3.7.0', '2023.11.13', '2023.11.13')
132 test(TEST_LOCKFILE_V1, 'zip stable Python 3.7.1', '2023.11.13', '2023.11.13')
133 test(TEST_LOCKFILE_V1, 'zip Python 3.7.1', '2023.12.31', '2023.11.13')
134 test(TEST_LOCKFILE_V1, 'zip stable Python 3.7.1', '2023.12.31', '2023.11.13')
136 test(TEST_LOCKFILE_V2, 'zip Python 3.11.1', '2023.11.13', '2023.11.13')
137 test(TEST_LOCKFILE_V2, 'zip stable Python 3.11.1', '2023.12.31', '2023.12.31')
138 test(TEST_LOCKFILE_V2, 'zip Python 3.6.1', '2023.11.13', '2022.08.18.36')
139 test(TEST_LOCKFILE_V2, 'zip stable Python 3.7.2', '2023.11.13', '2023.11.13')
140 test(TEST_LOCKFILE_V2, 'zip Python 3.7.2', '2023.12.31', '2023.11.13')
142 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.11.2', '2023.11.13', '2023.11.13')
143 test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.11.2', '2023.12.31', '2023.12.31')
144 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.6.2', '2023.11.13', '2022.08.18.36')
145 test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.7.3', '2023.11.13', '2023.11.13')
146 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.7.3', '2023.12.31', '2023.11.13')
147 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.6.3', 'pr0000', None, repo='fork/yt-dlp')
148 test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.7.4', 'pr0000', 'pr0000', repo='fork/yt-dlp')
149 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.6.4', 'pr0000', None, repo='fork/yt-dlp')
150 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.7.4', 'pr1234', None, repo='fork/yt-dlp')
151 test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.8.1', 'pr1234', 'pr1234', repo='fork/yt-dlp')
152 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.7.5', 'pr1234', None, repo='fork/yt-dlp')
153 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.11.3', 'pr9999', None, repo='fork/yt-dlp')
154 test(TEST_LOCKFILE_V1_V2, 'zip stable Python 3.12.0', 'pr9999', 'pr9999', repo='fork/yt-dlp')
155 test(TEST_LOCKFILE_V1_V2, 'zip Python 3.11.4', 'pr9999', None, repo='fork/yt-dlp')
157 def test_query_update(self):
158 ydl = FakeYDL()
160 def test(target, expected, current_version=None, current_commit=None, identifier=None):
161 updater = FakeUpdater(ydl, target)
162 if current_version:
163 updater.current_version = current_version
164 if current_commit:
165 updater.current_commit = current_commit
166 updater._identifier = identifier or 'zip'
167 update_info = updater.query_update(_output=True)
168 self.assertDictEqual(
169 update_info.__dict__ if update_info else {}, expected.__dict__ if expected else {})
171 test('yt-dlp/yt-dlp@latest', UpdateInfo(
172 '2023.12.31', version='2023.12.31', requested_version='2023.12.31', commit='b' * 40))
173 test('yt-dlp/yt-dlp-nightly-builds@latest', UpdateInfo(
174 '2023.12.31.123456', version='2023.12.31.123456', requested_version='2023.12.31.123456', commit='c' * 40))
175 test('yt-dlp/yt-dlp-master-builds@latest', UpdateInfo(
176 '2023.12.31.987654', version='2023.12.31.987654', requested_version='2023.12.31.987654', commit='d' * 40))
177 test('fork/yt-dlp@latest', UpdateInfo(
178 '2050.12.31', version='2050.12.31', requested_version='2050.12.31', commit='e' * 40))
179 test('fork/yt-dlp@pr0000', UpdateInfo(
180 'pr0000', version='2023.11.11.000000', requested_version='2023.11.11.000000', commit='f' * 40))
181 test('fork/yt-dlp@pr1234', UpdateInfo(
182 'pr1234', version='2023.12.31.555555', requested_version='2023.12.31.555555', commit='0' * 40))
183 test('fork/yt-dlp@pr9999', UpdateInfo(
184 'pr9999', version=None, requested_version=None, commit='1' * 40))
185 test('fork/yt-dlp-satellite@pr987', UpdateInfo(
186 'pr987', version=None, requested_version=None, commit='2' * 40))
187 test('yt-dlp/yt-dlp', None, current_version='2024.01.01')
188 test('stable', UpdateInfo(
189 '2023.12.31', version='2023.12.31', requested_version='2023.12.31', commit='b' * 40))
190 test('nightly', UpdateInfo(
191 '2023.12.31.123456', version='2023.12.31.123456', requested_version='2023.12.31.123456', commit='c' * 40))
192 test('master', UpdateInfo(
193 '2023.12.31.987654', version='2023.12.31.987654', requested_version='2023.12.31.987654', commit='d' * 40))
194 test('testing', None, current_commit='9' * 40)
195 test('testing', UpdateInfo('testing', commit='9' * 40))
198 if __name__ == '__main__':
199 unittest.main()