Files
slic/tests/test_utils_termtitle.py
T
tligui_y 08ab3d57ec
Run CI Tests / test (push) Successful in 1m17s
Update tests/test_utils_termtitle.py
2025-07-30 13:24:11 +02:00

77 lines
2.6 KiB
Python

import pytest
import sys
import os
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"
)
def test_terminal_title_via_xterm():
# Create a unique title based on the current timestamp
expected_title = f"TITLE_TEST_{int(time.time())}"
# Launch an xterm window with the specified title
proc = subprocess.Popen(
["xterm", "-T", expected_title, "-e", "sleep", "5"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
# 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
# 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}'"