mirror of
https://github.com/shouptech/synthale.git
synced 2026-02-03 15:39:45 +00:00
Write recipes to output directory
This commit is contained in:
parent
c4b12a9877
commit
45d3a57dd0
5 changed files with 102 additions and 7 deletions
|
|
@ -2,14 +2,23 @@
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
|
from synthale.recipes import load_file, load_all_files, write_recipes
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.argument('input')
|
@click.argument('input_path')
|
||||||
@click.argument('output')
|
@click.argument('output_path')
|
||||||
def main(input, output):
|
def main(input_path, output_path):
|
||||||
"""Generate markdown files from BeerXML files.
|
"""Generate markdown files from BeerXML files.
|
||||||
|
|
||||||
INPUT is either a directory containing XML files, or an individual XML
|
INPUT_PATH is either a directory containing XML files, or an individual XML
|
||||||
file. OUTPUT is the directory to write the markdown files to.
|
file. OUTPUT_PATH is the directory to write the markdown files to.
|
||||||
"""
|
"""
|
||||||
pass
|
click.echo("Generating markdown from '{}'...".format(input_path))
|
||||||
|
|
||||||
|
if input_path.endswith('.xml'):
|
||||||
|
recipes = load_file(input_path)
|
||||||
|
else:
|
||||||
|
recipes = load_all_files(input_path)
|
||||||
|
|
||||||
|
write_recipes(recipes, output_path)
|
||||||
|
|
|
||||||
|
|
@ -70,3 +70,14 @@ def load_all_files(path):
|
||||||
recipes.extend(load_file(os.path.join(path, name)))
|
recipes.extend(load_file(os.path.join(path, name)))
|
||||||
|
|
||||||
return recipes
|
return recipes
|
||||||
|
|
||||||
|
|
||||||
|
def write_recipes(recipes, output_path):
|
||||||
|
"""Write `recipes` to `output_path`.
|
||||||
|
|
||||||
|
`recipes` is a list of MarkdownRecipe objects. `output_path` is a directory
|
||||||
|
to write the recipes to.
|
||||||
|
"""
|
||||||
|
for recipe in recipes:
|
||||||
|
with open(os.path.join(output_path, recipe.filename), 'w') as f:
|
||||||
|
f.write(recipe.markdown)
|
||||||
|
|
|
||||||
22
tests/conftest.py
Normal file
22
tests/conftest.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
"""Contains fixtures used during tests."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import pybeerxml
|
||||||
|
|
||||||
|
from synthale.recipes import MarkdownRecipe
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def xml_recipes():
|
||||||
|
"""Generate a list of pybeerxml.Recipe objects."""
|
||||||
|
coffeestout = pybeerxml.Parser().parse('tests/recipes/coffee-stout.xml')[0]
|
||||||
|
weizen = pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0]
|
||||||
|
return [coffeestout, weizen]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def md_recipes():
|
||||||
|
"""Generate a list of MarkdownRecipe objects."""
|
||||||
|
coffeestout = pybeerxml.Parser().parse('tests/recipes/coffee-stout.xml')[0]
|
||||||
|
weizen = pybeerxml.Parser().parse('tests/recipes/weizen.xml')[0]
|
||||||
|
return [MarkdownRecipe(coffeestout), MarkdownRecipe(weizen)]
|
||||||
42
tests/test_cli.py
Normal file
42
tests/test_cli.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
"""Test interactions with the CLI."""
|
||||||
|
|
||||||
|
from click.testing import CliRunner
|
||||||
|
|
||||||
|
from synthale.cli import main
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_help():
|
||||||
|
"""Test the help output of the main CLI command."""
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(main, ['--help'])
|
||||||
|
assert 'INPUT_PATH' in result.output
|
||||||
|
assert 'OUTPUT_PATH' in result.output
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_directory(tmpdir, md_recipes):
|
||||||
|
"""Test command where input is a directory."""
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(main, ['tests/recipes', str(tmpdir)])
|
||||||
|
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert result.output.startswith(
|
||||||
|
"Generating markdown from 'tests/recipes'..."
|
||||||
|
)
|
||||||
|
|
||||||
|
for recipe in md_recipes:
|
||||||
|
path = tmpdir.join(recipe.filename)
|
||||||
|
assert path.read() == recipe.markdown
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_file(tmpdir):
|
||||||
|
"""Test command where input is a file."""
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(main, ['tests/recipes/weizen.xml', str(tmpdir)])
|
||||||
|
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert result.output.startswith(
|
||||||
|
"Generating markdown from 'tests/recipes/weizen.xml'..."
|
||||||
|
)
|
||||||
|
|
||||||
|
path = tmpdir.join('weizen.md')
|
||||||
|
assert path.read().startswith('Weizen')
|
||||||
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
import pybeerxml
|
import pybeerxml
|
||||||
|
|
||||||
from synthale.recipes import MarkdownRecipe, load_file, load_all_files
|
from synthale.recipes import (
|
||||||
|
MarkdownRecipe, load_file, load_all_files, write_recipes
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_load_file(capsys):
|
def test_load_file(capsys):
|
||||||
|
|
@ -40,3 +42,12 @@ def test_markdown_recipe_markdown():
|
||||||
xml_recipe.name = 'Foobar'
|
xml_recipe.name = 'Foobar'
|
||||||
recipe = MarkdownRecipe(xml_recipe)
|
recipe = MarkdownRecipe(xml_recipe)
|
||||||
assert recipe.markdown == 'Foobar\n======'
|
assert recipe.markdown == 'Foobar\n======'
|
||||||
|
|
||||||
|
|
||||||
|
def test_write_recipes(md_recipes, tmpdir):
|
||||||
|
"""Test write_recipes function."""
|
||||||
|
write_recipes(md_recipes, str(tmpdir))
|
||||||
|
|
||||||
|
for recipe in md_recipes:
|
||||||
|
path = tmpdir.join(recipe.filename)
|
||||||
|
assert path.read() == recipe.markdown
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue