-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathtest_references.py
166 lines (152 loc) · 6.76 KB
/
test_references.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""Test module of the references."""
import unittest
from books import Books
import os
import re
import sys
def slugify(txt):
"""Slugify function."""
output = txt.lower()
output = re.sub(r'\]\([^)]*\)', ']', output) # remove the content of parenthesis if reference
output = re.sub(r'<[^>]+>', '', output)
output = output.replace('+', 'p')
output = re.sub(r"[\(\):`'=]", '', output)
output = re.sub(r'\\_', '_', output)
output = re.sub(r'[\W-]+', '-', output)
output = re.sub(r'^-*', '', output)
output = re.sub(r'-*$', '', output)
return output.strip(' ').strip('-')
class TestReferences(unittest.TestCase):
"""Unit test of the references."""
def setUp(self):
"""Setup."""
books = Books()
self.anchors = {}
for book in books.books:
# we are not responsible of the content of the discord chats
if book.name == 'discord':
continue
for md_path in book.md_paths:
anchors = []
args = {} if sys.version_info[0] < 3 else {'encoding': 'utf-8'}
with open(md_path, **args) as f:
skipUntil = ''
for line in f:
if skipUntil:
if skipUntil in line:
skipUntil = ''
continue
if '<a' in line and 'name=' in line:
for m in re.finditer(
r'<a[^>]*name\s*=\s*"(.*)"[^>]*>',
line.strip()):
anchors.append(m.group(1))
if re.match(r'```', line):
skipUntil = '```'
continue
elif line.startswith('#'):
m = re.match(r'^#{1,4} .*$', line)
if m:
title = re.sub(r'^#*', '', line)
anchors.append(slugify(title))
elif line.startswith('%figure'):
title = slugify(line.replace('%figure', ''))
if title:
anchors.append(slugify(title))
elif line.startswith('%api'):
title = line.replace('%api', '')
anchors.append(slugify(title))
elif "**wb" in line:
for m in re.finditer(r'\*\*(wb[^\*]*)\*\*', line):
anchor = m.group(1).replace('\\_', '_')
anchors.append(anchor)
self.anchors[md_path] = anchors
def test_anchors_are_unique(self):
"""Test that the anchors are unique."""
books = Books()
for book in books.books:
# we are not responsible of the content of the discord chats
if book.name == 'discord':
continue
for md_path in book.md_paths:
anchors = self.anchors[md_path]
s = set()
for a in anchors:
if a in s:
self.assertTrue(
False,
msg='%s: Anchors "%s" are not unique'
% (md_path, a)
)
s.add(a)
def test_references_are_valid(self):
"""Test that the MD files refer valid URLs."""
books = Books()
for book in books.books:
# we are not responsible of the content of the discord chats
if book.name == 'discord':
continue
for md_path in book.md_paths:
args = {} if sys.version_info[0] < 3 else {'encoding': 'utf-8'}
with open(md_path, **args) as f:
content = f.read()
for m in re.finditer(r"[^!]\[(.*?)\]\(([^\)]+)\)", content):
# remove parameters
ref = m.group(2)
# 1. external link
self.assertFalse(
ref.startswith('www'),
msg='URL should not start with "www": "%s"' % (ref)
)
if ref.startswith('http'):
continue
# 3. steam link
if ref.startswith('steam:'):
continue
# 4. mailto
if ref.startswith('mailto:'):
mailto = ref[len('mailto:'):]
m = re.match(r'^([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)$', mailto)
self.assertIsNotNone(
m,
msg='Invalid address "%s"' % (mailto)
)
continue
# 5. variable (the variable should be in format `url.something`)
if ref.startswith('{{'):
if re.match(r'{{\s{0,}url\..*}}', ref) is not None:
continue
# 6. link to another MD file
link = ''
anchor = ''
if ref.startswith('#'):
anchor = ref[1:] # Remove the '#' character.
else:
ref = ref.split('#')
link = ref[0]
if len(ref) > 1:
anchor = ref[1]
if link != '':
self.assertTrue(
link.endswith('.md'),
msg='Invalid reference "%s" in %s:\n-> "%s"' %
(ref, md_path, m.group(0))
)
file_path = os.path.join(book.path, link)
self.assertTrue(
os.path.isfile(file_path),
msg='%s: "%s" not found' % (md_path, file_path)
)
# 7. Anchor
if anchor != '':
file_path = ''
if link == '':
file_path = os.path.join(book.path, md_path)
else:
file_path = os.path.join(book.path, link)
file_path = os.path.abspath(file_path) # Remove '..'-like patterns from file_path.
found = anchor in self.anchors[file_path]
self.assertTrue(
found, msg='%s: %s#%s not found' %
(md_path, file_path, anchor)
)