Skip to content

Commit

Permalink
tests: Robustify bluetoothctl parsing
Browse files Browse the repository at this point in the history
The latest version introduced a lot more ANSI escape sequences. Remove
all of them, and more robustly.
  • Loading branch information
martinpitt committed Nov 6, 2024
1 parent 1a8d872 commit d5e449b
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions tests/test_bluez5.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


import os
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -59,21 +60,14 @@ def _run_bluetoothctl(command):
name="org.freedesktop.DBus.Mock.Error",
)

# Strip the prompt from the start of every line, then remove empty
# lines.
# Strip the prompt and escape sequences from the start of every line,
# then remove empty lines.
#
# The prompt looks like `[bluetooth]# `, potentially containing command
# line colour control codes. Split at the first space.
#
# Sometimes we end up with the final line being `\x1b[K` (partial
# control code), which we need to ignore.
# line colour control codes.
def remove_prefix(line):
if line.startswith(("[bluetooth]#", "\x1b")):
parts = line.split(" ", 1)
try:
return parts[1].strip()
except IndexError:
return ""
line = re.sub(r"\x1b\[[0-9;]*[mPK]", "", line)
line = re.sub(r"^\[bluetooth\]# ", "", line)
return line.strip()

lines = out.split("\n")
Expand Down

0 comments on commit d5e449b

Please sign in to comment.