test_compat.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 compat_etree_fromstring, compat_expanduser
  11. from yt_dlp.compat.urllib.request import getproxies
  12. class TestCompat(unittest.TestCase):
  13. def test_compat_passthrough(self):
  14. with self.assertWarns(DeprecationWarning):
  15. _ = compat.compat_basestring
  16. with self.assertWarns(DeprecationWarning):
  17. _ = compat.WINDOWS_VT_MODE
  18. self.assertEqual(urllib.request.getproxies, getproxies)
  19. with self.assertWarns(DeprecationWarning):
  20. _ = compat.compat_pycrypto_AES # Must not raise error
  21. def test_compat_expanduser(self):
  22. old_home = os.environ.get('HOME')
  23. test_str = R'C:\Documents and Settings\тест\Application Data'
  24. try:
  25. os.environ['HOME'] = test_str
  26. self.assertEqual(compat_expanduser('~'), test_str)
  27. finally:
  28. os.environ['HOME'] = old_home or ''
  29. def test_compat_etree_fromstring(self):
  30. xml = '''
  31. <root foo="bar" spam="中文">
  32. <normal>foo</normal>
  33. <chinese>中文</chinese>
  34. <foo><bar>spam</bar></foo>
  35. </root>
  36. '''
  37. doc = compat_etree_fromstring(xml.encode())
  38. self.assertTrue(isinstance(doc.attrib['foo'], str))
  39. self.assertTrue(isinstance(doc.attrib['spam'], str))
  40. self.assertTrue(isinstance(doc.find('normal').text, str))
  41. self.assertTrue(isinstance(doc.find('chinese').text, str))
  42. self.assertTrue(isinstance(doc.find('foo/bar').text, str))
  43. def test_compat_etree_fromstring_doctype(self):
  44. xml = '''<?xml version="1.0"?>
  45. <!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
  46. <smil xmlns="http://www.w3.org/2001/SMIL20/Language"></smil>'''
  47. compat_etree_fromstring(xml)
  48. def test_struct_unpack(self):
  49. self.assertEqual(struct.unpack('!B', b'\x00'), (0,))
  50. if __name__ == '__main__':
  51. unittest.main()