Browse Source

main.py, test suite: minor cleanups

The MMGen Project 11 months ago
parent
commit
63a529845a
6 changed files with 62 additions and 63 deletions
  1. 4 2
      mmgen/main.py
  2. 0 2
      test/cmdtest_py_d/ct_misc.py
  3. 7 2
      test/hashfunc.py
  4. 24 25
      test/tooltest.py
  5. 14 18
      test/tooltest2.py
  6. 13 14
      test/unit_tests.py

+ 4 - 2
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:
 

+ 0 - 2
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", [])),

+ 7 - 2
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())

+ 24 - 25
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'))
-
-	end_msg(int(time.time()) - start_time)
+			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)

+ 14 - 18
test/tooltest2.py

@@ -17,7 +17,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
-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)

+ 13 - 14
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'))