forked from Ghostkeeper/SettingsGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
links.py
62 lines (55 loc) · 2.71 KB
/
links.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
#Copyright (C) 2020 Ghostkeeper
#This plug-in is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#This plug-in is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for details.
#You should have received a copy of the GNU Affero General Public License along with this plug-in. If not, see <https://gnu.org/licenses/>.
"""
These are tests to check whether there are any broken links in any articles.
"""
import os # To find all articles.
import re # To find the links in the Markdown articles.
import unittest # Run the automated tests.
class TestLinks(unittest.TestCase):
"""
These are tests to check whether there are any broken links in any articles.
"""
def all_articles(self):
"""
Get all article file paths.
:return: A sequence of all article file paths.
"""
article_directories = {"articles", "translations"}
for article_directory in article_directories:
for root, _, files in os.walk(os.path.join(os.path.dirname(__file__), "..", "resources", article_directory)):
for filename in files:
_, extension = os.path.splitext(filename)
if extension != ".md":
continue # Not an article. Ignore this.
yield os.path.join(root, filename)
def test_images(self):
"""
Test if the links to images are correct.
"""
find_images = re.compile(r"!\[.*\]\(([^\)]*)\)")
for filename in self.all_articles():
with self.subTest():
with open(filename) as f:
contents = f.read()
for image_link in find_images.findall(contents):
image_path = os.path.join(os.path.dirname(filename), image_link)
assert os.path.exists(image_path), "Article {article_path} refers to image {image_path}, which doesn't exist.".format(article_path=filename, image_path=image_link)
def test_articles(self):
"""
Test if the links to other articles are correct.
"""
find_links = re.compile(r"\[.*\]\(([^\)]*)\)")
for filename in self.all_articles():
with self.subTest():
with open(filename) as f:
contents = f.read()
for link in find_links.findall(contents):
if link.startswith("https://") or link.startswith("http://"):
continue # Don't find articles on the internet either.
article_path = os.path.join(os.path.dirname(filename), link)
assert os.path.exists(article_path), "Article {article_path} refers to article {path}, which doesn't exist.".format(article_path=filename, path=article_path)
if __name__ == "__main__":
unittest.main()