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.

141 lines
4.7 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. """Contains test cases for the parsers module."""
  4. from __future__ import unicode_literals
  5. import sys
  6. import os.path
  7. import unittest
  8. PATH = os.path.realpath(os.path.abspath(__file__))
  9. sys.path.insert(0, os.path.dirname(os.path.dirname(PATH)))
  10. try:
  11. from youtube_dl_gui.parsers import OptionsParser
  12. except ImportError as error:
  13. print error
  14. sys.exit(1)
  15. class TestParse(unittest.TestCase):
  16. """Test case for OptionsParser parse method."""
  17. def setUp(self):
  18. # Create the options_dict based on the OptionHolder
  19. # items inside the OptionsParser object
  20. self.options_dict = {item.name:item.default_value for item in OptionsParser()._ydl_options}
  21. # Add extra options used by the OptionsParser.parse method
  22. self.options_dict["save_path"] = "/home/user/Workplace/test/youtube"
  23. self.options_dict["cmd_args"] = ""
  24. self.options_dict["output_format"] = 1 # Title
  25. self.options_dict["second_video_format"] = "0"
  26. self.options_dict["min_filesize_unit"] = ""
  27. self.options_dict["max_filesize_unit"] = ""
  28. def check_options_parse(self, expected_options):
  29. options_parser = OptionsParser()
  30. self.assertItemsEqual(options_parser.parse(self.options_dict), expected_options)
  31. def test_parse_to_audio_requirement_bug(self):
  32. """Test case for the 'to_audio' requirement."""
  33. self.options_dict["audio_quality"] = "9"
  34. self.options_dict["audio_format"] = "mp3"
  35. self.options_dict["embed_thumbnail"] = True
  36. expected_cmd_list = ["--newline",
  37. "-x",
  38. "--audio-format",
  39. "mp3",
  40. "--embed-thumbnail",
  41. "--audio-quality",
  42. "9",
  43. "-o",
  44. "/home/user/Workplace/test/youtube/%(title)s.%(ext)s"]
  45. self.check_options_parse(expected_cmd_list)
  46. # Setting 'to_audio' to True should return the same results
  47. # since the '-x' flag is already set on audio extraction
  48. self.options_dict["to_audio"] = True
  49. self.check_options_parse(expected_cmd_list)
  50. def test_parse_cmd_args_with_quotes(self):
  51. """Test the youtube-dl cmd line args parsing when quotes are presented.
  52. See: https://github.com/MrS0m30n3/youtube-dl-gui/issues/54
  53. """
  54. self.options_dict["video_format"] = "mp4"
  55. # Test with three quoted 'cmd_args'
  56. self.options_dict["cmd_args"] = "--recode-video mkv --postprocessor-args \"-codec copy -report\""
  57. expected_cmd_list = ["--newline",
  58. "-f",
  59. "mp4",
  60. "-o",
  61. "/home/user/Workplace/test/youtube/%(title)s.%(ext)s",
  62. "--recode-video",
  63. "mkv",
  64. "--postprocessor-args",
  65. "-codec copy -report"]
  66. self.check_options_parse(expected_cmd_list)
  67. # Test with two quoted 'cmd_args'
  68. self.options_dict["cmd_args"] = "--postprocessor-args \"-y -report\""
  69. expected_cmd_list = ["--newline",
  70. "-f",
  71. "mp4",
  72. "-o",
  73. "/home/user/Workplace/test/youtube/%(title)s.%(ext)s",
  74. "--postprocessor-args",
  75. "-y -report"]
  76. self.check_options_parse(expected_cmd_list)
  77. # Test with one quoted 'cmd_arg' followed by other cmd line args
  78. self.options_dict["cmd_args"] = "--postprocessor-args \"-y\" -v"
  79. expected_cmd_list = ["--newline",
  80. "-f",
  81. "mp4",
  82. "-o",
  83. "/home/user/Workplace/test/youtube/%(title)s.%(ext)s",
  84. "--postprocessor-args",
  85. "-y",
  86. "-v"]
  87. self.check_options_parse(expected_cmd_list)
  88. # Test the example presented in issue #54
  89. self.options_dict["cmd_args"] = "-f \"(mp4)[width<1300]\""
  90. self.options_dict["video_format"] = "0" # Set video format to 'default'
  91. expected_cmd_list = ["--newline",
  92. "-o",
  93. "/home/user/Workplace/test/youtube/%(title)s.%(ext)s",
  94. "-f",
  95. "(mp4)[width<1300]"]
  96. self.check_options_parse(expected_cmd_list)
  97. def main():
  98. unittest.main()
  99. if __name__ == '__main__':
  100. main()