From 9ccb3fa2a335f3ba1b6bcbf2da76981ab9d63b51 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Tue, 22 Jan 2019 15:00:13 -0500 Subject: [PATCH] unit/test_cli tests --- .travis.yml | 3 ++- lbrynet/extras/cli.py | 51 +++++++++++++++++++++++++++++------------- tests/unit/test_cli.py | 17 +++++++------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index ade9f64a8..346867c11 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,8 @@ jobs: - pip install -e .[test] script: - HOME=/tmp coverage run -p --source=lbrynet -m twisted.trial --reactor=asyncio tests.unit.core tests.unit.cryptstream tests.unit.database tests.unit.dht tests.unit.lbryfilemanager tests.unit.lbrynet_daemon tests.unit.schema tests.unit.wallet tests.unit.components tests.unit.test_conf - #- HOME=/tmp coverage run -p --source=lbrynet -m twisted.trial --reactor=asyncio tests.unit.test_cli tests.unit.analytics + - HOME=/tmp coverage run -p --source=lbrynet -m twisted.trial --reactor=asyncio tests.unit.test_cli + #- HOME=/tmp coverage run -p --source=lbrynet -m twisted.trial --reactor=asyncio tests.unit.analytics after_success: - coverage combine - bash <(curl -s https://codecov.io/bash) diff --git a/lbrynet/extras/cli.py b/lbrynet/extras/cli.py index acac68f34..5479cdfc0 100644 --- a/lbrynet/extras/cli.py +++ b/lbrynet/extras/cli.py @@ -116,13 +116,19 @@ def set_kwargs(parsed_args): def get_argument_parser(): - main = argparse.ArgumentParser('lbrynet') + main = argparse.ArgumentParser('lbrynet', add_help=False) main.add_argument( '--version', dest='cli_version', action="store_true", help='Show lbrynet CLI version and exit.' ) + main.add_argument( + '-h', '--help', dest='help', action="store_true", + help='Show this help message and exit' + ) CLIConfig.contribute_args(main) sub = main.add_subparsers(dest='command') + help = sub.add_parser('help', help='Detailed help for remote commands.') + help.add_argument('help_command', nargs='*') start = sub.add_parser('start', help='Start lbrynet server.') start.add_argument( '--quiet', dest='quiet', action="store_true", @@ -133,10 +139,6 @@ def get_argument_parser(): help=('Enable debug output. Optionally specify loggers for which debug output ' 'should selectively be applied.') ) - start.add_argument( - '--version', action="store_true", - help='Show daemon version and quit' - ) Config.contribute_args(start) api = Daemon.get_api_definitions() for group in sorted(api): @@ -145,13 +147,16 @@ def get_argument_parser(): commands = group_command.add_subparsers(dest='subcommand') for command in api[group]['commands']: commands.add_parser(command['name'], help=command['doc'].strip().splitlines()[0]) + for deprecated in Daemon.deprecated_methods: + group_command = sub.add_parser(deprecated) + group_command.add_subparsers(dest='subcommand') return main def main(argv=None): argv = argv or sys.argv[1:] parser = get_argument_parser() - args = parser.parse_args(argv) + args, command_args = parser.parse_known_args(argv) conf = Config.create_from_arguments(args) @@ -182,25 +187,41 @@ def main(argv=None): else: log.info("Not connected to internet, unable to start") + elif args.command == 'help': + + if args.help_command: + method = '_'.join(args.help_command) + else: + parser.print_help() + return 0 + + if method not in Daemon.callable_methods: + print('Invalid command name: {method}') + return 1 + + fn = Daemon.callable_methods[method] + print(fn.__doc__) + elif args.command is not None: if args.subcommand is not None: method = f'{args.command}_{args.subcommand}' - command_before_args = args.subcommand - elif args.command in ('status', 'publish', 'version'): - method = command_before_args = args.command + elif args.command in ('status', 'publish', 'version', 'help', 'wallet_balance'): + method = args.command else: args.group_doc.print_help() return 0 - command_index = 0 - for i, argv_i in enumerate(argv): - if argv_i == command_before_args: - command_index = i - break + if method in Daemon.deprecated_methods: + new_method = Daemon.deprecated_methods[method].new_command + if new_method is None: + print(f"{method} is permanently deprecated and does not have a replacement command.") + return 0 + print(f"{method} is deprecated, using {new_method}.") + method = new_method fn = Daemon.callable_methods[method] - parsed = docopt(fn.__doc__, argv[command_index+1:]) + parsed = docopt(fn.__doc__, command_args) params = set_kwargs(parsed) loop = asyncio.get_event_loop() loop.run_until_complete(execute_command(conf, method, params)) diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 772b3ae09..66143d5fe 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -44,8 +44,7 @@ class CLITest(unittest.TestCase): with contextlib.redirect_stdout(actual_output): main(['help']) actual_output = actual_output.getvalue() - self.assertSubstring('lbrynet - LBRY command line client.', actual_output) - self.assertSubstring('USAGE', actual_output) + self.assertSubstring('usage: lbrynet [--version] [-h]', actual_output) def test_help_for_command_command(self): actual_output = StringIO() @@ -64,7 +63,7 @@ class CLITest(unittest.TestCase): def test_version_command(self): actual_output = StringIO() with contextlib.redirect_stdout(actual_output): - main(['version']) + main(['--version']) self.assertEqual( actual_output.getvalue().strip(), "lbrynet {lbrynet_version}".format(**get_platform()) @@ -72,12 +71,12 @@ class CLITest(unittest.TestCase): def test_invalid_command(self): actual_output = StringIO() - with contextlib.redirect_stdout(actual_output): - main(['publish1']) - self.assertEqual( - actual_output.getvalue().strip(), - "publish1 is not a valid command." - ) + with contextlib.redirect_stderr(actual_output): + try: + main(['publish1']) + except SystemExit: + pass + self.assertSubstring("invalid choice: 'publish1'", actual_output.getvalue()) def test_valid_command_daemon_not_started(self): actual_output = StringIO()