Update tests/test_utils_termtitle.py
Run CI Tests / test (push) Successful in 41s

This commit is contained in:
2025-07-30 13:28:06 +02:00
parent d899fe12aa
commit 11cf4183aa
+10 -62
View File
@@ -5,72 +5,20 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
from slic.utils.termtitle import *
import subprocess
import time
import subprocess
import time
import pytest
import subprocess
import time
import pytest
import shutil
import os
@pytest.mark.skipif(
shutil.which("xterm") is None or
shutil.which("xdotool") is None or
shutil.which("xprop") is None or
os.environ.get("DISPLAY") is None,
reason="Requires xterm, xdotool, xprop, and DISPLAY to be set"
shutil.which("tmux") is None or "TMUX" not in os.environ,
reason="tmux is not installed or not running inside a tmux session"
)
def test_terminal_title_via_xterm():
# Create a unique title based on the current timestamp
expected_title = f"TITLE_TEST_{int(time.time())}"
def test_terminal_title_with_tmux():
expected_title = "TMUX_CI_TEST"
# Launch an xterm window with the specified title
proc = subprocess.Popen(
["xterm", "-T", expected_title, "-e", "sleep", "5"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
subprocess.run(["tmux", "rename-window", expected_title], check=True)
# Try to detect the xterm window repeatedly (race condition possible)
# Retry for up to 2 seconds : 20 tries in total
window_id = None
for _ in range(20):
try:
# Use xdotool to find the window ID based on the window name
output = subprocess.check_output(
["xdotool", "search", "--name", expected_title],
text=True
)
window_id = output.splitlines()[0].strip()
break # Found the window, exit the loop
except subprocess.CalledProcessError:
time.sleep(0.1) # Wait and retry if not found yet
actual_title = subprocess.check_output(
["tmux", "display-message", "-p", "#W"],
text=True
).strip()
# If no window was found, kill the xterm process and fail the test
if not window_id:
proc.kill()
pytest.fail("Could not find xterm window with expected title after retrying")
# Use xprop to read the properties of the xterm window (especially WM_NAME)
try:
xprop_output = subprocess.check_output(["xprop", "-id", window_id], text=True)
except subprocess.CalledProcessError:
proc.kill()
pytest.fail("xprop failed to read window properties")
# Terminate the xterm process now that we're done
proc.terminate()
# Parse the xprop output to extract the actual window title
for line in xprop_output.splitlines():
if "WM_NAME(STRING)" in line or "WM_NAME(UTF8_STRING)" in line:
actual_title = line.split("=", 1)[1].strip().strip('"')
break
else:
pytest.fail("No WM_NAME property found in xprop output")
# Assert that the window's actual title matches what we expected
assert expected_title == actual_title, f"Expected '{expected_title}', got '{actual_title}'"
assert actual_title == expected_title, f"Expected '{expected_title}', got '{actual_title}'"