You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
6.4 KiB

  1. #!/usr/bin/env python
  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. # Various small unit tests
  8. import xml.etree.ElementTree
  9. #from youtube_dl.utils import htmlentity_transform
  10. from youtube_dl.utils import (
  11. timeconvert,
  12. sanitize_filename,
  13. unescapeHTML,
  14. orderedSet,
  15. DateRange,
  16. unified_strdate,
  17. find_xpath_attr,
  18. get_meta_content,
  19. xpath_with_ns,
  20. )
  21. if sys.version_info < (3, 0):
  22. _compat_str = lambda b: b.decode('unicode-escape')
  23. else:
  24. _compat_str = lambda s: s
  25. class TestUtil(unittest.TestCase):
  26. def test_timeconvert(self):
  27. self.assertTrue(timeconvert('') is None)
  28. self.assertTrue(timeconvert('bougrg') is None)
  29. def test_sanitize_filename(self):
  30. self.assertEqual(sanitize_filename('abc'), 'abc')
  31. self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
  32. self.assertEqual(sanitize_filename('123'), '123')
  33. self.assertEqual('abc_de', sanitize_filename('abc/de'))
  34. self.assertFalse('/' in sanitize_filename('abc/de///'))
  35. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
  36. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
  37. self.assertEqual('yes no', sanitize_filename('yes? no'))
  38. self.assertEqual('this - that', sanitize_filename('this: that'))
  39. self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
  40. aumlaut = _compat_str('\xe4')
  41. self.assertEqual(sanitize_filename(aumlaut), aumlaut)
  42. tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
  43. self.assertEqual(sanitize_filename(tests), tests)
  44. forbidden = '"\0\\/'
  45. for fc in forbidden:
  46. for fbc in forbidden:
  47. self.assertTrue(fbc not in sanitize_filename(fc))
  48. def test_sanitize_filename_restricted(self):
  49. self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
  50. self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
  51. self.assertEqual(sanitize_filename('123', restricted=True), '123')
  52. self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
  53. self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
  54. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
  55. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
  56. self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
  57. self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
  58. tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
  59. self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
  60. self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
  61. forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
  62. for fc in forbidden:
  63. for fbc in forbidden:
  64. self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
  65. # Handle a common case more neatly
  66. self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
  67. self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
  68. # .. but make sure the file name is never empty
  69. self.assertTrue(sanitize_filename('-', restricted=True) != '')
  70. self.assertTrue(sanitize_filename(':', restricted=True) != '')
  71. def test_sanitize_ids(self):
  72. self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
  73. self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
  74. self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
  75. def test_ordered_set(self):
  76. self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
  77. self.assertEqual(orderedSet([]), [])
  78. self.assertEqual(orderedSet([1]), [1])
  79. #keep the list ordered
  80. self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
  81. def test_unescape_html(self):
  82. self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
  83. def test_daterange(self):
  84. _20century = DateRange("19000101","20000101")
  85. self.assertFalse("17890714" in _20century)
  86. _ac = DateRange("00010101")
  87. self.assertTrue("19690721" in _ac)
  88. _firstmilenium = DateRange(end="10000101")
  89. self.assertTrue("07110427" in _firstmilenium)
  90. def test_unified_dates(self):
  91. self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
  92. self.assertEqual(unified_strdate('8/7/2009'), '20090708')
  93. self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
  94. self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
  95. def test_find_xpath_attr(self):
  96. testxml = u'''<root>
  97. <node/>
  98. <node x="a"/>
  99. <node x="a" y="c" />
  100. <node x="b" y="d" />
  101. </root>'''
  102. doc = xml.etree.ElementTree.fromstring(testxml)
  103. self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
  104. self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
  105. self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
  106. def test_meta_parser(self):
  107. testhtml = u'''
  108. <head>
  109. <meta name="description" content="foo &amp; bar">
  110. <meta content='Plato' name='author'/>
  111. </head>
  112. '''
  113. get_meta = lambda name: get_meta_content(name, testhtml)
  114. self.assertEqual(get_meta('description'), u'foo & bar')
  115. self.assertEqual(get_meta('author'), 'Plato')
  116. def test_xpath_with_ns(self):
  117. testxml = u'''<root xmlns:media="http://example.com/">
  118. <media:song>
  119. <media:author>The Author</media:author>
  120. <url>http://server.com/download.mp3</url>
  121. </media:song>
  122. </root>'''
  123. doc = xml.etree.ElementTree.fromstring(testxml)
  124. find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
  125. self.assertTrue(find('media:song') is not None)
  126. self.assertEqual(find('media:song/media:author').text, u'The Author')
  127. self.assertEqual(find('media:song/url').text, u'http://server.com/download.mp3')
  128. if __name__ == '__main__':
  129. unittest.main()