Skip to content

Commit

Permalink
unit/test_cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eukreign committed Jan 22, 2019
1 parent 5c1a79f commit 2a77b0c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -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)
Expand Down
51 changes: 36 additions & 15 deletions lbrynet/extras/cli.py
Expand Up @@ -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",
Expand All @@ -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):
Expand All @@ -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)

Expand Down Expand Up @@ -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))
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/test_cli.py
Expand Up @@ -44,8 +44,7 @@ def test_help_command(self):
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()
Expand All @@ -64,20 +63,20 @@ def test_help_for_command_command_with_invalid_command(self):
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())
)

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()
Expand Down

0 comments on commit 2a77b0c

Please sign in to comment.