-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_posts.py
60 lines (47 loc) · 1.79 KB
/
test_posts.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
#!/usr/bin/python
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
import datetime
import os
import pytest
import re
import frontmatter
from glob import glob
here = os.path.abspath(os.path.dirname(__file__))
root = os.path.dirname(here)
def test_posts():
posts = []
for base, dirnames, filenames in os.walk(os.path.join(root, "_posts")):
for filename in filenames:
if filename.endswith(".md"):
posts.append(os.path.join(base, filename))
for post in posts:
# The post must be named by year-mm-dd-title.md
filename = os.path.basename(post)
assert re.search(
"^(?:.+/)*([0-9]{4}-[0-9]{2}-[0-9]{2})-(.*)([.][^.]+)$", filename
)
assert filename.endswith(".md")
# Each post must be organized in a directory by year
year = post.split("/")[-2]
assert year == filename[0:4]
# Check frontendmatter fields
content = frontmatter.load(post)
# Check for required fields
for required in ["layout", "title", "author", "date"]:
assert required in content.metadata
# Must be a post with a GitHub author alias
assert content.metadata["layout"] == "post"
assert len(content.metadata["title"]) <= 26 # will overflow space
assert content.metadata["author"].startswith("@")
assert isinstance(content.metadata["date"], datetime.datetime)
# Check provided resources
if "resources" in content.metadata:
for resource in content.metadata["resources"]:
assert "name" in resource
assert "url" in resource
assert resource["name"]
assert resource["url"]