From 9e1800cb965dc39683ef6b5fb2dc3ef3a3b6829c Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 6 Oct 2020 14:22:35 -0700 Subject: [PATCH] Rework increment param in pexpect.expect_prompt This switches the 'increment' param from "after" to "before." Instead of expect_prompt saying if the next prompt will be incremented, each call site says if it should have been incremented sinec the last prompt. --- build_tools/pexpect_helper.py | 10 ++++++---- tests/pexpects/bind.py | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/build_tools/pexpect_helper.py b/build_tools/pexpect_helper.py index f94943fdb..17afc405d 100644 --- a/build_tools/pexpect_helper.py +++ b/build_tools/pexpect_helper.py @@ -145,7 +145,7 @@ class SpawnedProc(object): self.start_time = None self.spawn = pexpect.spawn(exe_path, env=env, encoding="utf-8", timeout=timeout) self.spawn.delaybeforesend = None - self.prompt_counter = 1 + self.prompt_counter = 0 def time_since_first_message(self): """ Return a delta in seconds since the first message, or 0 if this is the first. """ @@ -203,7 +203,9 @@ class SpawnedProc(object): def expect_prompt(self, increment=True, *args, **kwargs): """ Convenience function which matches some text and then a prompt. Match the given positional arguments as expect_re, and then look - for a prompt, bumping the prompt counter if increment is true. + for a prompt. + If increment is set, then this should be a new prompt and the prompt counter + should be bumped; otherwise this is not a new prompt. Returns None on success, and exits on failure. Example: sp.sendline("echo hello world") @@ -211,12 +213,12 @@ class SpawnedProc(object): """ if args: self.expect_re(*args, **kwargs) + if increment: + self.prompt_counter += 1 self.expect_re( get_prompt_re(self.prompt_counter), pat_desc="prompt %d" % self.prompt_counter, ) - if increment: - self.prompt_counter += 1 def report_exception_and_exit(self, pat, unmatched, err): """ Things have gone badly. diff --git a/tests/pexpects/bind.py b/tests/pexpects/bind.py index 660581807..1cd16de0c 100644 --- a/tests/pexpects/bind.py +++ b/tests/pexpects/bind.py @@ -3,7 +3,7 @@ from pexpect_helper import SpawnedProc sp = SpawnedProc() send, sendline, sleep, expect_prompt = sp.send, sp.sendline, sp.sleep, sp.expect_prompt -expect_prompt(increment=False) +expect_prompt() # Clear twice (regression test for #7280). send("\f") @@ -280,12 +280,12 @@ expect_prompt("qqq", unmatched="Leading qs not stripped") # Test bigword with single-character words. sendline("bind \cg kill-bigword") expect_prompt() -send("a b c d\x01") # ctrl-a, move back to the beginning of the line -send("\x07") # ctrl-g, kill bigword +send("a b c d\x01") # ctrl-a, move back to the beginning of the line +send("\x07") # ctrl-g, kill bigword sendline("echo") expect_prompt("^b c d") -send(" a b c d\x01") # ctrl-a, move back to the beginning of the line -send("\x07") # ctrl-g, kill bigword +send(" a b c d\x01") # ctrl-a, move back to the beginning of the line +send("\x07") # ctrl-g, kill bigword sendline("echo") expect_prompt("^b c d")