more tests
Browse files- pytube/helpers.py +9 -9
- tests/test_helpers.py +21 -0
pytube/helpers.py
CHANGED
@@ -26,21 +26,21 @@ def regex_search(pattern, string, groups=False, group=None, flags=0):
|
|
26 |
"""
|
27 |
regex = re.compile(pattern, flags)
|
28 |
results = regex.search(string)
|
29 |
-
logger.debug(
|
30 |
-
'finished regex search: %s',
|
31 |
-
pprint.pformat(
|
32 |
-
{
|
33 |
-
'pattern': pattern,
|
34 |
-
'results': results.group(0),
|
35 |
-
}, indent=2,
|
36 |
-
),
|
37 |
-
)
|
38 |
if not results:
|
39 |
raise RegexMatchError(
|
40 |
'regex pattern ({pattern}) had zero matches'
|
41 |
.format(pattern=pattern),
|
42 |
)
|
43 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
if groups:
|
45 |
return results.groups()
|
46 |
elif group is not None:
|
|
|
26 |
"""
|
27 |
regex = re.compile(pattern, flags)
|
28 |
results = regex.search(string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
if not results:
|
30 |
raise RegexMatchError(
|
31 |
'regex pattern ({pattern}) had zero matches'
|
32 |
.format(pattern=pattern),
|
33 |
)
|
34 |
else:
|
35 |
+
logger.debug(
|
36 |
+
'finished regex search: %s',
|
37 |
+
pprint.pformat(
|
38 |
+
{
|
39 |
+
'pattern': pattern,
|
40 |
+
'results': results.group(0),
|
41 |
+
}, indent=2,
|
42 |
+
),
|
43 |
+
)
|
44 |
if groups:
|
45 |
return results.groups()
|
46 |
elif group is not None:
|
tests/test_helpers.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pytube import helpers
|
5 |
+
from pytube.exceptions import RegexMatchError
|
6 |
+
|
7 |
+
|
8 |
+
def test_regex_search_no_match():
|
9 |
+
with pytest.raises(RegexMatchError):
|
10 |
+
helpers.regex_search('^a$', '', groups=True)
|
11 |
+
|
12 |
+
|
13 |
+
def test_regex_search():
|
14 |
+
# TODO(nficano): should check isinstance
|
15 |
+
assert helpers.regex_search('^a$', 'a') is not None
|
16 |
+
|
17 |
+
|
18 |
+
def test_safe_filename():
|
19 |
+
"""Unsafe characters get stripped from generated filename"""
|
20 |
+
assert helpers.safe_filename('abc1245$$') == 'abc1245'
|
21 |
+
assert helpers.safe_filename('abc##') == 'abc'
|