ct_help.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line 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_py_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, proto_cmds
  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. ('usage', (1,'usage message (via bad invocation)',[])),
  24. ('version', (1,'version message',[])),
  25. ('license', (1,'license message',[])),
  26. ('helpscreens', (1,'help screens', [])),
  27. ('longhelpscreens', (1,'help screens (--longhelp)',[])),
  28. ('show_hash_presets', (1,'info screen (--show-hash-presets)',[])),
  29. ('tool_help', (1,"'mmgen-tool' usage screen",[])),
  30. ('tool_cmd_usage', (1,"'mmgen-tool' usage screen",[])),
  31. ('test_help', (1,"'cmdtest.py' help screens",[])),
  32. ('tooltest_help', (1,"'tooltest.py' help screens",[])),
  33. )
  34. def usage(self):
  35. t = self.spawn('mmgen-walletgen', ['foo'], exit_val=1, no_passthru_opts=True)
  36. t.expect('USAGE: mmgen-walletgen')
  37. return t
  38. def version(self):
  39. t = self.spawn('mmgen-tool', ['--version'], exit_val=0)
  40. t.expect('MMGEN-TOOL version')
  41. return t
  42. def license(self):
  43. t = self.spawn(
  44. 'mmgen-walletconv',
  45. ['--stdout', '--in-fmt=hex', '--out-fmt=hex'],
  46. env = {'MMGEN_NO_LICENSE':''},
  47. no_passthru_opts = True)
  48. t.expect('to continue: ', 'w')
  49. t.expect('TERMS AND CONDITIONS') # start of GPL text
  50. if cfg.pexpect_spawn:
  51. t.send('G')
  52. t.expect('return for a fee.') # end of GPL text
  53. if cfg.pexpect_spawn:
  54. t.send('q')
  55. t.expect('to continue: ', 'c')
  56. t.expect('data: ','beadcafe'*4 + '\n')
  57. t.expect('to confirm: ', 'YES\n')
  58. return t
  59. def spawn_chk_expect(self,*args,**kwargs):
  60. expect = kwargs.pop('expect')
  61. t = self.spawn(*args,**kwargs)
  62. t.expect(expect)
  63. if t.pexpect_spawn:
  64. time.sleep(0.4)
  65. t.send('q')
  66. t.read()
  67. t.ok()
  68. t.skip_ok = True
  69. return t
  70. def helpscreens(self,arg='--help',scripts=(),expect='USAGE:.*OPTIONS:',pager=True):
  71. scripts = list(scripts) or [s.replace('mmgen-','') for s in os.listdir('cmds')]
  72. if 'tx' not in self.proto.mmcaps:
  73. scripts = [s for s in scripts if not (s == 'regtest' or s.startswith('tx'))]
  74. if 'xmrwallet' in scripts and (cfg.no_altcoin or not self.proto.coin in ('BTC','XMR')):
  75. scripts.remove('xmrwallet')
  76. if 'autosign' in scripts and sys.platform == 'win32':
  77. scripts.remove('autosign')
  78. for cmdname in sorted(scripts):
  79. t = self.spawn(
  80. f'mmgen-{cmdname}',
  81. [arg],
  82. extra_desc = f'(mmgen-{cmdname})',
  83. no_passthru_opts = not cmdname in proto_cmds)
  84. t.expect(expect,regex=True)
  85. if pager and t.pexpect_spawn:
  86. time.sleep(0.2)
  87. t.send('q')
  88. t.read()
  89. t.ok()
  90. t.skip_ok = True
  91. return t
  92. def longhelpscreens(self):
  93. return self.helpscreens(arg='--longhelp',expect='USAGE:.*LONG OPTIONS:')
  94. def show_hash_presets(self):
  95. return self.helpscreens(
  96. arg = '--show-hash-presets',
  97. scripts = (
  98. 'walletgen','walletconv','walletchk','passchg','subwalletgen',
  99. 'addrgen','keygen','passgen',
  100. 'txsign','txdo','txbump'),
  101. expect = 'Available parameters.*Preset',
  102. pager = False )
  103. def tool_help(self):
  104. if os.getenv('PYTHONOPTIMIZE') == '2':
  105. ymsg('Skipping tool help with PYTHONOPTIMIZE=2 (no docstrings)')
  106. return 'skip'
  107. for arg in (
  108. 'help',
  109. 'usage',
  110. ):
  111. t = self.spawn_chk_expect(
  112. 'mmgen-tool',
  113. [arg],
  114. extra_desc = f'(mmgen-tool {arg})',
  115. expect = 'GENERAL USAGE' )
  116. return t
  117. def tool_cmd_usage(self):
  118. if os.getenv('PYTHONOPTIMIZE') == '2':
  119. ymsg('Skipping tool cmd usage with PYTHONOPTIMIZE=2 (no docstrings)')
  120. return 'skip'
  121. from mmgen.main_tool import mods
  122. for cmdlist in mods.values():
  123. for cmd in cmdlist:
  124. t = self.spawn_chk( 'mmgen-tool', ['help',cmd], extra_desc=f'({cmd})' )
  125. return t
  126. def test_help(self):
  127. for arg,expect in (
  128. ('--help','USAGE'),
  129. ('--list-cmds','AVAILABLE COMMANDS'),
  130. ('--list-cmd-groups','AVAILABLE COMMAND GROUPS')
  131. ):
  132. t = self.spawn_chk_expect(
  133. 'cmdtest.py',
  134. [arg],
  135. cmd_dir = 'test',
  136. extra_desc = f'(cmdtest.py {arg})',
  137. expect = expect )
  138. return t
  139. def tooltest_help(self):
  140. for arg,expect in (
  141. ('--list-cmds','Available commands'),
  142. ('--testing-status','Testing status')
  143. ):
  144. t = self.spawn_chk_expect(
  145. 'tooltest.py',
  146. [arg],
  147. cmd_dir = 'test',
  148. extra_desc = f'(tooltest.py {arg})',
  149. expect = expect )
  150. return t