ts_base.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.test_py_d.ts_base: Base class for the test.py test suite
  20. """
  21. import os
  22. from mmgen.cfg import gc
  23. from ..include.common import *
  24. from .common import *
  25. class TestSuiteBase(object):
  26. 'initializer class for the test.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. def __init__(self,trunner,cfgs,spawn):
  35. self.proto = cfg._proto
  36. self.tr = trunner
  37. self.cfgs = cfgs
  38. self.spawn = spawn
  39. self.have_dfl_wallet = False
  40. self.usr_rand_chars = (5,30)[bool(cfg.usr_random)]
  41. self.usr_rand_arg = f'-r{self.usr_rand_chars}'
  42. self.altcoin_pfx = '' if self.proto.base_coin == 'BTC' else '-'+self.proto.base_coin
  43. self.tn_ext = ('','.testnet')[self.proto.testnet]
  44. d = {'bch':'btc','btc':'btc','ltc':'ltc'}
  45. self.fork = d[self.proto.coin.lower()] if self.proto.coin.lower() in d else None
  46. if len(self.tmpdir_nums) == 1:
  47. self.tmpdir_num = self.tmpdir_nums[0]
  48. @property
  49. def tmpdir(self):
  50. return os.path.join('test','tmp','{}{}'.format(self.tmpdir_num,'-α' if cfg.debug_utf8 else ''))
  51. @property
  52. def segwit_mmtype(self):
  53. return ('segwit','bech32')[bool(cfg.bech32)] if self.segwit else None
  54. @property
  55. def segwit_arg(self):
  56. return ['--type=' + self.segwit_mmtype] if self.segwit_mmtype else []
  57. def get_file_with_ext(self,ext,**kwargs):
  58. return get_file_with_ext(self.tmpdir,ext,**kwargs)
  59. def read_from_tmpfile(self,fn,binary=False):
  60. return read_from_file(os.path.join(self.tmpdir,fn),binary=binary)
  61. def write_to_tmpfile(self,fn,data,binary=False):
  62. return write_to_file(os.path.join(self.tmpdir,fn),data,binary=binary)
  63. def skip_for_win(self):
  64. if gc.platform == 'win':
  65. msg(f'Skipping test {self.test_name!r}: not supported on MSys2 platform')
  66. return True
  67. else:
  68. return False
  69. def spawn_chk(self,*args,**kwargs):
  70. """
  71. Drop-in replacement for spawn() + t.read() for tests that spawn more than one process.
  72. Ensures that test script execution stops when a spawned process fails.
  73. """
  74. t = self.spawn(*args,**kwargs)
  75. t.read()
  76. t.ok()
  77. t.skip_ok = True
  78. return t