From d4a37011ce7948516687246f4b2cbffc36955049 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 29 Oct 2022 20:10:24 +0000 Subject: [PATCH] test.include.pexpect: improve handling of send_delay, hold protect --- mmgen/globalvars.py | 2 ++ mmgen/term.py | 8 ++++---- test/include/pexpect.py | 6 +++--- test/test.py | 11 +++++++---- test/test_py_d/ts_cfg.py | 3 +-- test/test_py_d/ts_regtest.py | 7 ++----- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/mmgen/globalvars.py b/mmgen/globalvars.py index ab1b4738..5e82c8db 100755 --- a/mmgen/globalvars.py +++ b/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', diff --git a/mmgen/term.py b/mmgen/term.py index 507f887f..facf50cc 100755 --- a/mmgen/term.py +++ b/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 diff --git a/test/include/pexpect.py b/test/include/pexpect.py index af82044f..e040f9d4 100755 --- a/test/include/pexpect.py +++ b/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 diff --git a/test/test.py b/test/test.py index c2e50e18..9b7c3f73 100755 --- a/test/test.py +++ b/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) diff --git a/test/test_py_d/ts_cfg.py b/test/test_py_d/ts_cfg.py index a0417313..53a9c6dc 100755 --- a/test/test_py_d/ts_cfg.py +++ b/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') diff --git a/test/test_py_d/ts_regtest.py b/test/test_py_d/ts_regtest.py index c3360b7c..f9792cea 100755 --- a/test/test_py_d/ts_regtest.py +++ b/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