reformatted code

This commit is contained in:
Bastian Venthur
2023-06-16 10:25:21 +02:00
parent f6c5eaf375
commit 0349bd3359
11 changed files with 381 additions and 380 deletions

View File

@@ -1,73 +1,73 @@
# remove when we don't support py38 anymore
from __future__ import annotations
from tempfile import TemporaryDirectory
import os
from datetime import datetime
from typing import Any
from argparse import Namespace
from datetime import datetime
from tempfile import TemporaryDirectory
from typing import Any
import pytest
from pytest import CaptureFixture, LogCaptureFixture
from jinja2 import Template
from pytest import CaptureFixture, LogCaptureFixture
from blag import __VERSION__
from blag import blag
from blag import __VERSION__, blag
def test_generate_feed(cleandir: str) -> None:
articles: list[tuple[str, dict[str, Any]]] = []
blag.generate_feed(articles, 'build', ' ', ' ', ' ', ' ')
assert os.path.exists('build/atom.xml')
blag.generate_feed(articles, "build", " ", " ", " ", " ")
assert os.path.exists("build/atom.xml")
def test_feed(cleandir: str) -> None:
articles: list[tuple[str, dict[str, Any]]] = [
(
'dest1.html',
"dest1.html",
{
'title': 'title1',
'date': datetime(2019, 6, 6),
'content': 'content1',
"title": "title1",
"date": datetime(2019, 6, 6),
"content": "content1",
},
),
(
'dest2.html',
"dest2.html",
{
'title': 'title2',
'date': datetime(1980, 5, 9),
'content': 'content2',
"title": "title2",
"date": datetime(1980, 5, 9),
"content": "content2",
},
),
]
blag.generate_feed(
articles,
'build',
'https://example.com/',
'blog title',
'blog description',
'blog author',
"build",
"https://example.com/",
"blog title",
"blog description",
"blog author",
)
with open('build/atom.xml') as fh:
with open("build/atom.xml") as fh:
feed = fh.read()
assert '<title>blog title</title>' in feed
assert "<title>blog title</title>" in feed
# enable when https://github.com/getpelican/feedgenerator/issues/22
# is fixed
# assert '<subtitle>blog description</subtitle>' in feed
assert '<author><name>blog author</name></author>' in feed
assert "<author><name>blog author</name></author>" in feed
# article 1
assert '<title>title1</title>' in feed
assert "<title>title1</title>" in feed
assert '<summary type="html">title1' in feed
assert '<published>2019-06-06' in feed
assert "<published>2019-06-06" in feed
assert '<content type="html">content1' in feed
assert '<link href="https://example.com/dest1.html"' in feed
# article 2
assert '<title>title2</title>' in feed
assert "<title>title2</title>" in feed
assert '<summary type="html">title2' in feed
assert '<published>1980-05-09' in feed
assert "<published>1980-05-09" in feed
assert '<content type="html">content2' in feed
assert '<link href="https://example.com/dest2.html"' in feed
@@ -77,57 +77,57 @@ def test_generate_feed_with_description(cleandir: str) -> None:
# the feed, otherwise we simply use the title of the article
articles: list[tuple[str, dict[str, Any]]] = [
(
'dest.html',
"dest.html",
{
'title': 'title',
'description': 'description',
'date': datetime(2019, 6, 6),
'content': 'content',
"title": "title",
"description": "description",
"date": datetime(2019, 6, 6),
"content": "content",
},
)
]
blag.generate_feed(articles, 'build', ' ', ' ', ' ', ' ')
blag.generate_feed(articles, "build", " ", " ", " ", " ")
with open('build/atom.xml') as fh:
with open("build/atom.xml") as fh:
feed = fh.read()
assert '<title>title</title>' in feed
assert "<title>title</title>" in feed
assert '<summary type="html">description' in feed
assert '<published>2019-06-06' in feed
assert "<published>2019-06-06" in feed
assert '<content type="html">content' in feed
def test_parse_args_build() -> None:
# test default args
args = blag.parse_args(['build'])
assert args.input_dir == 'content'
assert args.output_dir == 'build'
assert args.template_dir == 'templates'
assert args.static_dir == 'static'
args = blag.parse_args(["build"])
assert args.input_dir == "content"
assert args.output_dir == "build"
assert args.template_dir == "templates"
assert args.static_dir == "static"
# input dir
args = blag.parse_args(['build', '-i', 'foo'])
assert args.input_dir == 'foo'
args = blag.parse_args(['build', '--input-dir', 'foo'])
assert args.input_dir == 'foo'
args = blag.parse_args(["build", "-i", "foo"])
assert args.input_dir == "foo"
args = blag.parse_args(["build", "--input-dir", "foo"])
assert args.input_dir == "foo"
# output dir
args = blag.parse_args(['build', '-o', 'foo'])
assert args.output_dir == 'foo'
args = blag.parse_args(['build', '--output-dir', 'foo'])
assert args.output_dir == 'foo'
args = blag.parse_args(["build", "-o", "foo"])
assert args.output_dir == "foo"
args = blag.parse_args(["build", "--output-dir", "foo"])
assert args.output_dir == "foo"
# template dir
args = blag.parse_args(['build', '-t', 'foo'])
assert args.template_dir == 'foo'
args = blag.parse_args(['build', '--template-dir', 'foo'])
assert args.template_dir == 'foo'
args = blag.parse_args(["build", "-t", "foo"])
assert args.template_dir == "foo"
args = blag.parse_args(["build", "--template-dir", "foo"])
assert args.template_dir == "foo"
# static dir
args = blag.parse_args(['build', '-s', 'foo'])
assert args.static_dir == 'foo'
args = blag.parse_args(['build', '--static-dir', 'foo'])
assert args.static_dir == 'foo'
args = blag.parse_args(["build", "-s", "foo"])
assert args.static_dir == "foo"
args = blag.parse_args(["build", "--static-dir", "foo"])
assert args.static_dir == "foo"
def test_get_config() -> None:
@@ -140,24 +140,24 @@ author = a. u. thor
"""
# happy path
with TemporaryDirectory() as dir:
configfile = f'{dir}/config.ini'
with open(configfile, 'w') as fh:
configfile = f"{dir}/config.ini"
with open(configfile, "w") as fh:
fh.write(config)
config_parsed = blag.get_config(configfile)
assert config_parsed['base_url'] == 'https://example.com/'
assert config_parsed['title'] == 'title'
assert config_parsed['description'] == 'description'
assert config_parsed['author'] == 'a. u. thor'
assert config_parsed["base_url"] == "https://example.com/"
assert config_parsed["title"] == "title"
assert config_parsed["description"] == "description"
assert config_parsed["author"] == "a. u. thor"
# a missing required config causes a sys.exit
for x in 'base_url', 'title', 'description', 'author':
config2 = '\n'.join(
for x in "base_url", "title", "description", "author":
config2 = "\n".join(
[line for line in config.splitlines() if not line.startswith(x)]
)
with TemporaryDirectory() as dir:
configfile = f'{dir}/config.ini'
with open(configfile, 'w') as fh:
configfile = f"{dir}/config.ini"
with open(configfile, "w") as fh:
fh.write(config2)
with pytest.raises(SystemExit):
config_parsed = blag.get_config(configfile)
@@ -171,19 +171,19 @@ description = description
author = a. u. thor
"""
with TemporaryDirectory() as dir:
configfile = f'{dir}/config.ini'
with open(configfile, 'w') as fh:
configfile = f"{dir}/config.ini"
with open(configfile, "w") as fh:
fh.write(config)
config_parsed = blag.get_config(configfile)
assert config_parsed['base_url'] == 'https://example.com/'
assert config_parsed["base_url"] == "https://example.com/"
def test_environment_factory(cleandir: str) -> None:
globals_: dict[str, object] = {'foo': 'bar', 'test': 'me'}
globals_: dict[str, object] = {"foo": "bar", "test": "me"}
env = blag.environment_factory("templates", globals_=globals_)
assert env.globals['foo'] == 'bar'
assert env.globals['test'] == 'me'
assert env.globals["foo"] == "bar"
assert env.globals["test"] == "me"
def test_process_markdown(
@@ -216,12 +216,12 @@ foo bar
convertibles = []
for i, txt in enumerate((page1, article1, article2)):
with open(f'content/{str(i)}', 'w') as fh:
with open(f"content/{str(i)}", "w") as fh:
fh.write(txt)
convertibles.append((str(i), str(i)))
articles, pages = blag.process_markdown(
convertibles, 'content', 'build', page_template, article_template
convertibles, "content", "build", page_template, article_template
)
assert isinstance(articles, list)
@@ -229,14 +229,14 @@ foo bar
for dst, context in articles:
assert isinstance(dst, str)
assert isinstance(context, dict)
assert 'content' in context
assert "content" in context
assert isinstance(pages, list)
assert len(pages) == 1
for dst, context in pages:
assert isinstance(dst, str)
assert isinstance(context, dict)
assert 'content' in context
assert "content" in context
def test_build(args: Namespace) -> None:
@@ -268,63 +268,63 @@ foo bar
# write some convertibles
convertibles = []
for i, txt in enumerate((page1, article1, article2)):
with open(f'{args.input_dir}/{str(i)}.md', 'w') as fh:
with open(f"{args.input_dir}/{str(i)}.md", "w") as fh:
fh.write(txt)
convertibles.append((str(i), str(i)))
# some static files
with open(f'{args.static_dir}/test', 'w') as fh:
fh.write('hello')
with open(f"{args.static_dir}/test", "w") as fh:
fh.write("hello")
os.mkdir(f'{args.input_dir}/testdir')
with open(f'{args.input_dir}/testdir/test', 'w') as fh:
fh.write('hello')
os.mkdir(f"{args.input_dir}/testdir")
with open(f"{args.input_dir}/testdir/test", "w") as fh:
fh.write("hello")
blag.build(args)
# test existence of the three converted files
for i in range(3):
assert os.path.exists(f'{args.output_dir}/{i}.html')
assert os.path.exists(f"{args.output_dir}/{i}.html")
# ... static file
assert os.path.exists(f'{args.output_dir}/test')
assert os.path.exists(f"{args.output_dir}/test")
# ... directory
assert os.path.exists(f'{args.output_dir}/testdir/test')
assert os.path.exists(f"{args.output_dir}/testdir/test")
# ... feed
assert os.path.exists(f'{args.output_dir}/atom.xml')
assert os.path.exists(f"{args.output_dir}/atom.xml")
# ... index
assert os.path.exists(f'{args.output_dir}/index.html')
assert os.path.exists(f"{args.output_dir}/index.html")
# ... archive
assert os.path.exists(f'{args.output_dir}/archive.html')
assert os.path.exists(f"{args.output_dir}/archive.html")
# ... tags
assert os.path.exists(f'{args.output_dir}/tags/index.html')
assert os.path.exists(f'{args.output_dir}/tags/foo.html')
assert os.path.exists(f'{args.output_dir}/tags/bar.html')
assert os.path.exists(f"{args.output_dir}/tags/index.html")
assert os.path.exists(f"{args.output_dir}/tags/foo.html")
assert os.path.exists(f"{args.output_dir}/tags/bar.html")
@pytest.mark.parametrize(
'template',
"template",
[
'page.html',
'article.html',
'index.html',
'archive.html',
'tags.html',
'tag.html',
]
"page.html",
"article.html",
"index.html",
"archive.html",
"tags.html",
"tag.html",
],
)
def test_missing_template_raises(template: str, args: Namespace) -> None:
os.remove(f'templates/{template}')
os.remove(f"templates/{template}")
with pytest.raises(SystemExit):
blag.build(args)
def test_main(cleandir: str) -> None:
blag.main(['build'])
blag.main(["build"])
def test_cli_version(capsys: CaptureFixture[str]) -> None:
with pytest.raises(SystemExit) as ex:
blag.main(['--version'])
blag.main(["--version"])
# normal system exit
assert ex.value.code == 0
# proper version reported
@@ -333,8 +333,8 @@ def test_cli_version(capsys: CaptureFixture[str]) -> None:
def test_cli_verbose(cleandir: str, caplog: LogCaptureFixture) -> None:
blag.main(['build'])
assert 'DEBUG' not in caplog.text
blag.main(["build"])
assert "DEBUG" not in caplog.text
blag.main(['--verbose', 'build'])
assert 'DEBUG' in caplog.text
blag.main(["--verbose", "build"])
assert "DEBUG" in caplog.text