From f91777cfa4fa14554ebe2e38741d10f02411b660 Mon Sep 17 00:00:00 2001 From: Alex David Date: Sat, 18 Apr 2020 10:09:38 -0700 Subject: [PATCH] Support list or string for default_command config --- tests/test_cli.py | 21 ++++++++------------- todoman/cli.py | 16 +++++----------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index c294436a..7a03a928 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -847,28 +847,23 @@ def test_status_filtering(runner, todo_factory): assert 'two' in result.output -def test_invoke_command(runner, tmpdir): +def test_default_command_string(runner, tmpdir): path = tmpdir.join('config') path.write('default_command = flush\n', 'a') - flush = mock.MagicMock() - with patch.dict(cli.commands, values=dict(flush=flush)): - result = runner.invoke(cli, catch_exceptions=False) - - assert not result.exception - assert not result.output.strip() - assert flush.call_count == 1 + result = runner.invoke(cli, catch_exceptions=False) + assert ( + 'Are you sure you want to delete all done tasks?' in result.output + ) -def test_invoke_invalid_command(runner, tmpdir): +def test_default_command_list(runner, tmpdir): path = tmpdir.join('config') - path.write('default_command = DoTheRobot\n', 'a') + path.write('default_command = show, 1234\n', 'a') result = runner.invoke(cli, catch_exceptions=False) - - assert result.exception assert ( - 'Error: Invalid setting for [main][default_command]' in result.output + 'No todo with id 1234.' in result.output ) diff --git a/todoman/cli.py b/todoman/cli.py index 06e64f69..1ee7548d 100644 --- a/todoman/cli.py +++ b/todoman/cli.py @@ -307,19 +307,13 @@ def cli(click_ctx, colour, porcelain, humanize, config): locale.setlocale(locale.LC_TIME, "") if not click_ctx.invoked_subcommand: - invoke_command( - click_ctx, - ctx.config['main']['default_command'], - ) + invoke_command(ctx.config['main']['default_command']) -def invoke_command(click_ctx, command): - name, *args = command.split(' ') - if name not in cli.commands: - raise click.ClickException( - 'Invalid setting for [main][default_command]' - ) - click_ctx.invoke(cli.commands[command], args) +def invoke_command(argv): + if not isinstance(argv, type([])): + argv = [argv] + cli.main(argv) try: # pragma: no cover