diff --git a/tests/test_utils_termtitle.py b/tests/test_utils_termtitle.py index fdafb270e..7162f7acf 100644 --- a/tests/test_utils_termtitle.py +++ b/tests/test_utils_termtitle.py @@ -10,44 +10,67 @@ 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())}" - # Lancer xterm avec un titre spécifique + # Launch an xterm window with the specified title proc = subprocess.Popen( ["xterm", "-T", expected_title, "-e", "sleep", "5"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) - # Attendre que la fenêtre apparaisse - time.sleep(1) + # 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 - # Récupérer le WINDOWID avec xdotool - try: - window_id = subprocess.check_output( - ["xdotool", "search", "--name", expected_title], - text=True - ).splitlines()[0].strip() - except subprocess.CalledProcessError: + # 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") + pytest.fail("Could not find xterm window with expected title after retrying") - # Lire le titre de la fenêtre avec xprop + # 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 on found window") + pytest.fail("xprop failed to read window properties") - proc.terminate() # Fermer le xterm + # Terminate the xterm process now that we're done + proc.terminate() - # Extraire le titre réel + # 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 found in xprop output") + 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}'"