ct_base.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test.cmdtest_py_d.ct_base: Base class for the cmdtest.py test suite
  20. """
  21. import sys,os
  22. from mmgen.util import msg
  23. from ..include.common import cfg,write_to_file,read_from_file
  24. from .common import get_file_with_ext
  25. class CmdTestBase:
  26. 'initializer class for the cmdtest.py test suite'
  27. base_passthru_opts = ('data_dir','skip_cfg_file')
  28. passthru_opts = ()
  29. extra_spawn_args = []
  30. networks = ()
  31. segwit_opts_ok = False
  32. color = False
  33. need_daemon = False
  34. win_skip = False
  35. tmpdir_nums = []
  36. def __init__(self,trunner,cfgs,spawn):
  37. if hasattr(self,'name'): # init will be called multiple times for classes with multiple inheritance
  38. return
  39. self.name = type(self).__name__
  40. self.proto = cfg._proto
  41. self.tr = trunner
  42. self.cfgs = cfgs
  43. self.spawn = spawn
  44. self.have_dfl_wallet = False
  45. self.usr_rand_chars = (5,30)[bool(cfg.usr_random)]
  46. self.usr_rand_arg = f'-r{self.usr_rand_chars}'
  47. self.altcoin_pfx = '' if self.proto.base_coin == 'BTC' else '-'+self.proto.base_coin
  48. self.tn_ext = ('','.testnet')[self.proto.testnet]
  49. d = {'bch':'btc','btc':'btc','ltc':'ltc'}
  50. self.fork = d[self.proto.coin.lower()] if self.proto.coin.lower() in d else None
  51. if len(self.tmpdir_nums) == 1:
  52. self.tmpdir_num = self.tmpdir_nums[0]
  53. if self.tr:
  54. self.spawn_env = dict(self.tr.spawn_env)
  55. self.spawn_env['MMGEN_TEST_SUITE_ENABLE_COLOR'] = '1' if self.color else ''
  56. else:
  57. self.spawn_env = {} # placeholder
  58. @property
  59. def tmpdir(self):
  60. return os.path.join('test','tmp','{}{}'.format(self.tmpdir_num,'-α' if cfg.debug_utf8 else ''))
  61. def get_file_with_ext(self,ext,**kwargs):
  62. return get_file_with_ext(self.tmpdir,ext,**kwargs)
  63. def read_from_tmpfile(self,fn,binary=False):
  64. return read_from_file(os.path.join(self.tmpdir,fn),binary=binary)
  65. def write_to_tmpfile(self,fn,data,binary=False):
  66. return write_to_file(os.path.join(self.tmpdir,fn),data,binary=binary)
  67. def delete_tmpfile(self,fn):
  68. fn = os.path.join(self.tmpdir,fn)
  69. try:
  70. return os.unlink(fn)
  71. except:
  72. msg(f'{fn}: file does not exist or could not be deleted')
  73. def skip_for_win(self):
  74. if sys.platform == 'win32':
  75. msg(f'Skipping test {self.test_name!r}: not supported on MSys2 platform')
  76. return True
  77. else:
  78. return False
  79. def spawn_chk(self,*args,**kwargs):
  80. """
  81. Drop-in replacement for spawn() + t.read() for tests that spawn more than one process.
  82. Ensures that test script execution stops when a spawned process fails.
  83. """
  84. t = self.spawn(*args,**kwargs)
  85. t.read()
  86. t.ok()
  87. t.skip_ok = True
  88. return t