-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_md.py
91 lines (67 loc) · 2.45 KB
/
test_md.py
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
86
import re
import requests
# Get all markdown files in the directory
def get_file_names():
import os
exceptions = ['README.md']
files = os.listdir('.')
file_names = []
for file in files:
if '.md' in file and file not in exceptions:
print(file)
file_names.append(file)
return file_names
# Check the PR has only Markdown files
# def test_check_only_md():
# import os
# files = os.listdir('.')
# exceptions = ['README.md', 'test_md.py']
# for file in files:
# if '.md' not in file or file not in exceptions:
# assert False
# assert True
# Check for comment in the first line
def test_check_comment():
for file_name in get_file_names():
with open(file_name, 'r') as f:
text = f.read()
first_line = text.split('\n')[0]
if('<!--' in first_line and '-->' in first_line):
assert True
else:
assert False
# Check basic content in the markdown files
def test_check_md_content():
for file_name in get_file_names():
with open(file_name, 'r') as f:
text = f.read()
if "Author" not in text:
assert False
if "Acknowledgements" not in text:
assert False
if "Feedback" not in text:
assert False
assert True
# Check all links are working in the markdown file
def test_check_urls():
for file_name in get_file_names():
with open(file_name, 'r') as f:
text = f.read()
urls = re.findall("(?P<url>https?://[^\s]+)", text)
for url in urls:
url = url.replace(')', '')
try:
r = requests.get(url)
if r.status_code == 200 or r.status_code >= 500 or r.status_code == 403:
continue
else:
assert False
except requests.exceptions.RequestException as err:
print ("OOps: Something Else",err)
except requests.exceptions.HTTPError as errh:
print ("Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
print ("Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
print ("Timeout Error:",errt)
assert True