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