2018-08-04 23:19:10 +02:00
|
|
|
import contextlib
|
|
|
|
from io import StringIO
|
2019-01-24 03:59:26 +01:00
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
import docopt
|
|
|
|
from torba.testcase import AsyncioTestCase
|
2018-08-04 23:19:10 +02:00
|
|
|
|
2018-11-04 21:10:49 +01:00
|
|
|
from lbrynet.extras.cli import normalize_value, main
|
2018-11-09 19:06:02 +01:00
|
|
|
from lbrynet.extras.system_info import get_platform
|
2019-01-24 03:59:26 +01:00
|
|
|
from lbrynet.extras.daemon.Daemon import Daemon
|
2018-08-04 23:19:10 +02:00
|
|
|
|
|
|
|
|
2019-01-24 01:02:11 +01:00
|
|
|
class CLITest(AsyncioTestCase):
|
2018-08-04 23:19:10 +02:00
|
|
|
|
2019-01-23 19:00:58 +01:00
|
|
|
@staticmethod
|
|
|
|
def shell(argv):
|
|
|
|
actual_output = StringIO()
|
|
|
|
with contextlib.redirect_stdout(actual_output):
|
|
|
|
with contextlib.redirect_stderr(actual_output):
|
|
|
|
try:
|
|
|
|
main(argv)
|
|
|
|
except SystemExit as e:
|
|
|
|
print(e.args[0])
|
|
|
|
return actual_output.getvalue().strip()
|
|
|
|
|
2018-08-04 23:19:10 +02:00
|
|
|
def test_guess_type(self):
|
|
|
|
self.assertEqual('0.3.8', normalize_value('0.3.8'))
|
2018-08-06 06:28:11 +02:00
|
|
|
self.assertEqual('0.3', normalize_value('0.3'))
|
2018-08-04 23:19:10 +02:00
|
|
|
self.assertEqual(3, normalize_value('3'))
|
|
|
|
self.assertEqual(3, normalize_value(3))
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
'VdNmakxFORPSyfCprAD/eDDPk5TY9QYtSA==',
|
|
|
|
normalize_value('VdNmakxFORPSyfCprAD/eDDPk5TY9QYtSA==')
|
|
|
|
)
|
|
|
|
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertTrue(normalize_value('TRUE'))
|
|
|
|
self.assertTrue(normalize_value('true'))
|
|
|
|
self.assertTrue(normalize_value('TrUe'))
|
|
|
|
self.assertFalse(normalize_value('FALSE'))
|
|
|
|
self.assertFalse(normalize_value('false'))
|
|
|
|
self.assertFalse(normalize_value('FaLsE'))
|
|
|
|
self.assertTrue(normalize_value(True))
|
2018-08-04 23:19:10 +02:00
|
|
|
|
|
|
|
self.assertEqual('3', normalize_value('3', key="uri"))
|
|
|
|
self.assertEqual('0.3', normalize_value('0.3', key="uri"))
|
|
|
|
self.assertEqual('True', normalize_value('True', key="uri"))
|
|
|
|
self.assertEqual('False', normalize_value('False', key="uri"))
|
|
|
|
|
|
|
|
self.assertEqual('3', normalize_value('3', key="file_name"))
|
|
|
|
self.assertEqual('3', normalize_value('3', key="name"))
|
|
|
|
self.assertEqual('3', normalize_value('3', key="download_directory"))
|
|
|
|
self.assertEqual('3', normalize_value('3', key="channel_name"))
|
|
|
|
|
|
|
|
self.assertEqual(3, normalize_value('3', key="some_other_thing"))
|
|
|
|
|
2019-01-23 19:00:58 +01:00
|
|
|
def test_help(self):
|
|
|
|
self.assertIn(
|
2019-01-25 00:49:15 +01:00
|
|
|
'Usage: lbrynet [-v] [--api HOST:PORT]', self.shell(['--help'])
|
2019-01-23 19:00:58 +01:00
|
|
|
)
|
|
|
|
# start is special command, with separate help handling
|
|
|
|
self.assertIn(
|
|
|
|
'--share-usage-data', self.shell(['start', '--help'])
|
|
|
|
)
|
|
|
|
# publish is ungrouped command, returns usage only implicitly
|
|
|
|
self.assertIn(
|
|
|
|
'publish (<name> | --name=<name>)', self.shell(['publish'])
|
|
|
|
)
|
|
|
|
# publish is ungrouped command, with explicit --help
|
|
|
|
self.assertIn(
|
|
|
|
'Make a new name claim and publish', self.shell(['publish', '--help'])
|
|
|
|
)
|
|
|
|
# account is a group, returns help implicitly
|
|
|
|
self.assertIn(
|
2019-01-25 00:49:15 +01:00
|
|
|
'Return the balance of an account',
|
2019-01-23 19:00:58 +01:00
|
|
|
self.shell(['account'])
|
|
|
|
)
|
|
|
|
# account is a group, with explicit --help
|
|
|
|
self.assertIn(
|
2019-01-25 00:49:15 +01:00
|
|
|
'Return the balance of an account',
|
2019-01-23 19:00:58 +01:00
|
|
|
self.shell(['account', '--help'])
|
|
|
|
)
|
|
|
|
# account add is a grouped command, returns usage implicitly
|
|
|
|
self.assertIn(
|
|
|
|
'account_add (<account_name> | --account_name=<account_name>)',
|
|
|
|
self.shell(['account', 'add'])
|
|
|
|
)
|
|
|
|
# account add is a grouped command, with explicit --help
|
|
|
|
self.assertIn(
|
|
|
|
'Add a previously created account from a seed,', self.shell(['account', 'add', '--help'])
|
|
|
|
)
|
|
|
|
# help for invalid command, with explicit --help
|
|
|
|
self.assertIn(
|
|
|
|
"invalid choice: 'publish1'", self.shell(['publish1', '--help'])
|
|
|
|
)
|
|
|
|
# help for invalid command, implicit
|
|
|
|
self.assertIn(
|
|
|
|
"invalid choice: 'publish1'", self.shell(['publish1'])
|
|
|
|
)
|
2018-08-04 23:19:10 +02:00
|
|
|
|
|
|
|
def test_version_command(self):
|
|
|
|
self.assertEqual(
|
2019-01-23 19:00:58 +01:00
|
|
|
"lbrynet {lbrynet_version}".format(**get_platform()), self.shell(['--version'])
|
2018-08-04 23:19:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_valid_command_daemon_not_started(self):
|
|
|
|
self.assertEqual(
|
2019-01-23 19:00:58 +01:00
|
|
|
"Could not connect to daemon. Are you sure it's running?",
|
|
|
|
self.shell(["publish", '--name=asd', '--bid=99'])
|
2018-08-04 23:19:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_deprecated_command_daemon_not_started(self):
|
|
|
|
actual_output = StringIO()
|
|
|
|
with contextlib.redirect_stdout(actual_output):
|
2019-01-23 19:00:58 +01:00
|
|
|
main(["wallet", "balance"])
|
2018-08-04 23:19:10 +02:00
|
|
|
self.assertEqual(
|
|
|
|
actual_output.getvalue().strip(),
|
2018-08-26 05:50:48 +02:00
|
|
|
"wallet_balance is deprecated, using account_balance.\n"
|
2018-08-04 23:19:10 +02:00
|
|
|
"Could not connect to daemon. Are you sure it's running?"
|
|
|
|
)
|
2019-01-24 03:59:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DaemonDocsTests(TestCase):
|
|
|
|
|
|
|
|
def test_can_parse_api_method_docs(self):
|
|
|
|
failures = []
|
|
|
|
for name, fn in Daemon.callable_methods.items():
|
|
|
|
try:
|
|
|
|
docopt.docopt(fn.__doc__, ())
|
|
|
|
except docopt.DocoptLanguageError as err:
|
|
|
|
failures.append(f"invalid docstring for {name}, {err.message}")
|
|
|
|
except docopt.DocoptExit:
|
|
|
|
pass
|
|
|
|
if failures:
|
|
|
|
self.fail("\n" + "\n".join(failures))
|