|
| 1 | +"""Test Windows compatibility fixes for pytest-postgresql.""" |
| 2 | + |
| 3 | +import subprocess |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +from pytest_postgresql.executor import PostgreSQLExecutor |
| 7 | + |
| 8 | + |
| 9 | +class TestWindowsCompatibility: |
| 10 | + """Test Windows-specific functionality.""" |
| 11 | + |
| 12 | + def test_get_base_command_unified(self): |
| 13 | + """Test that base command is unified across platforms.""" |
| 14 | + executor = PostgreSQLExecutor( |
| 15 | + executable="/path/to/pg_ctl", |
| 16 | + host="localhost", |
| 17 | + port=5432, |
| 18 | + datadir="/tmp/data", |
| 19 | + unixsocketdir="/tmp/socket", |
| 20 | + logfile="/tmp/log", |
| 21 | + startparams="-w", |
| 22 | + dbname="test", |
| 23 | + ) |
| 24 | + |
| 25 | + # Test that command format is consistent across platforms |
| 26 | + with patch("platform.system", return_value="Windows"): |
| 27 | + windows_command = executor._get_base_command() |
| 28 | + |
| 29 | + with patch("platform.system", return_value="Linux"): |
| 30 | + unix_command = executor._get_base_command() |
| 31 | + |
| 32 | + # Both should be the same now |
| 33 | + assert windows_command == unix_command |
| 34 | + |
| 35 | + # Both should use the simplified format without single quotes |
| 36 | + assert "log_destination=stderr" in windows_command |
| 37 | + assert "log_destination='stderr'" not in windows_command |
| 38 | + assert "unix_socket_directories={unixsocketdir}" in windows_command |
| 39 | + assert "unix_socket_directories='{unixsocketdir}'" not in windows_command |
| 40 | + |
| 41 | + def test_windows_terminate_process(self): |
| 42 | + """Test Windows process termination.""" |
| 43 | + executor = PostgreSQLExecutor( |
| 44 | + executable="/path/to/pg_ctl", |
| 45 | + host="localhost", |
| 46 | + port=5432, |
| 47 | + datadir="/tmp/data", |
| 48 | + unixsocketdir="/tmp/socket", |
| 49 | + logfile="/tmp/log", |
| 50 | + startparams="-w", |
| 51 | + dbname="test", |
| 52 | + ) |
| 53 | + |
| 54 | + # Mock process |
| 55 | + mock_process = MagicMock() |
| 56 | + executor.process = mock_process |
| 57 | + |
| 58 | + # No need to mock platform.system() since the method doesn't check it anymore |
| 59 | + executor._windows_terminate_process() |
| 60 | + |
| 61 | + # Should call terminate first |
| 62 | + mock_process.terminate.assert_called_once() |
| 63 | + mock_process.wait.assert_called() |
| 64 | + |
| 65 | + def test_windows_terminate_process_force_kill(self): |
| 66 | + """Test Windows process termination with force kill on timeout.""" |
| 67 | + executor = PostgreSQLExecutor( |
| 68 | + executable="/path/to/pg_ctl", |
| 69 | + host="localhost", |
| 70 | + port=5432, |
| 71 | + datadir="/tmp/data", |
| 72 | + unixsocketdir="/tmp/socket", |
| 73 | + logfile="/tmp/log", |
| 74 | + startparams="-w", |
| 75 | + dbname="test", |
| 76 | + ) |
| 77 | + |
| 78 | + # Mock process that times out |
| 79 | + mock_process = MagicMock() |
| 80 | + mock_process.wait.side_effect = [subprocess.TimeoutExpired(cmd="test", timeout=5), None] |
| 81 | + executor.process = mock_process |
| 82 | + |
| 83 | + # No need to mock platform.system() since the method doesn't check it anymore |
| 84 | + executor._windows_terminate_process() |
| 85 | + |
| 86 | + # Should call terminate, wait (timeout), then kill, then wait again |
| 87 | + mock_process.terminate.assert_called_once() |
| 88 | + mock_process.kill.assert_called_once() |
| 89 | + assert mock_process.wait.call_count == 2 |
| 90 | + |
| 91 | + def test_stop_method_windows(self): |
| 92 | + """Test stop method on Windows.""" |
| 93 | + executor = PostgreSQLExecutor( |
| 94 | + executable="/path/to/pg_ctl", |
| 95 | + host="localhost", |
| 96 | + port=5432, |
| 97 | + datadir="/tmp/data", |
| 98 | + unixsocketdir="/tmp/socket", |
| 99 | + logfile="/tmp/log", |
| 100 | + startparams="-w", |
| 101 | + dbname="test", |
| 102 | + ) |
| 103 | + |
| 104 | + # Mock subprocess and process |
| 105 | + with ( |
| 106 | + patch("subprocess.check_output") as mock_subprocess, |
| 107 | + patch("platform.system", return_value="Windows"), |
| 108 | + patch.object(executor, "_windows_terminate_process") as mock_terminate, |
| 109 | + ): |
| 110 | + result = executor.stop() |
| 111 | + |
| 112 | + # Should call pg_ctl stop and Windows terminate |
| 113 | + mock_subprocess.assert_called_once() |
| 114 | + mock_terminate.assert_called_once() |
| 115 | + assert result is executor |
| 116 | + |
| 117 | + def test_stop_method_unix(self): |
| 118 | + """Test stop method on Unix systems.""" |
| 119 | + executor = PostgreSQLExecutor( |
| 120 | + executable="/path/to/pg_ctl", |
| 121 | + host="localhost", |
| 122 | + port=5432, |
| 123 | + datadir="/tmp/data", |
| 124 | + unixsocketdir="/tmp/socket", |
| 125 | + logfile="/tmp/log", |
| 126 | + startparams="-w", |
| 127 | + dbname="test", |
| 128 | + ) |
| 129 | + |
| 130 | + # Mock subprocess and super().stop |
| 131 | + with ( |
| 132 | + patch("subprocess.check_output") as mock_subprocess, |
| 133 | + patch("platform.system", return_value="Linux"), |
| 134 | + patch("pytest_postgresql.executor.TCPExecutor.stop") as mock_super_stop, |
| 135 | + ): |
| 136 | + mock_super_stop.return_value = executor |
| 137 | + result = executor.stop() |
| 138 | + |
| 139 | + # Should call pg_ctl stop and parent class stop |
| 140 | + mock_subprocess.assert_called_once() |
| 141 | + mock_super_stop.assert_called_once_with(None, None) |
| 142 | + assert result is executor |
| 143 | + |
| 144 | + def test_stop_method_fallback_on_killpg_error(self): |
| 145 | + """Test stop method falls back to Windows termination on killpg AttributeError.""" |
| 146 | + executor = PostgreSQLExecutor( |
| 147 | + executable="/path/to/pg_ctl", |
| 148 | + host="localhost", |
| 149 | + port=5432, |
| 150 | + datadir="/tmp/data", |
| 151 | + unixsocketdir="/tmp/socket", |
| 152 | + logfile="/tmp/log", |
| 153 | + startparams="-w", |
| 154 | + dbname="test", |
| 155 | + ) |
| 156 | + |
| 157 | + # Mock subprocess and super().stop to raise AttributeError |
| 158 | + with ( |
| 159 | + patch("subprocess.check_output") as mock_subprocess, |
| 160 | + patch("platform.system", return_value="Linux"), |
| 161 | + patch( |
| 162 | + "pytest_postgresql.executor.TCPExecutor.stop", |
| 163 | + side_effect=AttributeError("module 'os' has no attribute 'killpg'"), |
| 164 | + ), |
| 165 | + patch.object(executor, "_windows_terminate_process") as mock_terminate, |
| 166 | + ): |
| 167 | + result = executor.stop() |
| 168 | + |
| 169 | + # Should call pg_ctl stop, fail on super().stop, then use Windows terminate |
| 170 | + mock_subprocess.assert_called_once() |
| 171 | + mock_terminate.assert_called_once() |
| 172 | + assert result is executor |
| 173 | + |
| 174 | + def test_command_formatting_windows(self): |
| 175 | + """Test that command is properly formatted for Windows.""" |
| 176 | + with patch("platform.system", return_value="Windows"): |
| 177 | + executor = PostgreSQLExecutor( |
| 178 | + executable="C:/Program Files/PostgreSQL/bin/pg_ctl.exe", |
| 179 | + host="localhost", |
| 180 | + port=5555, |
| 181 | + datadir="C:/temp/data", |
| 182 | + unixsocketdir="C:/temp/socket", |
| 183 | + logfile="C:/temp/log.txt", |
| 184 | + startparams="-w -s", |
| 185 | + dbname="testdb", |
| 186 | + postgres_options="-c shared_preload_libraries=test", |
| 187 | + ) |
| 188 | + |
| 189 | + # The command should be properly formatted without quotes around stderr |
| 190 | + expected_parts = [ |
| 191 | + "C:/Program Files/PostgreSQL/bin/pg_ctl.exe start", |
| 192 | + '-D "C:/temp/data"', |
| 193 | + '-o "-F -p 5555 -c log_destination=stderr', |
| 194 | + "-c logging_collector=off", |
| 195 | + "-c unix_socket_directories=C:/temp/socket", |
| 196 | + '-c shared_preload_libraries=test"', |
| 197 | + '-l "C:/temp/log.txt"', |
| 198 | + "-w -s", |
| 199 | + ] |
| 200 | + |
| 201 | + # Check if all expected parts are in the command |
| 202 | + command = executor.command |
| 203 | + for part in expected_parts: |
| 204 | + assert part in command, f"Expected '{part}' in command: {command}" |
0 commit comments