test_compat.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import struct
  8. from yt_dlp import compat
  9. from yt_dlp.compat import urllib # isort: split
  10. from yt_dlp.compat import (
  11. compat_etree_fromstring,
  12. compat_expanduser,
  13. compat_urllib_parse_unquote,
  14. compat_urllib_parse_urlencode,
  15. )
  16. from yt_dlp.compat.urllib.request import getproxies
  17. class TestCompat(unittest.TestCase):
  18. def test_compat_passthrough(self):
  19. with self.assertWarns(DeprecationWarning):
  20. compat.compat_basestring
  21. with self.assertWarns(DeprecationWarning):
  22. compat.WINDOWS_VT_MODE
  23. self.assertEqual(urllib.request.getproxies, getproxies)
  24. with self.assertWarns(DeprecationWarning):
  25. compat.compat_pycrypto_AES # Must not raise error
  26. def test_compat_expanduser(self):
  27. old_home = os.environ.get('HOME')
  28. test_str = R'C:\Documents and Settings\тест\Application Data'
  29. try:
  30. os.environ['HOME'] = test_str
  31. self.assertEqual(compat_expanduser('~'), test_str)
  32. finally:
  33. os.environ['HOME'] = old_home or ''
  34. def test_compat_urllib_parse_unquote(self):
  35. self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def')
  36. self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def')
  37. self.assertEqual(compat_urllib_parse_unquote(''), '')
  38. self.assertEqual(compat_urllib_parse_unquote('%'), '%')
  39. self.assertEqual(compat_urllib_parse_unquote('%%'), '%%')
  40. self.assertEqual(compat_urllib_parse_unquote('%%%'), '%%%')
  41. self.assertEqual(compat_urllib_parse_unquote('%2F'), '/')
  42. self.assertEqual(compat_urllib_parse_unquote('%2f'), '/')
  43. self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波')
  44. self.assertEqual(
  45. compat_urllib_parse_unquote('''<meta property="og:description" content="%E2%96%81%E2%96%82%E2%96%83%E2%96%84%25%E2%96%85%E2%96%86%E2%96%87%E2%96%88" />
  46. %<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''),
  47. '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" />
  48. %<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''')
  49. self.assertEqual(
  50. compat_urllib_parse_unquote('''%28%5E%E2%97%A3_%E2%97%A2%5E%29%E3%81%A3%EF%B8%BB%E3%83%87%E2%95%90%E4%B8%80 %E2%87%80 %E2%87%80 %E2%87%80 %E2%87%80 %E2%87%80 %E2%86%B6%I%Break%25Things%'''),
  51. '''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%Things%''')
  52. def test_compat_urllib_parse_unquote_plus(self):
  53. self.assertEqual(urllib.parse.unquote_plus('abc%20def'), 'abc def')
  54. self.assertEqual(urllib.parse.unquote_plus('%7e/abc+def'), '~/abc def')
  55. def test_compat_urllib_parse_urlencode(self):
  56. self.assertEqual(compat_urllib_parse_urlencode({'abc': 'def'}), 'abc=def')
  57. self.assertEqual(compat_urllib_parse_urlencode({'abc': b'def'}), 'abc=def')
  58. self.assertEqual(compat_urllib_parse_urlencode({b'abc': 'def'}), 'abc=def')
  59. self.assertEqual(compat_urllib_parse_urlencode({b'abc': b'def'}), 'abc=def')
  60. self.assertEqual(compat_urllib_parse_urlencode([('abc', 'def')]), 'abc=def')
  61. self.assertEqual(compat_urllib_parse_urlencode([('abc', b'def')]), 'abc=def')
  62. self.assertEqual(compat_urllib_parse_urlencode([(b'abc', 'def')]), 'abc=def')
  63. self.assertEqual(compat_urllib_parse_urlencode([(b'abc', b'def')]), 'abc=def')
  64. def test_compat_etree_fromstring(self):
  65. xml = '''
  66. <root foo="bar" spam="中文">
  67. <normal>foo</normal>
  68. <chinese>中文</chinese>
  69. <foo><bar>spam</bar></foo>
  70. </root>
  71. '''
  72. doc = compat_etree_fromstring(xml.encode())
  73. self.assertTrue(isinstance(doc.attrib['foo'], str))
  74. self.assertTrue(isinstance(doc.attrib['spam'], str))
  75. self.assertTrue(isinstance(doc.find('normal').text, str))
  76. self.assertTrue(isinstance(doc.find('chinese').text, str))
  77. self.assertTrue(isinstance(doc.find('foo/bar').text, str))
  78. def test_compat_etree_fromstring_doctype(self):
  79. xml = '''<?xml version="1.0"?>
  80. <!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
  81. <smil xmlns="http://www.w3.org/2001/SMIL20/Language"></smil>'''
  82. compat_etree_fromstring(xml)
  83. def test_struct_unpack(self):
  84. self.assertEqual(struct.unpack('!B', b'\x00'), (0,))
  85. if __name__ == '__main__':
  86. unittest.main()