hbmartin commited on
Commit
b8d2d59
·
1 Parent(s): 84a96f3

test for main cli and arguments parsing

Browse files
Files changed (2) hide show
  1. pytube/cli.py +24 -18
  2. tests/test_cli.py +68 -0
pytube/cli.py CHANGED
@@ -9,7 +9,7 @@ import logging
9
  import os
10
  import sys
11
  from io import BufferedWriter
12
- from typing import Tuple, Any, Optional
13
 
14
  from pytube import __version__, CaptionQuery
15
  from pytube import YouTube
@@ -22,6 +22,28 @@ def main():
22
  """Command line application to download youtube videos."""
23
  # noinspection PyTypeChecker
24
  parser = argparse.ArgumentParser(description=main.__doc__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  parser.add_argument("url", help="The YouTube /watch url", nargs="?")
26
  parser.add_argument(
27
  "--version", action="version", version="%(prog)s " + __version__,
@@ -63,23 +85,7 @@ def main():
63
  ),
64
  )
65
 
66
- args = parser.parse_args()
67
- logging.getLogger().setLevel(max(3 - args.verbosity, 0) * 10)
68
-
69
- if not args.url:
70
- parser.print_help()
71
- sys.exit(1)
72
-
73
- youtube = YouTube(args.url)
74
-
75
- if args.list:
76
- display_streams(youtube)
77
- if args.build_playback_report:
78
- build_playback_report(youtube)
79
- if args.itag:
80
- download(youtube=youtube, itag=args.itag)
81
- if hasattr(args, "caption_code"):
82
- download_caption(youtube=youtube, lang_code=args.caption_code)
83
 
84
 
85
  def build_playback_report(youtube: YouTube) -> None:
 
9
  import os
10
  import sys
11
  from io import BufferedWriter
12
+ from typing import Tuple, Any, Optional, List
13
 
14
  from pytube import __version__, CaptionQuery
15
  from pytube import YouTube
 
22
  """Command line application to download youtube videos."""
23
  # noinspection PyTypeChecker
24
  parser = argparse.ArgumentParser(description=main.__doc__)
25
+ args = _parse_args(parser)
26
+ logging.getLogger().setLevel(max(3 - args.verbosity, 0) * 10)
27
+
28
+ if not args.url:
29
+ parser.print_help()
30
+ sys.exit(1)
31
+
32
+ youtube = YouTube(args.url)
33
+
34
+ if args.list:
35
+ display_streams(youtube)
36
+ if args.build_playback_report:
37
+ build_playback_report(youtube)
38
+ if args.itag:
39
+ download(youtube=youtube, itag=args.itag)
40
+ if hasattr(args, "caption_code"):
41
+ download_caption(youtube=youtube, lang_code=args.caption_code)
42
+
43
+
44
+ def _parse_args(
45
+ parser: argparse.ArgumentParser, args: Optional[List] = None
46
+ ) -> argparse.Namespace:
47
  parser.add_argument("url", help="The YouTube /watch url", nargs="?")
48
  parser.add_argument(
49
  "--version", action="version", version="%(prog)s " + __version__,
 
85
  ),
86
  )
87
 
88
+ return parser.parse_args(args)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
 
91
  def build_playback_report(youtube: YouTube) -> None:
tests/test_cli.py CHANGED
@@ -1,4 +1,5 @@
1
  # -*- coding: utf-8 -*-
 
2
  from unittest import mock
3
  from unittest.mock import MagicMock, patch
4
 
@@ -6,6 +7,8 @@ import pytest
6
 
7
  from pytube import cli, StreamQuery, Caption, CaptionQuery
8
 
 
 
9
 
10
  @mock.patch("pytube.cli.YouTube")
11
  def test_download_when_itag_not_found(youtube):
@@ -88,3 +91,68 @@ def test_on_progress(stream, writer):
88
  cli.display_progress_bar = MagicMock()
89
  cli.on_progress(stream, "", writer, 7)
90
  cli.display_progress_bar.assert_called_once_with(3, 10)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # -*- coding: utf-8 -*-
2
+ import argparse
3
  from unittest import mock
4
  from unittest.mock import MagicMock, patch
5
 
 
7
 
8
  from pytube import cli, StreamQuery, Caption, CaptionQuery
9
 
10
+ parse_args = cli._parse_args
11
+
12
 
13
  @mock.patch("pytube.cli.YouTube")
14
  def test_download_when_itag_not_found(youtube):
 
91
  cli.display_progress_bar = MagicMock()
92
  cli.on_progress(stream, "", writer, 7)
93
  cli.display_progress_bar.assert_called_once_with(3, 10)
94
+
95
+
96
+ def test_parse_args_falsey():
97
+ parser = argparse.ArgumentParser()
98
+ args = cli._parse_args(parser, ["urlhere"])
99
+ assert args.url == "urlhere"
100
+ assert args.build_playback_report is False
101
+ assert args.itag is None
102
+ assert args.list is False
103
+ assert args.verbosity == 0
104
+
105
+
106
+ def test_parse_args_truthy():
107
+ parser = argparse.ArgumentParser()
108
+ args = cli._parse_args(
109
+ parser, ["urlhere", "--build-playback-report", "-c", "en", "-l", "--itag=10"]
110
+ )
111
+ assert args.url == "urlhere"
112
+ assert args.build_playback_report is True
113
+ assert args.itag == 10
114
+ assert args.list is True
115
+
116
+
117
+ @mock.patch("pytube.cli.YouTube.__init__", return_value=None)
118
+ def test_main_download(youtube):
119
+ parser = argparse.ArgumentParser()
120
+ args = parse_args(parser, ["urlhere", "--itag=10"])
121
+ cli._parse_args = MagicMock(return_value=args)
122
+ cli.download = MagicMock()
123
+ cli.main()
124
+ youtube.assert_called()
125
+ cli.download.assert_called()
126
+
127
+
128
+ @mock.patch("pytube.cli.YouTube.__init__", return_value=None)
129
+ def test_main_build_playback_report(youtube):
130
+ parser = argparse.ArgumentParser()
131
+ args = parse_args(parser, ["urlhere", "--build-playback-report"])
132
+ cli._parse_args = MagicMock(return_value=args)
133
+ cli.build_playback_report = MagicMock()
134
+ cli.main()
135
+ youtube.assert_called()
136
+ cli.build_playback_report.assert_called()
137
+
138
+
139
+ @mock.patch("pytube.cli.YouTube.__init__", return_value=None)
140
+ def test_main_display_streams(youtube):
141
+ parser = argparse.ArgumentParser()
142
+ args = parse_args(parser, ["urlhere", "-l"])
143
+ cli._parse_args = MagicMock(return_value=args)
144
+ cli.display_streams = MagicMock()
145
+ cli.main()
146
+ youtube.assert_called()
147
+ cli.display_streams.assert_called()
148
+
149
+
150
+ @mock.patch("pytube.cli.YouTube.__init__", return_value=None)
151
+ def test_main_download_caption(youtube):
152
+ parser = argparse.ArgumentParser()
153
+ args = parse_args(parser, ["urlhere", "-c"])
154
+ cli._parse_args = MagicMock(return_value=args)
155
+ cli.download_caption = MagicMock()
156
+ cli.main()
157
+ youtube.assert_called()
158
+ cli.download_caption.assert_called()