From 653827c15e93165a722340b199b8708d83e660b3 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 13 Oct 2023 09:51:14 +0000 Subject: [PATCH] test suite: minor fixes and cleanups --- mmgen/autosign.py | 2 +- scripts/exec_wrapper.py | 3 +-- test/cmdtest_py_d/ct_base.py | 9 +-------- test/cmdtest_py_d/ct_chainsplit.py | 2 +- test/cmdtest_py_d/ct_misc.py | 2 +- test/cmdtest_py_d/ct_regtest.py | 8 +++++--- test/cmdtest_py_d/ct_shared.py | 10 +++++++++- test/hashfunc.py | 10 +++++++--- test/unit_tests.py | 6 +++++- test/unit_tests_d/ut_dep.py | 8 ++++++-- test/unit_tests_d/ut_testdep.py | 8 ++++---- test/unit_tests_d/ut_tx_deserialize.py | 3 ++- 12 files changed, 43 insertions(+), 28 deletions(-) diff --git a/mmgen/autosign.py b/mmgen/autosign.py index fcdbfc5f..12b694b5 100755 --- a/mmgen/autosign.py +++ b/mmgen/autosign.py @@ -125,7 +125,7 @@ class Signable: for f in bad_files: yield red(f.name) - class xmr_signable(transaction): # virtual class + class xmr_signable(transaction): # mixin class def need_daemon_restart(self,m,new_idx): old_idx = self.parent.xmr_cur_wallet_idx diff --git a/scripts/exec_wrapper.py b/scripts/exec_wrapper.py index eb99003f..1d749dbe 100755 --- a/scripts/exec_wrapper.py +++ b/scripts/exec_wrapper.py @@ -74,8 +74,7 @@ def exec_wrapper_write_traceback(e,exit_val): sys.stdout.write('{}\n{}\n'.format( c.yellow( '\n'.join(tb_lines) ), c.red(exc_line) )) - from test.include.common import cmdtest_py_error_fn - with open(cmdtest_py_error_fn,'w') as fp: + with open('test.err','w') as fp: fp.write('\n'.join(tb_lines + [exc_line])) print(c.blue('{} script exited with error').format( diff --git a/test/cmdtest_py_d/ct_base.py b/test/cmdtest_py_d/ct_base.py index c8c74a2c..abb6fbb9 100755 --- a/test/cmdtest_py_d/ct_base.py +++ b/test/cmdtest_py_d/ct_base.py @@ -37,6 +37,7 @@ class CmdTestBase: color = False need_daemon = False win_skip = False + tmpdir_nums = [] def __init__(self,trunner,cfgs,spawn): if hasattr(self,'name'): # init will be called multiple times for classes with multiple inheritance @@ -65,14 +66,6 @@ class CmdTestBase: def tmpdir(self): return os.path.join('test','tmp','{}{}'.format(self.tmpdir_num,'-α' if cfg.debug_utf8 else '')) - @property - def segwit_mmtype(self): - return ('segwit','bech32')[bool(cfg.bech32)] if self.segwit else None - - @property - def segwit_arg(self): - return ['--type=' + self.segwit_mmtype] if self.segwit_mmtype else [] - def get_file_with_ext(self,ext,**kwargs): return get_file_with_ext(self.tmpdir,ext,**kwargs) diff --git a/test/cmdtest_py_d/ct_chainsplit.py b/test/cmdtest_py_d/ct_chainsplit.py index 9d674fce..387be51f 100755 --- a/test/cmdtest_py_d/ct_chainsplit.py +++ b/test/cmdtest_py_d/ct_chainsplit.py @@ -23,7 +23,7 @@ This module is unmaintained and currently non-functional from mmgen.util import die -from .common import get_file_with_ext +from .common import get_file_with_ext,rt_pw from .ct_regtest import CmdTestRegtest class CmdTestChainsplit(CmdTestRegtest): diff --git a/test/cmdtest_py_d/ct_misc.py b/test/cmdtest_py_d/ct_misc.py index 01508342..93eab602 100755 --- a/test/cmdtest_py_d/ct_misc.py +++ b/test/cmdtest_py_d/ct_misc.py @@ -25,7 +25,7 @@ import sys,os,re,time from mmgen.util import ymsg from ..include.common import cfg,start_test_daemons,stop_test_daemons,imsg -from .common import get_file_with_ext +from .common import get_file_with_ext,dfl_words_file from .ct_base import CmdTestBase from .ct_main import CmdTestMain diff --git a/test/cmdtest_py_d/ct_regtest.py b/test/cmdtest_py_d/ct_regtest.py index f66137a2..a475bb74 100755 --- a/test/cmdtest_py_d/ct_regtest.py +++ b/test/cmdtest_py_d/ct_regtest.py @@ -20,7 +20,7 @@ test.cmdtest_py_d.ct_regtest: Regtest tests for the cmdtest.py test suite """ -import os,json,time,re +import sys,os,json,time,re from decimal import Decimal from mmgen.color import yellow @@ -59,6 +59,7 @@ pat_date_time = r'\b\d\d\d\d-\d\d-\d\d\s+\d\d:\d\d\b' dfl_wcls = get_wallet_cls('mmgen') +tx_fee = rtFundAmt = rtFee = rtBals = rtBals_gb = rtBobOp3 = rtAmts = {} # pylint rt_pw = 'abc-α' rt_data = { 'tx_fee': {'btc':'0.0001','bch':'0.001','ltc':'0.01'}, @@ -172,6 +173,8 @@ class CmdTestRegtest(CmdTestBase,CmdTestShared): color = True deterministic = False test_rbf = False + proto = None # pylint + cmd_group_in = ( ('setup', 'regtest (Bob and Alice) mode setup'), ('subgroup.misc', []), @@ -445,9 +448,8 @@ class CmdTestRegtest(CmdTestBase,CmdTestShared): self.proto = init_proto( cfg, self.proto.coin, network='regtest', need_amt=True ) coin = self.proto.coin.lower() - import test.cmdtest_py_d.ct_regtest as rt_mod for k in rt_data: - setattr( rt_mod, k, rt_data[k][coin] if coin in rt_data[k] else None ) + setattr( sys.modules[__name__], k, rt_data[k][coin] if coin in rt_data[k] else None ) if self.proto.coin == 'BTC': self.test_rbf = True # tests are non-coin-dependent, so run just once for BTC diff --git a/test/cmdtest_py_d/ct_shared.py b/test/cmdtest_py_d/ct_shared.py index 858cdc3e..a29fe9b5 100755 --- a/test/cmdtest_py_d/ct_shared.py +++ b/test/cmdtest_py_d/ct_shared.py @@ -23,12 +23,20 @@ test.cmdtest_py_d.ct_shared: Shared methods for the cmdtest.py test suite from mmgen.util import get_extension from mmgen.wallet import get_wallet_cls -from ..include.common import cmp_or_die,strip_ansi_escapes,joinpath +from ..include.common import cfg,cmp_or_die,strip_ansi_escapes,joinpath from .common import ref_bw_file,ref_bw_hash_preset,ref_dir class CmdTestShared: 'shared methods for the cmdtest.py test suite' + @property + def segwit_mmtype(self): + return ('segwit','bech32')[bool(cfg.bech32)] if self.segwit else None + + @property + def segwit_arg(self): + return ['--type=' + self.segwit_mmtype] if self.segwit_mmtype else [] + def txcreate_ui_common( self, t, diff --git a/test/hashfunc.py b/test/hashfunc.py index 5f713271..cd6fc3ec 100755 --- a/test/hashfunc.py +++ b/test/hashfunc.py @@ -119,7 +119,10 @@ class TestKeccak(TestHashFunc): from Cryptodome.Hash import keccak self.hashlib = hashlib else: - import sha3 + try: + import sha3 + except ImportError as e: + die(2,str(e)) self.hashlib = sha3 def test_constants(self): @@ -172,9 +175,10 @@ class TestSha512(TestSha2): 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 ) +from test.include.common import getrand,set_globals +from mmgen.cfg import Config + if __name__ == '__main__': - from test.include.common import getrand,set_globals - from mmgen.cfg import Config assert len(sys.argv) in (2,3),"Test takes 1 or 2 arguments: test name, plus optional rounds count" test = sys.argv[1].capitalize() diff --git a/test/unit_tests.py b/test/unit_tests.py index 0130ed8a..eff9641a 100755 --- a/test/unit_tests.py +++ b/test/unit_tests.py @@ -35,10 +35,14 @@ if not os.getenv('MMGEN_DEVTOOLS'): from mmgen.cfg import Config,gc from mmgen.color import green,gray -from mmgen.util import msg,gmsg,ymsg,Msg,die,async_run +from mmgen.util import msg,gmsg,ymsg,Msg,async_run from test.include.common import set_globals,end_msg +def die(ev,s): + msg(s) + sys.exit(ev) + opts_data = { 'text': { 'desc': "Unit tests for the MMGen suite", diff --git a/test/unit_tests_d/ut_dep.py b/test/unit_tests_d/ut_dep.py index de47e06f..3cdd2fc1 100755 --- a/test/unit_tests_d/ut_dep.py +++ b/test/unit_tests_d/ut_dep.py @@ -35,10 +35,14 @@ class unit_tests: if python_version >= '3.11': ut.skip_msg(f'Python version {python_version}') else: - from sha3 import keccak_256 + try: + from sha3 import keccak_256 + except ImportError as e: + ymsg(str(e)) + return False return True - def pycryptodomex(self,name,ut): # ETH,XMR + def pycryptodomex(self,name,ut): # ETH,XMR (keccak) from mmgen.pyversion import python_version if python_version >= '3.11' or sys.platform == 'win32': try: diff --git a/test/unit_tests_d/ut_testdep.py b/test/unit_tests_d/ut_testdep.py index 037a59f1..4fd6f397 100755 --- a/test/unit_tests_d/ut_testdep.py +++ b/test/unit_tests_d/ut_testdep.py @@ -5,7 +5,7 @@ test.unit_tests_d.ut_testdep: test dependency unit tests for the MMGen suite """ import sys,os -from subprocess import run,PIPE +from subprocess import run,DEVNULL from mmgen.util import ymsg @@ -25,7 +25,7 @@ class unit_tests: def losetup(self,name,ut): os.stat('/dev/loop0') - run(['/sbin/losetup','-f'],check=True,stdout=PIPE) + run(['/sbin/losetup','-f'],check=True,stdout=DEVNULL) return True def pycoin(self,name,ut): @@ -42,11 +42,11 @@ class unit_tests: return True def keyconv(self,name,ut): - run(['keyconv','-G','ltc'],stdout=PIPE,stderr=PIPE,check=True) + run(['keyconv','-G','ltc'],stdout=DEVNULL,stderr=DEVNULL,check=True) return True def zcash_mini(self,name,ut): - run(['zcash-mini'],stdout=PIPE,check=True) + run(['zcash-mini'],stdout=DEVNULL,check=True) return True def ethkey(self,name,ut): diff --git a/test/unit_tests_d/ut_tx_deserialize.py b/test/unit_tests_d/ut_tx_deserialize.py index 338bf76a..a32c62c7 100755 --- a/test/unit_tests_d/ut_tx_deserialize.py +++ b/test/unit_tests_d/ut_tx_deserialize.py @@ -88,7 +88,8 @@ async def test_tx(tx_proto,tx_hex,desc,n): assert A == B, fs.format(i,A,B) async def do_mmgen_ref(daemons,fns,name,desc): - start_test_daemons(*daemons) + # NB: remove_datadir is required here for some reason (seems to be Bitcoin Core version-dependent) + start_test_daemons(*daemons,remove_datadir=True) print_info(name,desc) for n,fn in enumerate(fns): tx = await CompletedTX( cfg=cfg, filename=fn, quiet_open=True )