diff --git a/src/synthale/cli.py b/src/synthale/cli.py index abca9f6..4b41418 100644 --- a/src/synthale/cli.py +++ b/src/synthale/cli.py @@ -2,14 +2,23 @@ import click +from synthale.recipes import load_file, load_all_files, write_recipes + @click.command() -@click.argument('input') -@click.argument('output') -def main(input, output): +@click.argument('input_path') +@click.argument('output_path') +def main(input_path, output_path): """Generate markdown files from BeerXML files. - INPUT is either a directory containing XML files, or an individual XML - file. OUTPUT is the directory to write the markdown files to. + INPUT_PATH is either a directory containing XML files, or an individual XML + 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) diff --git a/src/synthale/recipes.py b/src/synthale/recipes.py index 250b4ed..2b46916 100644 --- a/src/synthale/recipes.py +++ b/src/synthale/recipes.py @@ -70,3 +70,14 @@ def load_all_files(path): recipes.extend(load_file(os.path.join(path, name))) 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) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..41d96d7 --- /dev/null +++ b/tests/conftest.py @@ -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)] diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..e37c39b --- /dev/null +++ b/tests/test_cli.py @@ -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') diff --git a/tests/test_recipes.py b/tests/test_recipes.py index 689a1b0..1890072 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -2,7 +2,9 @@ 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): @@ -40,3 +42,12 @@ def test_markdown_recipe_markdown(): xml_recipe.name = 'Foobar' recipe = MarkdownRecipe(xml_recipe) 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