Browse Source

test.py: allow color for some test groups; minor fixes, cleanups

The MMGen Project 4 years ago
parent
commit
d2055c93da

+ 4 - 1
mmgen/globalvars.py

@@ -120,7 +120,10 @@ class GlobalContext(Lockable):
 	terminal_width       = 0
 
 	mnemonic_entry_modes = {}
-	color = sys.stdout.isatty()
+	color = bool(
+		( sys.stdout.isatty() and not os.getenv('MMGEN_TEST_SUITE_PEXPECT') ) or
+		os.getenv('MMGEN_FORCE_COLOR')
+	)
 
 	for k in ('linux','win','msys'):
 		if sys.platform[:len(k)] == k:

+ 5 - 1
mmgen/util.py

@@ -151,7 +151,11 @@ def set_for_type(val,refval,desc,invert_bool=False,src=None):
 
 	if type(refval) == bool:
 		v = str(val).lower()
-		ret = True if v in ('true','yes','1') else False if v in ('false','no','none','0') else None
+		ret = (
+			True  if v in ('true','yes','1','on') else
+			False if v in ('false','no','none','0','off','') else
+			None
+		)
 		if ret is not None:
 			return not ret if invert_bool else ret
 	else:

+ 4 - 0
test/include/common.py

@@ -27,6 +27,10 @@ import os
 from mmgen.common import *
 from mmgen.devtools import *
 
+def strip_ansi_escapes(s):
+	import re
+	return re.sub('\x1b\[[;0-9]+?m','',s)
+
 ascii_uc   = ''.join(map(chr,list(range(65,91))))   # 26 chars
 ascii_lc   = ''.join(map(chr,list(range(97,123))))  # 26 chars
 lat_accent = ''.join(map(chr,list(range(192,383)))) # 191 chars, L,S

+ 1 - 1
test/objtest.py

@@ -178,7 +178,7 @@ def do_loop():
 			msg(clr(f'{nl}Testing {test}'))
 
 		for k in ('bad','good'):
-			if not opt.silent:
+			if not opt.super_silent:
 				msg(purple(capfirst(k)+' input:'))
 			for arg in test_data[test][k]:
 				run_test(

+ 1 - 0
test/objtest_py_d/ot_btc_mainnet.py

@@ -169,6 +169,7 @@ tests = {
 		'arg1': 'proto',
 		'exc_name': 'BadTwLabel',
 		'bad':  (
+			{'text':'',              'proto':proto},
 			{'text':'x x',           'proto':proto},
 			{'text':'x я',           'proto':proto},
 			{'text':'я:я',           'proto':proto},

+ 10 - 4
test/test.py

@@ -460,8 +460,13 @@ def set_environ_for_spawned_scripts():
 		for name in g.env_opts:
 			if name[:11] == 'MMGEN_DEBUG':
 				os.environ[name] = '1'
-	if not opt.pexpect_spawn: os.environ['MMGEN_TEST_SUITE_POPEN_SPAWN'] = '1'
-	if not opt.system: os.environ['PYTHONPATH'] = repo_root
+
+	if not opt.pexpect_spawn:
+		os.environ['MMGEN_TEST_SUITE_POPEN_SPAWN'] = '1'
+
+	if not opt.system:
+		os.environ['PYTHONPATH'] = repo_root
+
 	if not opt.buf_keypress:
 		os.environ['MMGEN_DISABLE_HOLD_PROTECT'] = '1'
 
@@ -469,11 +474,10 @@ def set_environ_for_spawned_scripts():
 	if os.getenv('MMGEN_TRACEBACK') and not opt.traceback:
 		os.environ['MMGEN_TRACEBACK'] = ''
 
-	# Disable color in spawned scripts so pexpect can parse their output
-	os.environ['MMGEN_DISABLE_COLOR'] = '1'
 	os.environ['MMGEN_NO_LICENSE'] = '1'
 	os.environ['MMGEN_MIN_URANDCHARS'] = '3'
 	os.environ['MMGEN_BOGUS_SEND'] = '1'
+	os.environ['MMGEN_TEST_SUITE_PEXPECT'] = '1'
 
 def set_restore_term_at_exit():
 	import termios,atexit
@@ -694,6 +698,8 @@ class TestSuiteRunner(object):
 				self.ts.test_name,
 				cmd_disp))
 
+		os.environ['MMGEN_FORCE_COLOR'] = ('1' if self.ts.color else '')
+
 		from test.include.pexpect import MMGenPexpect
 		return MMGenPexpect(args,no_output=no_output)
 

+ 1 - 0
test/test_py_d/ts_base.py

@@ -33,6 +33,7 @@ class TestSuiteBase(object):
 	extra_spawn_args = []
 	networks = ()
 	segwit_opts_ok = False
+	color = False
 
 	def __init__(self,trunner,cfgs,spawn):
 		from mmgen.protocol import init_proto_from_opts

+ 6 - 4
test/test_py_d/ts_input.py

@@ -19,6 +19,7 @@ class TestSuiteInput(TestSuiteBase):
 	'user input'
 	networks = ('btc',)
 	tmpdir_nums = []
+	color = True
 	cmd_group = (
 		('get_passphrase_ui',             (1,"hash preset, password and label (wallet.py)", [])),
 		('get_passphrase_cmdline',        (1,"hash preset, password and label (wallet.py - from cmdline)", [])),
@@ -185,7 +186,7 @@ class TestSuiteInput(TestSuiteBase):
 		mne = mn_entry(fmt,entry_mode)
 		t.expect('Entry mode: ',str(mne.entry_modes.index(entry_mode)+1))
 		t.expect('Using (.+) entry mode',regex=True)
-		mode = t.p.match.group(1).lower()
+		mode = strip_ansi_escapes(t.p.match.group(1)).lower()
 		assert mode == mne.em.name.lower(), '{} != {}'.format(mode,mne.em.name.lower())
 		stealth_mnemonic_entry(t,mne,mn,entry_mode=entry_mode,pad_entry=pad_entry)
 		t.expect(sample_mn[fmt]['hex'])
@@ -202,7 +203,7 @@ class TestSuiteInput(TestSuiteBase):
 			for idx,val in ((5,'x'),(18,'0'),(30,'7'),(44,'9')):
 				mn.insert(idx,val)
 		t = self.spawn('mmgen-walletconv',['-r10','-S','-i',fmt,'-o',out_fmt or fmt])
-		t.expect('{} type: {}'.format(capfirst(wcls.wclass),wcls.mn_type))
+		t.expect('{} type:.*{}'.format(capfirst(wcls.wclass),wcls.mn_type),regex=True)
 		t.expect(wcls.choose_seedlen_prompt,'1')
 		t.expect('(Y/n): ','y')
 		if wcls.wclass == 'mnemonic':
@@ -212,7 +213,7 @@ class TestSuiteInput(TestSuiteBase):
 			mne = mn_entry(fmt,entry_mode)
 			t.expect('Entry mode: ',str(mne.entry_modes.index(entry_mode)+1))
 			t.expect('Using (.+) entry mode',regex=True)
-			mode = t.p.match.group(1).lower()
+			mode = strip_ansi_escapes(t.p.match.group(1)).lower()
 			assert mode == mne.em.name.lower(), '{} != {}'.format(mode,mne.em.name.lower())
 			stealth_mnemonic_entry(t,mne,mn,entry_mode=entry_mode)
 		elif wcls.wclass == 'dieroll':
@@ -224,7 +225,8 @@ class TestSuiteInput(TestSuiteBase):
 				t.expect(wcls.user_entropy_prompt,'n')
 		if not usr_rand:
 			sid_chk = 'FE3C6545'
-			sid = t.expect_getend('Valid {} for Seed ID '.format(wcls.desc))[:8]
+			sid = t.expect_getend('Valid {} for Seed ID '.format(wcls.desc))
+			sid = strip_ansi_escapes(sid.split(',')[0])
 			assert sid == sid_chk,'Seed ID mismatch! {} != {}'.format(sid,sid_chk)
 		t.expect('to confirm: ','YES\n')
 		t.read()

+ 4 - 2
test/test_py_d/ts_main.py

@@ -42,6 +42,7 @@ def make_brainwallet_file(fn):
 	write_data_to_file(fn,d,'brainwallet password',quiet=True,ignore_opt_outdir=True)
 
 def verify_checksum_or_exit(checksum,chk):
+	chk = strip_ansi_escapes(chk)
 	if checksum != chk:
 		raise TestSuiteFatalException('Checksum error: {}'.format(chk))
 	vmsg(green('Checksums match: ') + cyan(chk))
@@ -66,6 +67,7 @@ class TestSuiteMain(TestSuiteBase,TestSuiteShared):
 	networks = ('btc','btc_tn','ltc','ltc_tn','bch','bch_tn')
 	passthru_opts = ('daemon_data_dir','rpc_port','coin','testnet')
 	segwit_opts_ok = True
+	color = True
 	cmd_group = (
 		('walletgen_dfl_wallet', (15,'wallet generation (default wallet)',[[[],15]])),
 		('subwalletgen_dfl_wallet', (15,'subwallet generation (default wallet)',[[[pwfile],15]])),
@@ -221,7 +223,7 @@ class TestSuiteMain(TestSuiteBase,TestSuiteShared):
 		t.license()
 		wcls = MMGenWallet
 		t.passphrase(wcls.desc,self.cfgs['1']['wpasswd'])
-		t.expect('Generating subseed 10S')
+		t.expect('Generating subseed.*10S',regex=True)
 		t.passphrase_new('new '+wcls.desc,'foo')
 		t.usr_rand(self.usr_rand_chars)
 		fn = t.written_to_file(capfirst(wcls.desc))
@@ -236,7 +238,7 @@ class TestSuiteMain(TestSuiteBase,TestSuiteShared):
 		t = self.spawn('mmgen-subwalletgen', args)
 		t.license()
 		t.passphrase(icls.desc,self.cfgs['1']['wpasswd'])
-		t.expect('Generating subseed 3L')
+		t.expect('Generating subseed.*3L',regex=True)
 		fn = t.written_to_file(capfirst(ocls.desc))
 		ext = get_extension(fn)
 		assert ext == ocls.ext,'incorrect file extension: {}'.format(ext)