1
0
mirror of https://github.com/venthur/blag.git synced 2025-11-25 12:42:41 +00:00

added cleandir to change cwd

This commit is contained in:
Bastian Venthur
2021-03-29 13:02:12 +02:00
parent 72971408b2
commit 499b0dfe11
2 changed files with 41 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
from tempfile import TemporaryDirectory
from os import mkdir
import os
import pytest
@@ -45,14 +45,32 @@ def tag_template(environment):
@pytest.fixture
def tempdir():
config = """
[main]
base_url = https://example.com/
title = title
description = description
author = a. u. thor
"""
with TemporaryDirectory() as dir:
for d in 'content', 'build', 'static', 'templates':
mkdir(f'{dir}/{d}')
os.mkdir(f'{dir}/{d}')
with open(f'{dir}/config.ini', 'w') as fh:
fh.write(config)
yield dir
@pytest.fixture
def args(tempdir):
def cleandir(tempdir):
old_cwd = os.getcwd()
os.chdir(tempdir)
yield
os.chdir(old_cwd)
@pytest.fixture
def args(cleandir):
class NameSpace:
def __init__(self, **kwargs):
@@ -60,9 +78,9 @@ def args(tempdir):
setattr(self, name, kwargs[name])
args = NameSpace(
input_dir=f'{tempdir}/content',
output_dir=f'{tempdir}/build',
static_dir=f'{tempdir}/static',
template_dir=f'{tempdir}/templates',
input_dir='content',
output_dir='build',
static_dir='static',
template_dir='templates',
)
yield args

View File

@@ -7,15 +7,13 @@ import pytest
from blag import blag
def test_generate_feed(tempdir):
outdir = f'{tempdir}/build'
def test_generate_feed(args):
articles = []
blag.generate_feed(articles, outdir, ' ', ' ', ' ', ' ')
assert os.path.exists(f'{outdir}/atom.xml')
blag.generate_feed(articles, args.output_dir, ' ', ' ', ' ', ' ')
assert os.path.exists(f'{args.output_dir}/atom.xml')
def test_feed(tempdir):
outdir = f'{tempdir}/build'
def test_feed(args):
articles = [
[
'dest1.html',
@@ -36,9 +34,9 @@ def test_feed(tempdir):
]
blag.generate_feed(articles, outdir, 'https://example.com/', 'blog title',
'blog description', 'blog author')
with open(f'{outdir}/atom.xml') as fh:
blag.generate_feed(articles, args.output_dir, 'https://example.com/',
'blog title', 'blog description', 'blog author')
with open(f'{args.output_dir}/atom.xml') as fh:
feed = fh.read()
assert '<title>blog title</title>' in feed
@@ -62,8 +60,7 @@ def test_feed(tempdir):
assert '<link href="https://example.com/dest2.html"' in feed
def test_generate_feed_with_description(tempdir):
outdir = f'{tempdir}/build'
def test_generate_feed_with_description(args):
# if a description is provided, it will be used as the summary in
# the feed, otherwise we simply use the title of the article
articles = [[
@@ -75,9 +72,9 @@ def test_generate_feed_with_description(tempdir):
'content': 'content',
}
]]
blag.generate_feed(articles, outdir, ' ', ' ', ' ', ' ')
blag.generate_feed(articles, args.output_dir, ' ', ' ', ' ', ' ')
with open(f'{outdir}/atom.xml') as fh:
with open(f'{args.output_dir}/atom.xml') as fh:
feed = fh.read()
assert '<title>title</title>' in feed
@@ -179,9 +176,7 @@ def test_environment_factory():
assert env.globals['test'] == 'me'
def test_process_markdown(tempdir, page_template, article_template):
inputdir = f'{tempdir}/content'
outdir = f'{tempdir}/build'
def test_process_markdown(args, page_template, article_template):
page1 = """\
title: some page
@@ -208,14 +203,14 @@ foo bar
convertibles = []
for i, txt in enumerate((page1, article1, article2)):
i = str(i)
with open(f'{inputdir}/{i}', 'w') as fh:
with open(f'{args.input_dir}/{i}', 'w') as fh:
fh.write(txt)
convertibles.append([i, i])
articles, pages = blag.process_markdown(
convertibles,
inputdir,
outdir,
args.input_dir,
args.output_dir,
page_template,
article_template
)
@@ -280,11 +275,5 @@ foo bar
blag.build(args)
def test_main(args):
arglist = ['build']
arglist.append(f'--input-dir={args.input_dir}')
arglist.append(f'--output-dir={args.output_dir}')
arglist.append(f'--static-dir={args.static_dir}')
arglist.append(f'--template-dir={args.template_dir}')
blag.main(arglist)
def test_main(cleandir):
blag.main(['build'])