ct_help.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. test.cmdtest_d.ct_help: helpscreen test group for the cmdtest.py test suite
  12. """
  13. import sys, os, time
  14. from mmgen.util import ymsg
  15. from mmgen.cfg import gc
  16. from ..include.common import cfg
  17. from .ct_base import CmdTestBase
  18. class CmdTestHelp(CmdTestBase):
  19. 'help, info and usage screens'
  20. networks = ('btc', 'ltc', 'bch', 'eth', 'xmr', 'doge')
  21. passthru_opts = ('daemon_data_dir', 'rpc_port', 'coin', 'testnet')
  22. cmd_group = (
  23. ('usage1', (1, 'usage message (via --usage)', [])),
  24. ('usage2', (1, 'usage message (via bad invocation)', [])),
  25. ('version', (1, 'version message', [])),
  26. ('license', (1, 'license message', [])),
  27. ('helpscreens', (1, 'help screens', [])),
  28. ('longhelpscreens', (1, 'help screens (--longhelp)', [])),
  29. ('show_hash_presets', (1, 'info screen (--show-hash-presets)', [])),
  30. ('tool_help', (1, '‘mmgen-tool’ usage screen', [])),
  31. ('tool_cmd_usage', (1, '‘mmgen-tool’ usage screen', [])),
  32. ('test_help', (1, '‘cmdtest.py’ help screens', [])),
  33. ('tooltest_help', (1, '‘tooltest.py’ help screens', [])),
  34. )
  35. def usage1(self):
  36. t = self.spawn('mmgen-txsend', ['--usage'], no_passthru_opts=True)
  37. t.expect('USAGE: mmgen-txsend')
  38. return t
  39. def usage2(self):
  40. t = self.spawn('mmgen-walletgen', ['foo'], exit_val=1, no_passthru_opts=True)
  41. t.expect('USAGE: mmgen-walletgen')
  42. return t
  43. def version(self):
  44. t = self.spawn('mmgen-tool', ['--version'], exit_val=0)
  45. t.expect('MMGEN-TOOL version')
  46. return t
  47. def license(self):
  48. t = self.spawn(
  49. 'mmgen-walletconv',
  50. ['--stdout', '--in-fmt=hex', '--out-fmt=hex'],
  51. env = {'MMGEN_NO_LICENSE':''},
  52. no_passthru_opts = True)
  53. t.expect('to continue: ', 'w')
  54. t.expect('TERMS AND CONDITIONS') # start of GPL text
  55. if cfg.pexpect_spawn:
  56. t.send('G')
  57. t.expect('return for a fee.') # end of GPL text
  58. if cfg.pexpect_spawn:
  59. t.send('q')
  60. t.expect('to continue: ', 'c')
  61. t.expect('data: ', 'beadcafe'*4 + '\n')
  62. t.expect('to confirm: ', 'YES\n')
  63. return t
  64. def spawn_chk_expect(self, *args, **kwargs):
  65. expect = kwargs.pop('expect')
  66. t = self.spawn(*args, **kwargs)
  67. t.expect(expect)
  68. if t.pexpect_spawn:
  69. time.sleep(0.4)
  70. t.send('q')
  71. t.read()
  72. t.ok()
  73. t.skip_ok = True
  74. return t
  75. def helpscreens(self, arg='--help', scripts=(), expect='USAGE:.*OPTIONS:', pager=True):
  76. scripts = list(scripts or gc.cmd_caps_data)
  77. def gen_skiplist():
  78. for script in scripts:
  79. d = gc.cmd_caps_data[script]
  80. for cap in d.caps:
  81. if cap not in self.proto.mmcaps:
  82. yield script
  83. break
  84. else:
  85. if sys.platform == 'win32' and 'w' not in d.platforms:
  86. yield script
  87. elif d.coin and len(d.coin) > 1 and self.proto.coin.lower() not in (d.coin, 'btc'):
  88. yield script
  89. for cmdname in sorted(set(scripts) - set(list(gen_skiplist()))):
  90. t = self.spawn(
  91. f'mmgen-{cmdname}',
  92. [arg],
  93. extra_desc = f'(mmgen-{cmdname})',
  94. no_passthru_opts = not gc.cmd_caps_data[cmdname].proto)
  95. t.expect(expect, regex=True)
  96. if pager and t.pexpect_spawn:
  97. time.sleep(0.2)
  98. t.send('q')
  99. t.read()
  100. t.ok()
  101. t.skip_ok = True
  102. return t
  103. def longhelpscreens(self):
  104. return self.helpscreens(arg='--longhelp', expect='USAGE:.*GLOBAL OPTIONS:')
  105. def show_hash_presets(self):
  106. return self.helpscreens(
  107. arg = '--show-hash-presets',
  108. scripts = (
  109. 'walletgen', 'walletconv', 'walletchk', 'passchg', 'subwalletgen',
  110. 'addrgen', 'keygen', 'passgen',
  111. 'txsign', 'txdo', 'txbump'),
  112. expect = 'Available parameters.*Preset',
  113. pager = False)
  114. def tool_help(self):
  115. if os.getenv('PYTHONOPTIMIZE') == '2':
  116. ymsg('Skipping tool help with PYTHONOPTIMIZE=2 (no docstrings)')
  117. return 'skip'
  118. for arg in (
  119. 'help',
  120. 'usage',
  121. ):
  122. t = self.spawn_chk_expect(
  123. 'mmgen-tool',
  124. [arg],
  125. extra_desc = f'(mmgen-tool {arg})',
  126. expect = 'GENERAL USAGE')
  127. return t
  128. def tool_cmd_usage(self):
  129. if os.getenv('PYTHONOPTIMIZE') == '2':
  130. ymsg('Skipping tool cmd usage with PYTHONOPTIMIZE=2 (no docstrings)')
  131. return 'skip'
  132. from mmgen.main_tool import mods
  133. for cmdlist in mods.values():
  134. for cmd in cmdlist:
  135. t = self.spawn_chk('mmgen-tool', ['help', cmd], extra_desc=f'({cmd})')
  136. return t
  137. def test_help(self):
  138. for arg, expect in (
  139. ('--help', 'USAGE'),
  140. ('--list-cmds', 'AVAILABLE COMMANDS'),
  141. ('--list-cmd-groups', 'AVAILABLE COMMAND GROUPS')
  142. ):
  143. t = self.spawn_chk_expect(
  144. 'cmdtest.py',
  145. [arg],
  146. cmd_dir = 'test',
  147. extra_desc = f'(cmdtest.py {arg})',
  148. expect = expect)
  149. return t
  150. def tooltest_help(self):
  151. for arg, expect in (
  152. ('--list-cmds', 'Available commands'),
  153. ('--testing-status', 'Testing status')
  154. ):
  155. t = self.spawn_chk_expect(
  156. 'tooltest.py',
  157. [arg],
  158. cmd_dir = 'test',
  159. extra_desc = f'(tooltest.py {arg})',
  160. expect = expect)
  161. return t