-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
85 lines (72 loc) · 1.75 KB
/
tests.py
File metadata and controls
85 lines (72 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# pytest -q tests.py
import faker
import os
import re
import requests
# Utilities
F = faker.Faker()
def recursive_file(path=''):
'''
Example
=======
>>> for file in recursive_file('.'):
... print(file)
'''
path = path or os.path.curdir
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
yield os.path.join(dirpath, filename)
def ping_links(*links, seconds=3, **kwargs):
'''
Example
=======
>>> ping_links('...', '...', seconds=1)
'''
unavailable_links = list()
for link in links:
try:
response = requests.head(link, timeout=seconds, **kwargs)
is_available = response.status_code < 400
except:
is_available = False
if not is_available:
unavailable_links.append(link)
assert not unavailable_links, \
f'`{unavailable_links}` are unavailable'
def read_file(file_name, encode='utf-8'):
'''
Example
=======
>>> content = read_file('...')
'''
try:
with open(file_name, 'r', encoding=encode) as f:
return f.read()
except Exception as e:
print(e)
return ''
# Tests
def test_link_available():
available_ext = ('.md', )
ref_pattern = re.compile(r'(?<=\()https[^\)]+(?=\))')
headers = {'user-agent': F.user_agent()}
for file in recursive_file():
_, ext = os.path.splitext(file)
if ext in available_ext:
content = read_file(file)
links = ref_pattern.findall(content)
ping_links(*links, headers=headers)
# Main
if __name__ == '__main__':
import typing
_locals = locals()
_locals_key = tuple(_locals.keys())
is_test = lambda s: s.startswith('test')
for func in filter(is_test, _locals_key):
_func = _locals[func]
if isinstance(_func, typing.Callable) and \
_func.__code__.co_argcount==0:
try:
_func()
except AssertionError as ae:
print('AssertionError:', ae)