Skip to content

Commit

Permalink
Drop invasive control sequences from pexpect debug output
Browse files Browse the repository at this point in the history
A failing test might emit an OSC 133 prompt marking sequence, confusing
the parent terminal to think the test output contains a shell prompt. Let's
remove these.
  • Loading branch information
krobelus committed Apr 9, 2024
1 parent adb4014 commit 64bc989
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion build_tools/pexpect_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@

UNEXPECTED_SUCCESS = object()

# When rendering fish's output, remove the control sequences that modify terminal state,
# to avoid confusing the calling terminal. No need to replace things like colors and cursor
# movement that are harmless and/or will not leak anyway.
SANITIZE_FOR_PRINTING_RE = re.compile(
r"""
\x1b\[\?1004[hl]
| \x1b\[\?2004[hl]
| \x1b\[>4;[10]m
| \x1b\[>5u
| \x1b\[<1u
| \x1b=
| \x1b>
| \x1b\].*?\x07
""",
re.VERBOSE)


def get_prompt_re(counter):
"""Return a regular expression for matching a with a given prompt counter."""
Expand Down Expand Up @@ -287,7 +303,7 @@ def report_exception_and_exit(self, pat, unmatched, err):
print("")
print("{CYAN}When written to the tty, this looks like:{RESET}".format(**colors))
print("{CYAN}<-------{RESET}".format(**colors))
sys.stdout.write(self.spawn.before)
sys.stdout.write(SANITIZE_FOR_PRINTING_RE.sub('', self.spawn.before))
sys.stdout.flush()
maybe_nl=""
if not self.spawn.before.endswith("\n"):
Expand Down

0 comments on commit 64bc989

Please sign in to comment.