From 3eecc2cec3a8cf1187bb64126067d328bda6b041 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Wed, 2 Jan 2019 11:55:12 -0700 Subject: [PATCH] Print version option --- setup.py | 2 +- src/synthale/__init__.py | 2 ++ src/synthale/cli.py | 24 +++++++++++++++++-- tests/test_cli.py | 50 ++++++++++++++++++++++++++++------------ 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/setup.py b/setup.py index 202d422..a805f04 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ setup( ], entry_points=''' [console_scripts] - synthale=synthale.cli:main + synthale=synthale.cli:cli ''', classifiers=[ 'Development Status :: 4 - Beta', diff --git a/src/synthale/__init__.py b/src/synthale/__init__.py index 60298aa..eb9694c 100644 --- a/src/synthale/__init__.py +++ b/src/synthale/__init__.py @@ -14,3 +14,5 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . + +VERSION = '0.0.1' diff --git a/src/synthale/cli.py b/src/synthale/cli.py index df5dcf1..dd90f32 100644 --- a/src/synthale/cli.py +++ b/src/synthale/cli.py @@ -15,12 +15,32 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import platform + import click +from synthale import VERSION from synthale.recipes import load_file, load_all_files, write_recipes -@click.command() +@click.group() +def cli(): + """Synthale converts BeerXML files to markdown. + + Copyright (C) 2019 Mike Shoup""" + pass + + +@cli.command() +def version(): + """Print version and exit.""" + click.echo( + 'Synthale version: {}\n' + 'Python version: {}'.format(VERSION, platform.python_version()) + ) + + +@cli.command() @click.option( '--vol-unit', '-v', type=click.Choice(('gallons', 'liters')), @@ -47,7 +67,7 @@ from synthale.recipes import load_file, load_all_files, write_recipes ) @click.argument('input_path') @click.argument('output_path') -def main( +def generate( input_path, output_path, vol_unit, diff --git a/tests/test_cli.py b/tests/test_cli.py index 49fac46..85e9907 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -15,24 +15,27 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import platform + from click.testing import CliRunner import pytest -from synthale.cli import main +from synthale import VERSION +from synthale.cli import cli -def test_main_help(): - """Test the help output of the main CLI command.""" +def test_cli_help(): + """Test the help output of the cli CLI command.""" runner = CliRunner() - result = runner.invoke(main, ['--help']) + result = runner.invoke(cli, ['generate', '--help']) assert 'INPUT_PATH' in result.output assert 'OUTPUT_PATH' in result.output -def test_main_directory(tmpdir, md_recipes): +def test_cli_directory(tmpdir, md_recipes): """Test command where input is a directory.""" runner = CliRunner() - result = runner.invoke(main, ['tests/recipes', str(tmpdir)]) + result = runner.invoke(cli, ['generate', 'tests/recipes', str(tmpdir)]) assert result.exit_code == 0 assert result.output.startswith( @@ -44,10 +47,14 @@ def test_main_directory(tmpdir, md_recipes): assert path.read() == recipe.markdown -def test_main_file(tmpdir): +def test_cli_file(tmpdir): """Test command where input is a file.""" runner = CliRunner() - result = runner.invoke(main, ['tests/recipes/weizen.xml', str(tmpdir)]) + result = runner.invoke(cli, [ + 'generate', + 'tests/recipes/weizen.xml', + str(tmpdir) + ]) assert result.exit_code == 0 assert result.output.startswith( @@ -72,16 +79,29 @@ def test_main_file(tmpdir): ('--temp-unit', 'fahrenheit', '| 152.0 °F |'), ('--temp-unit', 'celsius', '| 66.7 °C |') )) -def test_main_units(tmpdir, option, flag, expected): +def test_cli_units(tmpdir, option, flag, expected): """Test units options.""" runner = CliRunner() - result = runner.invoke( - main, [option, flag, 'tests/recipes/coffee-stout.xml', str(tmpdir)] - ) + result = runner.invoke(cli, [ + 'generate', + option, + flag, + 'tests/recipes/coffee-stout.xml', + str(tmpdir) + ]) assert result.exit_code == 0 path = tmpdir.join('coffee_stout.md') - result = path.read() - print(result) - assert expected in result + assert expected in path.read() + + +def test_cli_version(): + """Test version output.""" + runner = CliRunner() + result = runner.invoke(cli, ['version']) + assert result.exit_code == 0 + assert result.output.startswith( + 'Synthale version: {}\n' + 'Python version: {}'.format(VERSION, platform.python_version()) + )