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 tempfile import TemporaryDirectory
from os import mkdir import os
import pytest import pytest
@@ -45,14 +45,32 @@ def tag_template(environment):
@pytest.fixture @pytest.fixture
def tempdir(): def tempdir():
config = """
[main]
base_url = https://example.com/
title = title
description = description
author = a. u. thor
"""
with TemporaryDirectory() as dir: with TemporaryDirectory() as dir:
for d in 'content', 'build', 'static', 'templates': 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 yield dir
@pytest.fixture @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: class NameSpace:
def __init__(self, **kwargs): def __init__(self, **kwargs):
@@ -60,9 +78,9 @@ def args(tempdir):
setattr(self, name, kwargs[name]) setattr(self, name, kwargs[name])
args = NameSpace( args = NameSpace(
input_dir=f'{tempdir}/content', input_dir='content',
output_dir=f'{tempdir}/build', output_dir='build',
static_dir=f'{tempdir}/static', static_dir='static',
template_dir=f'{tempdir}/templates', template_dir='templates',
) )
yield args yield args

View File

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