|
1 | | -"""Examples for advanced command polling in tmux tests.""" |
| 1 | +"""Examples for command polling in tmux tests.""" |
| 2 | + |
2 | 3 | from __future__ import annotations |
3 | 4 |
|
4 | | -import re |
5 | 5 | import time |
6 | 6 |
|
7 | 7 |
|
8 | | -def wait_for_output(pane, expected_pattern, timeout=5, interval=0.1): |
9 | | - """ |
10 | | - Wait for a specific pattern to appear in the pane output. |
11 | | -
|
12 | | - Args: |
13 | | - pane: The tmux pane to check |
14 | | - expected_pattern: Regex pattern to look for |
15 | | - timeout: Maximum time to wait in seconds |
16 | | - interval: Time between checks in seconds |
17 | | -
|
18 | | - Returns |
19 | | - ------- |
20 | | - The matching line if found, None if timeout occurs |
21 | | - """ |
22 | | - pattern = re.compile(expected_pattern) |
23 | | - start_time = time.time() |
24 | | - |
25 | | - while time.time() - start_time < timeout: |
26 | | - output = pane.capture_pane() |
27 | | - |
28 | | - for line in output: |
29 | | - if pattern.search(line): |
30 | | - return line |
31 | | - |
32 | | - time.sleep(interval) |
33 | | - |
34 | | - return None # Timeout occurred |
35 | | - |
36 | | - |
37 | 8 | def test_command_with_polling(session): |
38 | | - """Test robust command polling.""" |
| 9 | + """Test running a command and polling for completion.""" |
| 10 | + # Create a window for testing |
39 | 11 | window = session.new_window(window_name="polling-test") |
40 | 12 | pane = window.active_pane |
41 | | - |
42 | | - # Start a command that takes time |
43 | | - pane.send_keys("echo 'Starting'; sleep 2; echo 'Finished!'", enter=True) |
44 | | - |
45 | | - # Wait for command to complete |
46 | | - result = wait_for_output(pane, r"Finished!") |
47 | | - |
48 | | - # Verify command completed successfully |
49 | | - assert result is not None, "Command did not finish within timeout" |
50 | | - |
51 | | - # Run a sequence of commands |
52 | | - pane.send_keys("echo 'Processing step 1'", enter=True) |
53 | | - assert wait_for_output(pane, r"Processing step 1") |
54 | | - |
55 | | - pane.send_keys("echo 'Processing step 2'", enter=True) |
56 | | - assert wait_for_output(pane, r"Processing step 2") |
57 | | - |
58 | | - pane.send_keys("echo 'Processing step 3'", enter=True) |
59 | | - assert wait_for_output(pane, r"Processing step 3") |
60 | | - |
61 | | - # Verify order of operations |
62 | | - output = pane.capture_pane() |
63 | | - step1_line = next( |
64 | | - (i for i, line in enumerate(output) if "Processing step 1" in line), -1 |
65 | | - ) |
66 | | - step2_line = next( |
67 | | - (i for i, line in enumerate(output) if "Processing step 2" in line), -1 |
68 | | - ) |
69 | | - step3_line = next( |
70 | | - (i for i, line in enumerate(output) if "Processing step 3" in line), -1 |
71 | | - ) |
72 | | - |
73 | | - assert step1_line < step2_line < step3_line, ( |
74 | | - "Commands did not execute in the correct order" |
75 | | - ) |
76 | | - |
77 | | - |
78 | | -def test_error_handling(pane): |
79 | | - """Test handling of command errors.""" |
80 | | - # Send a command that will fail |
81 | | - pane.send_keys("command_that_does_not_exist", enter=True) |
82 | | - |
83 | | - # Wait for the error to appear |
84 | | - result = wait_for_output(pane, r"command not found", timeout=2) |
85 | | - |
86 | | - # Verify error message appears |
87 | | - assert result is not None, "Error message not found" |
88 | | - |
89 | | - # Clear screen |
| 13 | + |
| 14 | + # Clear the pane |
90 | 15 | pane.send_keys("clear", enter=True) |
91 | | - time.sleep(0.5) |
92 | | - |
93 | | - # Try to recover with a valid command |
94 | | - pane.send_keys("echo 'Recovery successful'", enter=True) |
95 | | - result = wait_for_output(pane, r"Recovery successful") |
96 | | - |
97 | | - assert result is not None, "Recovery command failed" |
| 16 | + time.sleep(0.3) |
| 17 | + |
| 18 | + # Run a command that takes some time (using sleep) |
| 19 | + pane.send_keys("echo 'Starting task'; sleep 2; echo 'Task complete'", enter=True) |
| 20 | + |
| 21 | + # Poll for completion by checking for the completion message |
| 22 | + max_polls = 10 |
| 23 | + poll_interval = 0.5 |
| 24 | + completion_found = False |
| 25 | + |
| 26 | + for _ in range(max_polls): |
| 27 | + output = pane.capture_pane() |
| 28 | + if any("Task complete" in line for line in output): |
| 29 | + completion_found = True |
| 30 | + break |
| 31 | + time.sleep(poll_interval) |
| 32 | + |
| 33 | + # Verify the task completed |
| 34 | + assert completion_found, "Task did not complete within the expected time" |
| 35 | + |
| 36 | + # Additional verification that both messages are in the output |
| 37 | + final_output = pane.capture_pane() |
| 38 | + assert any("Starting task" in line for line in final_output) |
| 39 | + assert any("Task complete" in line for line in final_output) |
| 40 | + |
| 41 | + |
| 42 | +def test_error_handling(session): |
| 43 | + """Test error handling during command execution.""" |
| 44 | + # Create a window for testing |
| 45 | + window = session.new_window(window_name="error-test") |
| 46 | + pane = window.active_pane |
| 47 | + |
| 48 | + # Clear the pane |
| 49 | + pane.send_keys("clear", enter=True) |
| 50 | + time.sleep(0.3) |
| 51 | + |
| 52 | + # Run a command that will produce an error |
| 53 | + pane.send_keys("echo 'Running command with error'; ls /nonexistent_directory; echo 'Command finished'", enter=True) |
| 54 | + time.sleep(1) # Wait for command to complete |
| 55 | + |
| 56 | + # Capture the output |
| 57 | + output = pane.capture_pane() |
| 58 | + |
| 59 | + # Verify error message and completion message |
| 60 | + assert any("Running command with error" in line for line in output), "Start message not found" |
| 61 | + assert any("Command finished" in line for line in output), "Completion message not found" |
| 62 | + |
| 63 | + # Verify error message is in the output |
| 64 | + has_error = any("No such file or directory" in line for line in output) or any("cannot access" in line for line in output) |
| 65 | + assert has_error, "Error message not found in output" |
0 commit comments