Last active
April 17, 2020 07:06
-
-
Save AntoineAugusti/809d6ea91051430fd263dd5b682a738d to your computer and use it in GitHub Desktop.
Writing tests for Jekyll posts https://blog.antoine-augusti.fr/2019/08/writing-tests-for-your-static-website-jekyll-hugo/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 2 | |
jobs: | |
build: | |
docker: | |
- image: circleci/python:3.6 | |
steps: | |
- checkout | |
- restore_cache: | |
keys: | |
- v1-dependencies-{{ checksum "requirements.txt" }} | |
- run: | |
name: Install dependencies | |
command: | | |
python -m venv venv | |
. venv/bin/activate | |
pip install -r requirements.txt | |
- save_cache: | |
paths: | |
- ./venv | |
key: v1-dependencies-{{ checksum "requirements.txt" }} | |
- run: | |
name: Tests | |
command: | | |
. venv/bin/activate | |
nosetests . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nose==1.3.7 | |
python-frontmatter==0.4.5 | |
PyYAML==5.1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import frontmatter | |
class TestPosts(unittest.TestCase): | |
REQUIRED_KEYS = [ | |
"author", | |
"description", | |
"image", | |
"layout", | |
"title", | |
"twitter", | |
"tags", | |
] | |
ALLOWED_TAGS = { | |
"accompagnement", | |
"algorithme", | |
"collectif", | |
"datascience", | |
"design", | |
"développement", | |
} | |
def test_posts(self): | |
tags = [] | |
for file in glob.glob("_posts/*.md"): | |
# Skip drafts | |
if file.startswith("_posts/_"): | |
continue | |
with open(file) as f: | |
post = frontmatter.load(f) | |
for key in self.REQUIRED_KEYS: | |
if key not in post: | |
self.fail( | |
f"La clé `{key}` est absente de `{file}` et est obligatoire" | |
) | |
image = post["image"] | |
if not (image.startswith("/img/") or image.startswith("http")): | |
self.fail(f"L'image de `{file}` semble invalide : {post['image']}") | |
self.assertEquals(post["layout"], "post") | |
twitter = post["twitter"] | |
if twitter is not None and ( | |
twitter.startswith("https://") or twitter.startswith("@") | |
): | |
self.fail( | |
f"Le Twitter `{file}` semble invalide, il faut indiquer uniquement le nom d'utilisateur : {post['twitter']}" | |
) | |
for tag in post["tags"]: | |
self.assertIn( | |
tag, self.ALLOWED_TAGS, f"Le fichier `{file}` a un tag invalide." | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment