]> Raphaël G. Git Repositories - youtubedl/blob - test/test_utils.py
Update changelog.
[youtubedl] / test / test_utils.py
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 # Allow direct execution
5 import os
6 import sys
7 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10
11 # Various small unit tests
12 import xml.etree.ElementTree
13
14 #from youtube_dl.utils import htmlentity_transform
15 from youtube_dl.utils import (
16 timeconvert,
17 sanitize_filename,
18 unescapeHTML,
19 orderedSet,
20 DateRange,
21 unified_strdate,
22 find_xpath_attr,
23 get_meta_content,
24 xpath_with_ns,
25 smuggle_url,
26 unsmuggle_url,
27 shell_quote,
28 encodeFilename,
29 )
30
31 if sys.version_info < (3, 0):
32 _compat_str = lambda b: b.decode('unicode-escape')
33 else:
34 _compat_str = lambda s: s
35
36
37 class TestUtil(unittest.TestCase):
38 def test_timeconvert(self):
39 self.assertTrue(timeconvert('') is None)
40 self.assertTrue(timeconvert('bougrg') is None)
41
42 def test_sanitize_filename(self):
43 self.assertEqual(sanitize_filename('abc'), 'abc')
44 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
45
46 self.assertEqual(sanitize_filename('123'), '123')
47
48 self.assertEqual('abc_de', sanitize_filename('abc/de'))
49 self.assertFalse('/' in sanitize_filename('abc/de///'))
50
51 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
52 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
53 self.assertEqual('yes no', sanitize_filename('yes? no'))
54 self.assertEqual('this - that', sanitize_filename('this: that'))
55
56 self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
57 aumlaut = _compat_str('\xe4')
58 self.assertEqual(sanitize_filename(aumlaut), aumlaut)
59 tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
60 self.assertEqual(sanitize_filename(tests), tests)
61
62 forbidden = '"\0\\/'
63 for fc in forbidden:
64 for fbc in forbidden:
65 self.assertTrue(fbc not in sanitize_filename(fc))
66
67 def test_sanitize_filename_restricted(self):
68 self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
69 self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
70
71 self.assertEqual(sanitize_filename('123', restricted=True), '123')
72
73 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
74 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
75
76 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
77 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
78 self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
79 self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
80
81 tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
82 self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
83 self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
84
85 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
86 for fc in forbidden:
87 for fbc in forbidden:
88 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
89
90 # Handle a common case more neatly
91 self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
92 self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
93 # .. but make sure the file name is never empty
94 self.assertTrue(sanitize_filename('-', restricted=True) != '')
95 self.assertTrue(sanitize_filename(':', restricted=True) != '')
96
97 def test_sanitize_ids(self):
98 self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
99 self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
100 self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
101
102 def test_ordered_set(self):
103 self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
104 self.assertEqual(orderedSet([]), [])
105 self.assertEqual(orderedSet([1]), [1])
106 #keep the list ordered
107 self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
108
109 def test_unescape_html(self):
110 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
111
112 def test_daterange(self):
113 _20century = DateRange("19000101","20000101")
114 self.assertFalse("17890714" in _20century)
115 _ac = DateRange("00010101")
116 self.assertTrue("19690721" in _ac)
117 _firstmilenium = DateRange(end="10000101")
118 self.assertTrue("07110427" in _firstmilenium)
119
120 def test_unified_dates(self):
121 self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
122 self.assertEqual(unified_strdate('8/7/2009'), '20090708')
123 self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
124 self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
125
126 def test_find_xpath_attr(self):
127 testxml = u'''<root>
128 <node/>
129 <node x="a"/>
130 <node x="a" y="c" />
131 <node x="b" y="d" />
132 </root>'''
133 doc = xml.etree.ElementTree.fromstring(testxml)
134
135 self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
136 self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
137 self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
138
139 def test_meta_parser(self):
140 testhtml = u'''
141 <head>
142 <meta name="description" content="foo &amp; bar">
143 <meta content='Plato' name='author'/>
144 </head>
145 '''
146 get_meta = lambda name: get_meta_content(name, testhtml)
147 self.assertEqual(get_meta('description'), u'foo & bar')
148 self.assertEqual(get_meta('author'), 'Plato')
149
150 def test_xpath_with_ns(self):
151 testxml = u'''<root xmlns:media="http://example.com/">
152 <media:song>
153 <media:author>The Author</media:author>
154 <url>http://server.com/download.mp3</url>
155 </media:song>
156 </root>'''
157 doc = xml.etree.ElementTree.fromstring(testxml)
158 find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
159 self.assertTrue(find('media:song') is not None)
160 self.assertEqual(find('media:song/media:author').text, u'The Author')
161 self.assertEqual(find('media:song/url').text, u'http://server.com/download.mp3')
162
163 def test_smuggle_url(self):
164 data = {u"ö": u"ö", u"abc": [3]}
165 url = 'https://foo.bar/baz?x=y#a'
166 smug_url = smuggle_url(url, data)
167 unsmug_url, unsmug_data = unsmuggle_url(smug_url)
168 self.assertEqual(url, unsmug_url)
169 self.assertEqual(data, unsmug_data)
170
171 res_url, res_data = unsmuggle_url(url)
172 self.assertEqual(res_url, url)
173 self.assertEqual(res_data, None)
174
175 def test_shell_quote(self):
176 args = ['ffmpeg', '-i', encodeFilename(u'ñ€ß\'.mp4')]
177 self.assertEqual(shell_quote(args), u"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
178
179
180 if __name__ == '__main__':
181 unittest.main()