From 63a529845a6e332ba374895b46a8eb77ca08cc12 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 12 Dec 2023 10:19:50 +0000 Subject: [PATCH] main.py, test suite: minor cleanups --- mmgen/main.py | 6 +++-- test/cmdtest_py_d/ct_misc.py | 2 -- test/hashfunc.py | 9 +++++-- test/tooltest.py | 47 ++++++++++++++++++------------------ test/tooltest2.py | 32 +++++++++++------------- test/unit_tests.py | 27 ++++++++++----------- 6 files changed, 61 insertions(+), 62 deletions(-) diff --git a/mmgen/main.py b/mmgen/main.py index f09787a3..52860313 100755 --- a/mmgen/main.py +++ b/mmgen/main.py @@ -33,10 +33,12 @@ def launch(*, mod=None, func=None, package='mmgen'): try: __import__(f'{package}.main_{mod}') if mod else func() except KeyboardInterrupt: - sys.stderr.write('\nUser interrupt\n') + from .color import yellow + sys.stderr.write(yellow('\nUser interrupt\n')) sys.exit(1) except EOFError: - sys.stderr.write('\nEnd of file\n') + from .color import yellow + sys.stderr.write(yellow('\nEnd of file\n')) sys.exit(1) except Exception as e: diff --git a/test/cmdtest_py_d/ct_misc.py b/test/cmdtest_py_d/ct_misc.py index f1c79d2f..2c1f2542 100755 --- a/test/cmdtest_py_d/ct_misc.py +++ b/test/cmdtest_py_d/ct_misc.py @@ -122,7 +122,6 @@ class CmdTestMisc(CmdTestBase): class CmdTestHelp(CmdTestBase): 'help, info and usage screens' networks = ('btc','ltc','bch','eth','xmr') - tmpdir_nums = [] passthru_opts = ('daemon_data_dir','rpc_port','coin','testnet') cmd_group = ( ('usage', (1,'usage message',[])), @@ -276,7 +275,6 @@ class CmdTestHelp(CmdTestBase): class CmdTestOutput(CmdTestBase): 'screen output' networks = ('btc',) - tmpdir_nums = [] cmd_group = ( ('output_gr', (1,"Greek text", [])), ('output_ru', (1,"Russian text", [])), diff --git a/test/hashfunc.py b/test/hashfunc.py index c62492ae..8813ea7d 100755 --- a/test/hashfunc.py +++ b/test/hashfunc.py @@ -168,9 +168,14 @@ from mmgen.cfg import Config if __name__ == '__main__': - assert len(sys.argv) in (2,3),"Test takes 1 or 2 arguments: test name, plus optional rounds count" + if len(sys.argv) not in (2,3): + die(1,'Test takes 1 or 2 arguments: test name, plus optional rounds count') + test = sys.argv[1].capitalize() - assert test in ('Sha256','Sha512','Keccak'), "Valid choices for test are 'sha256','sha512' or 'keccak'" + + if test not in ('Sha256','Sha512','Keccak'): + die(1, "Valid choices for test are 'sha256','sha512' or 'keccak'") + random_rounds = int(sys.argv[2]) if len(sys.argv) == 3 else 500 set_globals(Config()) diff --git a/test/tooltest.py b/test/tooltest.py index 9063b14a..63dcc92a 100755 --- a/test/tooltest.py +++ b/test/tooltest.py @@ -486,29 +486,28 @@ def do_cmds(cmd_group): cmdline = [cmd] + [os.path.join(tcfg['tmpdir'],fn) for fn in fns] getattr(tc,cmd)(*cmdline) -if __name__ == '__main__': - try: - if cfg._args: - if len(cfg._args) != 1: - die(1,'Only one command may be specified') - cmd = cfg._args[0] - if cmd in cmd_data: - cleandir(tcfg['tmpdir'],do_msg=True) - msg('Running tests for {}:'.format( cmd_data[cmd]['desc'] )) - do_cmds(cmd) - elif cmd == 'clean': - cleandir(tcfg['tmpdir'],do_msg=True) - sys.exit(0) - else: - die(1,f'{cmd!r}: unrecognized command') - else: +try: + if cfg._args: + if len(cfg._args) != 1: + die(1,'Only one command may be specified') + cmd = cfg._args[0] + if cmd in cmd_data: cleandir(tcfg['tmpdir'],do_msg=True) - for cmd in cmd_data: - msg('Running tests for {}:'.format( cmd_data[cmd]['desc'] )) - do_cmds(cmd) - if cmd is not list(cmd_data.keys())[-1]: - msg('') - except KeyboardInterrupt: - die(1,green('\nExiting at user request')) + msg('Running tests for {}:'.format( cmd_data[cmd]['desc'] )) + do_cmds(cmd) + elif cmd == 'clean': + cleandir(tcfg['tmpdir'],do_msg=True) + sys.exit(0) + else: + die(1,f'{cmd!r}: unrecognized command') + else: + cleandir(tcfg['tmpdir'],do_msg=True) + for cmd in cmd_data: + msg('Running tests for {}:'.format( cmd_data[cmd]['desc'] )) + do_cmds(cmd) + if cmd is not list(cmd_data.keys())[-1]: + msg('') +except KeyboardInterrupt: + die(1,green('\nExiting at user request')) - end_msg(int(time.time()) - start_time) +end_msg(int(time.time()) - start_time) diff --git a/test/tooltest2.py b/test/tooltest2.py index 34c3074f..79f56bd8 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -17,7 +17,7 @@ # along with this program. If not, see . """ -test/tooltest2.py: Simple tests for the 'mmgen-tool' utility +test/tooltest2.py: Test the 'mmgen-tool' utility """ # TODO: move all non-interactive 'mmgen-tool' tests in 'cmdtest.py' here @@ -34,6 +34,7 @@ except ImportError: from test.include.common import set_globals,end_msg,sample_text,init_coverage +from mmgen import main_tool from mmgen.cfg import Config from mmgen.color import green,blue,purple,cyan,gray from mmgen.util import msg,msg_r,Msg,is_hex_str,async_run,die @@ -968,6 +969,18 @@ def list_tested_cmds(): for gid in tests: Msg('\n'.join(tests[gid])) +async def main(): + if cfg._args: + for cmd in cfg._args: + if cmd in tests: + await do_group(cmd) + else: + if not await do_cmd_in_group(cmd): + die(1,f'{cmd!r}: not a recognized test or test group') + else: + for garg in tests: + await do_group(garg) + qmsg = cfg._util.qmsg vmsg = cfg._util.vmsg @@ -977,8 +990,6 @@ if cfg.tool_api: del tests['Wallet'] del tests['File'] -from mmgen import main_tool - if cfg.list_tests: Msg('Available tests:') for modname,cmdlist in main_tool.mods.items(): @@ -1008,21 +1019,6 @@ if cfg.fork: start_time = int(time.time()) -async def main(): - try: - if cfg._args: - for cmd in cfg._args: - if cmd in tests: - await do_group(cmd) - else: - if not await do_cmd_in_group(cmd): - die(1,f'{cmd!r}: not a recognized test or test group') - else: - for garg in tests: - await do_group(garg) - except KeyboardInterrupt: - die(1,green('\nExiting at user request')) - if __name__ == '__main__': async_run(main()) end_msg(int(time.time()) - start_time) diff --git a/test/unit_tests.py b/test/unit_tests.py index f14090b3..cfa54cfa 100755 --- a/test/unit_tests.py +++ b/test/unit_tests.py @@ -207,17 +207,16 @@ def run_test(test,subtest=None): if not mod.unit_test().run_test(test,UnitTestHelpers(test)): die(4,'Unit test {test!r} failed') -if __name__ == '__main__': - try: - for test in (cfg._args or all_tests): - if '.' in test: - test,subtest = test.split('.') - else: - subtest = None - if test not in all_tests: - die(1,f'{test!r}: test not recognized') - if test not in exclude: - run_test(test,subtest=subtest) - end_msg(int(time.time()) - start_time) - except KeyboardInterrupt: - die(1,green('\nExiting at user request')) +try: + for test in (cfg._args or all_tests): + if '.' in test: + test,subtest = test.split('.') + else: + subtest = None + if test not in all_tests: + die(1,f'{test!r}: test not recognized') + if test not in exclude: + run_test(test,subtest=subtest) + end_msg(int(time.time()) - start_time) +except KeyboardInterrupt: + die(1,green('\nExiting at user request'))