Browse Source

test.include.pexpect: improve handling of send_delay, hold protect

The MMGen Project 2 years ago
parent
commit
d4a37011ce
6 changed files with 19 additions and 18 deletions
  1. 2 0
      mmgen/globalvars.py
  2. 4 4
      mmgen/term.py
  3. 3 3
      test/include/pexpect.py
  4. 7 4
      test/test.py
  5. 1 2
      test/test_py_d/ts_cfg.py
  6. 2 5
      test/test_py_d/ts_regtest.py

+ 2 - 0
mmgen/globalvars.py

@@ -116,6 +116,7 @@ class GlobalContext(Lockable):
 	test_suite           = False
 	test_suite_deterministic = False
 	test_suite_popen_spawn = False
+	hold_protect_disable = False
 
 	mnemonic_entry_modes = {}
 
@@ -249,6 +250,7 @@ class GlobalContext(Lockable):
 		'MMGEN_DEBUG_TW',
 		'MMGEN_DEBUG_UTF8',
 		'MMGEN_DEBUG_SUBSEED',
+		'MMGEN_HOLD_PROTECT_DISABLE',
 		'MMGEN_QUIET',
 		'MMGEN_FORCE_256_COLOR',
 		'MMGEN_MIN_URANDCHARS',

+ 4 - 4
mmgen/term.py

@@ -84,7 +84,7 @@ class MMGenTermLinux(MMGenTerm):
 
 	@classmethod
 	def kb_hold_protect(cls):
-		if g.test_suite:
+		if g.hold_protect_disable:
 			return
 		tty.setcbreak(cls.stdin_fd)
 		timeout = 0.3
@@ -106,7 +106,7 @@ class MMGenTermLinux(MMGenTerm):
 		timeout = 0.3
 		tty.setcbreak(cls.stdin_fd)
 		msg_r(prompt)
-		if g.test_suite:
+		if g.hold_protect_disable:
 			prehold_protect = False
 		while True:
 			# Protect against held-down key before read()
@@ -142,9 +142,9 @@ class MMGenTermLinuxStub(MMGenTermLinux):
 		pass
 
 	@classmethod
-	def get_char(cls,prompt='',immed_chars='',prehold_protect=None,num_bytes=None):
+	def get_char(cls,prompt='',immed_chars='',prehold_protect=None,num_bytes=5):
 		msg_r(prompt)
-		return sys.stdin.read(1)
+		return os.read(0,num_bytes).decode()
 
 	get_char_raw = get_char
 

+ 3 - 3
test/include/pexpect.py

@@ -40,9 +40,10 @@ NL = '\n'
 
 class MMGenPexpect:
 
-	def __init__(self,args,no_output=False,env=None,pexpect_spawn=False):
+	def __init__(self,args,no_output=False,env=None,pexpect_spawn=False,send_delay=None):
 
 		self.pexpect_spawn = pexpect_spawn
+		self.send_delay = send_delay
 		self.req_exit_val = 0
 		self.skip_ok = False
 		self.sent_value = None
@@ -182,7 +183,6 @@ class MMGenPexpect:
 		return m
 
 	def expect(self,s,t='',delay=None,regex=False,nonl=False,silent=False):
-		delay = delay or (0,0.3)[bool(opt.buf_keypress)]
 
 		if not silent:
 			if opt.verbose:
@@ -217,7 +217,7 @@ class MMGenPexpect:
 			return ret
 
 	def send(self,t,delay=None,s=False):
-		delay = delay or (0,0.3)[bool(opt.buf_keypress)]
+		delay = delay or self.send_delay
 		if delay:
 			time.sleep(delay)
 		ret = self.p.send(t) # returns num bytes written

+ 7 - 4
test/test.py

@@ -337,9 +337,6 @@ def set_environ_for_spawned_scripts():
 	if not opt.system:
 		os.environ['PYTHONPATH'] = repo_root
 
-	if not opt.buf_keypress:
-		os.environ['MMGEN_DISABLE_HOLD_PROTECT'] = '1'
-
 	os.environ['MMGEN_NO_LICENSE'] = '1'
 	os.environ['MMGEN_MIN_URANDCHARS'] = '3'
 	os.environ['MMGEN_BOGUS_SEND'] = '1'
@@ -610,8 +607,13 @@ class TestSuiteRunner(object):
 				self.ts.test_name,
 				cmd_disp))
 
+		# NB: the `pexpect_spawn` arg enables hold_protect and send_delay while the corresponding cmdline
+		# option does not.  For performance reasons, this is the desired behavior.  For full emulation of
+		# the user experience with hold protect enabled, specify --buf-keypress or --demo.
+		send_delay = 0.4 if pexpect_spawn is True or opt.buf_keypress else None
 		pexpect_spawn = pexpect_spawn if pexpect_spawn is not None else bool(opt.pexpect_spawn)
 
+		os.environ['MMGEN_HOLD_PROTECT_DISABLE'] = '' if send_delay else '1'
 		os.environ['MMGEN_TEST_SUITE_POPEN_SPAWN'] = '' if pexpect_spawn else '1'
 		os.environ['MMGEN_FORCE_COLOR'] = '1' if self.ts.color else ''
 
@@ -626,7 +628,8 @@ class TestSuiteRunner(object):
 			args          = args,
 			no_output     = no_output,
 			env           = env,
-			pexpect_spawn = pexpect_spawn )
+			pexpect_spawn = pexpect_spawn,
+			send_delay    = send_delay )
 
 	def end_msg(self):
 		t = int(time.time() - self.start_time)

+ 1 - 2
test/test_py_d/ts_cfg.py

@@ -126,8 +126,7 @@ class TestSuiteCfg(TestSuiteBase):
 			t.expect(s)
 
 		if t.pexpect_spawn: # view and exit pager
-			if opt.exact_output:
-				time.sleep(1)
+			time.sleep(1 if opt.exact_output else t.send_delay)
 			t.send('q')
 
 		t.expect(cp,'n')

+ 2 - 5
test/test_py_d/ts_regtest.py

@@ -1258,11 +1258,8 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared):
 				s,
 				regex=True )
 			if t.pexpect_spawn and s == 'w':
-				t.expect(r'Total.*',regex=True)
-				if opt.exact_output:
-					time.sleep(1)
-				t.send('q')
-				time.sleep(0.2)
+				t.expect(r'Total.*','q',regex=True,delay=1 if opt.exact_output else t.send_delay)
+				time.sleep(t.send_delay)
 				t.send('e')
 		return t