test_update.py 10 KB

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