unit/test_cli tests

This commit is contained in:
Lex Berezhny 2019-01-22 15:00:13 -05:00
parent 48533df523
commit 9ccb3fa2a3
3 changed files with 46 additions and 25 deletions

View file

@ -25,7 +25,8 @@ jobs:
- pip install -e .[test] - pip install -e .[test]
script: 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.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: after_success:
- coverage combine - coverage combine
- bash <(curl -s https://codecov.io/bash) - bash <(curl -s https://codecov.io/bash)

View file

@ -116,13 +116,19 @@ def set_kwargs(parsed_args):
def get_argument_parser(): def get_argument_parser():
main = argparse.ArgumentParser('lbrynet') main = argparse.ArgumentParser('lbrynet', add_help=False)
main.add_argument( main.add_argument(
'--version', dest='cli_version', action="store_true", '--version', dest='cli_version', action="store_true",
help='Show lbrynet CLI version and exit.' 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) CLIConfig.contribute_args(main)
sub = main.add_subparsers(dest='command') 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 = sub.add_parser('start', help='Start lbrynet server.')
start.add_argument( start.add_argument(
'--quiet', dest='quiet', action="store_true", '--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 ' help=('Enable debug output. Optionally specify loggers for which debug output '
'should selectively be applied.') 'should selectively be applied.')
) )
start.add_argument(
'--version', action="store_true",
help='Show daemon version and quit'
)
Config.contribute_args(start) Config.contribute_args(start)
api = Daemon.get_api_definitions() api = Daemon.get_api_definitions()
for group in sorted(api): for group in sorted(api):
@ -145,13 +147,16 @@ def get_argument_parser():
commands = group_command.add_subparsers(dest='subcommand') commands = group_command.add_subparsers(dest='subcommand')
for command in api[group]['commands']: for command in api[group]['commands']:
commands.add_parser(command['name'], help=command['doc'].strip().splitlines()[0]) 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 return main
def main(argv=None): def main(argv=None):
argv = argv or sys.argv[1:] argv = argv or sys.argv[1:]
parser = get_argument_parser() parser = get_argument_parser()
args = parser.parse_args(argv) args, command_args = parser.parse_known_args(argv)
conf = Config.create_from_arguments(args) conf = Config.create_from_arguments(args)
@ -182,25 +187,41 @@ def main(argv=None):
else: else:
log.info("Not connected to internet, unable to start") 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: elif args.command is not None:
if args.subcommand is not None: if args.subcommand is not None:
method = f'{args.command}_{args.subcommand}' method = f'{args.command}_{args.subcommand}'
command_before_args = args.subcommand elif args.command in ('status', 'publish', 'version', 'help', 'wallet_balance'):
elif args.command in ('status', 'publish', 'version'): method = args.command
method = command_before_args = args.command
else: else:
args.group_doc.print_help() args.group_doc.print_help()
return 0 return 0
command_index = 0 if method in Daemon.deprecated_methods:
for i, argv_i in enumerate(argv): new_method = Daemon.deprecated_methods[method].new_command
if argv_i == command_before_args: if new_method is None:
command_index = i print(f"{method} is permanently deprecated and does not have a replacement command.")
break return 0
print(f"{method} is deprecated, using {new_method}.")
method = new_method
fn = Daemon.callable_methods[method] fn = Daemon.callable_methods[method]
parsed = docopt(fn.__doc__, argv[command_index+1:]) parsed = docopt(fn.__doc__, command_args)
params = set_kwargs(parsed) params = set_kwargs(parsed)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(execute_command(conf, method, params)) loop.run_until_complete(execute_command(conf, method, params))

View file

@ -44,8 +44,7 @@ class CLITest(unittest.TestCase):
with contextlib.redirect_stdout(actual_output): with contextlib.redirect_stdout(actual_output):
main(['help']) main(['help'])
actual_output = actual_output.getvalue() actual_output = actual_output.getvalue()
self.assertSubstring('lbrynet - LBRY command line client.', actual_output) self.assertSubstring('usage: lbrynet [--version] [-h]', actual_output)
self.assertSubstring('USAGE', actual_output)
def test_help_for_command_command(self): def test_help_for_command_command(self):
actual_output = StringIO() actual_output = StringIO()
@ -64,7 +63,7 @@ class CLITest(unittest.TestCase):
def test_version_command(self): def test_version_command(self):
actual_output = StringIO() actual_output = StringIO()
with contextlib.redirect_stdout(actual_output): with contextlib.redirect_stdout(actual_output):
main(['version']) main(['--version'])
self.assertEqual( self.assertEqual(
actual_output.getvalue().strip(), actual_output.getvalue().strip(),
"lbrynet {lbrynet_version}".format(**get_platform()) "lbrynet {lbrynet_version}".format(**get_platform())
@ -72,12 +71,12 @@ class CLITest(unittest.TestCase):
def test_invalid_command(self): def test_invalid_command(self):
actual_output = StringIO() actual_output = StringIO()
with contextlib.redirect_stdout(actual_output): with contextlib.redirect_stderr(actual_output):
main(['publish1']) try:
self.assertEqual( main(['publish1'])
actual_output.getvalue().strip(), except SystemExit:
"publish1 is not a valid command." pass
) self.assertSubstring("invalid choice: 'publish1'", actual_output.getvalue())
def test_valid_command_daemon_not_started(self): def test_valid_command_daemon_not_started(self):
actual_output = StringIO() actual_output = StringIO()