|
| 1 | +"""Examples for working with custom environment in tmux tests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import time |
| 7 | + |
| 8 | + |
| 9 | +def test_environment_variables(session) -> None: |
| 10 | + """Test setting and using environment variables.""" |
| 11 | + # Create a window for testing |
| 12 | + window = session.new_window(window_name="env-test") |
| 13 | + pane = window.active_pane |
| 14 | + |
| 15 | + # Set environment variables for the pane |
| 16 | + test_env = { |
| 17 | + "TEST_VAR1": "value1", |
| 18 | + "TEST_VAR2": "value2", |
| 19 | + "TEST_PATH": "/custom/path:/another/path", |
| 20 | + } |
| 21 | + |
| 22 | + # Clear the pane first |
| 23 | + pane.send_keys("clear", enter=True) |
| 24 | + time.sleep(0.3) |
| 25 | + |
| 26 | + # Set environment variables |
| 27 | + for key, value in test_env.items(): |
| 28 | + pane.send_keys(f"export {key}='{value}'", enter=True) |
| 29 | + |
| 30 | + time.sleep(0.5) |
| 31 | + |
| 32 | + # Verify environment variables were set |
| 33 | + pane.send_keys("echo $TEST_VAR1", enter=True) |
| 34 | + time.sleep(0.5) |
| 35 | + output = pane.capture_pane() |
| 36 | + assert any("value1" in line for line in output) |
| 37 | + |
| 38 | + # Test with a script that uses the variables |
| 39 | + pane.send_keys('echo "Combined: $TEST_VAR1 and $TEST_VAR2"', enter=True) |
| 40 | + time.sleep(0.5) |
| 41 | + output = pane.capture_pane() |
| 42 | + assert any("Combined: value1 and value2" in line for line in output) |
| 43 | + |
| 44 | + |
| 45 | +def test_directory_navigation(session, tmp_path) -> None: |
| 46 | + """Test navigating directories in tmux.""" |
| 47 | + # Create a window for testing |
| 48 | + window = session.new_window(window_name="dir-test") |
| 49 | + pane = window.active_pane |
| 50 | + |
| 51 | + # Clear the pane |
| 52 | + pane.send_keys("clear", enter=True) |
| 53 | + time.sleep(0.3) |
| 54 | + |
| 55 | + # Get and save the initial directory to tmp_path |
| 56 | + initial_dir_file = tmp_path / "initial_dir.txt" |
| 57 | + pane.send_keys(f"pwd > {initial_dir_file}", enter=True) |
| 58 | + time.sleep(0.5) |
| 59 | + |
| 60 | + # Navigate to /tmp directory |
| 61 | + pane.send_keys("cd /tmp", enter=True) |
| 62 | + time.sleep(0.5) |
| 63 | + |
| 64 | + # Verify we changed directory |
| 65 | + pane.send_keys("pwd", enter=True) |
| 66 | + time.sleep(0.5) |
| 67 | + output = pane.capture_pane() |
| 68 | + assert any("/tmp" in line for line in output) |
| 69 | + |
| 70 | + # Create a test directory and navigate to it |
| 71 | + test_dir = f"tmux_test_{os.getpid()}" |
| 72 | + pane.send_keys(f"mkdir -p {test_dir}", enter=True) |
| 73 | + pane.send_keys(f"cd {test_dir}", enter=True) |
| 74 | + time.sleep(0.5) |
| 75 | + |
| 76 | + # Verify we're in the test directory |
| 77 | + pane.send_keys("pwd", enter=True) |
| 78 | + time.sleep(0.5) |
| 79 | + output = pane.capture_pane() |
| 80 | + assert any(f"/tmp/{test_dir}" in line for line in output) |
| 81 | + |
| 82 | + # Clean up |
| 83 | + pane.send_keys("cd ..", enter=True) |
| 84 | + pane.send_keys(f"rm -r {test_dir}", enter=True) |
| 85 | + |
| 86 | + |
| 87 | +def test_custom_session(TestServer) -> None: |
| 88 | + """Test creating a session with custom parameters.""" |
| 89 | + # Create a new server instance |
| 90 | + server = TestServer() |
| 91 | + |
| 92 | + # Create a session with custom parameters |
| 93 | + session_params = { |
| 94 | + "session_name": "custom-session", |
| 95 | + "x": 800, |
| 96 | + "y": 600, |
| 97 | + "start_directory": "/tmp", |
| 98 | + } |
| 99 | + |
| 100 | + session = server.new_session(**session_params) |
| 101 | + |
| 102 | + # Verify session was created with the right name |
| 103 | + assert session.session_name == "custom-session" |
| 104 | + |
| 105 | + # Verify working directory was set correctly |
| 106 | + window = session.active_window |
| 107 | + pane = window.active_pane |
| 108 | + |
| 109 | + pane.send_keys("pwd", enter=True) |
| 110 | + time.sleep(0.5) |
| 111 | + output = pane.capture_pane() |
| 112 | + assert any("/tmp" in line for line in output) |
0 commit comments