Skip to content

Commit 6ebc7eb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent e20e5b5 commit 6ebc7eb

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

pytest_postgresql/executor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
# along with pytest-postgresql. If not, see <http://www.gnu.org/licenses/>.
1818
"""PostgreSQL executor crafter around pg_ctl."""
1919

20+
import os
2021
import os.path
2122
import platform
2223
import re
2324
import shutil
25+
import signal
2426
import subprocess
2527
import tempfile
2628
import time
27-
import os
28-
import signal
2929
from typing import Any, Optional, TypeVar
3030

3131
from mirakuru import TCPExecutor
@@ -56,9 +56,9 @@ def _get_base_command(self) -> str:
5656
# Windows doesn't handle single quotes well in subprocess calls
5757
return (
5858
'{executable} start -D "{datadir}" '
59-
"-o \"-F -p {port} -c log_destination=stderr "
59+
'-o "-F -p {port} -c log_destination=stderr '
6060
"-c logging_collector=off "
61-
"-c unix_socket_directories={unixsocketdir} {postgres_options}\" "
61+
'-c unix_socket_directories={unixsocketdir} {postgres_options}" '
6262
'-l "{logfile}" {startparams}'
6363
)
6464
else:
@@ -240,7 +240,7 @@ def _windows_terminate_process(self, sig: Optional[int] = None) -> None:
240240
"""Terminate process on Windows."""
241241
if self.process is None:
242242
return
243-
243+
244244
try:
245245
if platform.system() == "Windows":
246246
# On Windows, try to terminate gracefully first

tests/test_windows_compatibility.py

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
"""Test Windows compatibility fixes for pytest-postgresql."""
22

3-
import platform
4-
import pytest
5-
from unittest.mock import Mock, patch, MagicMock
63
import subprocess
7-
import signal
4+
from unittest.mock import MagicMock, patch
85

96
from pytest_postgresql.executor import PostgreSQLExecutor
107

@@ -24,8 +21,8 @@ def test_get_base_command_windows(self):
2421
startparams="-w",
2522
dbname="test",
2623
)
27-
28-
with patch('platform.system', return_value='Windows'):
24+
25+
with patch("platform.system", return_value="Windows"):
2926
command = executor._get_base_command()
3027
# Windows command should not have single quotes around stderr
3128
assert "log_destination=stderr" in command
@@ -45,8 +42,8 @@ def test_get_base_command_unix(self):
4542
startparams="-w",
4643
dbname="test",
4744
)
48-
49-
with patch('platform.system', return_value='Linux'):
45+
46+
with patch("platform.system", return_value="Linux"):
5047
command = executor._get_base_command()
5148
# Unix command should have single quotes around stderr
5249
assert "log_destination='stderr'" in command
@@ -64,14 +61,14 @@ def test_windows_terminate_process(self):
6461
startparams="-w",
6562
dbname="test",
6663
)
67-
64+
6865
# Mock process
6966
mock_process = MagicMock()
7067
executor.process = mock_process
71-
72-
with patch('platform.system', return_value='Windows'):
68+
69+
with patch("platform.system", return_value="Windows"):
7370
executor._windows_terminate_process()
74-
71+
7572
# Should call terminate first
7673
mock_process.terminate.assert_called_once()
7774
mock_process.wait.assert_called()
@@ -88,15 +85,15 @@ def test_windows_terminate_process_force_kill(self):
8885
startparams="-w",
8986
dbname="test",
9087
)
91-
88+
9289
# Mock process that times out
9390
mock_process = MagicMock()
9491
mock_process.wait.side_effect = [subprocess.TimeoutExpired(cmd="test", timeout=5), None]
9592
executor.process = mock_process
96-
97-
with patch('platform.system', return_value='Windows'):
93+
94+
with patch("platform.system", return_value="Windows"):
9895
executor._windows_terminate_process()
99-
96+
10097
# Should call terminate, wait (timeout), then kill, then wait again
10198
mock_process.terminate.assert_called_once()
10299
mock_process.kill.assert_called_once()
@@ -114,14 +111,16 @@ def test_stop_method_windows(self):
114111
startparams="-w",
115112
dbname="test",
116113
)
117-
114+
118115
# Mock subprocess and process
119-
with patch('subprocess.check_output') as mock_subprocess, \
120-
patch('platform.system', return_value='Windows'), \
121-
patch.object(executor, '_windows_terminate_process') as mock_terminate:
122-
116+
with (
117+
patch("subprocess.check_output") as mock_subprocess,
118+
patch("platform.system", return_value="Windows"),
119+
patch.object(executor, "_windows_terminate_process") as mock_terminate,
120+
):
121+
123122
result = executor.stop()
124-
123+
125124
# Should call pg_ctl stop and Windows terminate
126125
mock_subprocess.assert_called_once()
127126
mock_terminate.assert_called_once_with(None)
@@ -139,15 +138,17 @@ def test_stop_method_unix(self):
139138
startparams="-w",
140139
dbname="test",
141140
)
142-
141+
143142
# Mock subprocess and super().stop
144-
with patch('subprocess.check_output') as mock_subprocess, \
145-
patch('platform.system', return_value='Linux'), \
146-
patch('pytest_postgresql.executor.TCPExecutor.stop') as mock_super_stop:
147-
143+
with (
144+
patch("subprocess.check_output") as mock_subprocess,
145+
patch("platform.system", return_value="Linux"),
146+
patch("pytest_postgresql.executor.TCPExecutor.stop") as mock_super_stop,
147+
):
148+
148149
mock_super_stop.return_value = executor
149150
result = executor.stop()
150-
151+
151152
# Should call pg_ctl stop and parent class stop
152153
mock_subprocess.assert_called_once()
153154
mock_super_stop.assert_called_once_with(None, None)
@@ -165,24 +166,28 @@ def test_stop_method_fallback_on_killpg_error(self):
165166
startparams="-w",
166167
dbname="test",
167168
)
168-
169+
169170
# Mock subprocess and super().stop to raise AttributeError
170-
with patch('subprocess.check_output') as mock_subprocess, \
171-
patch('platform.system', return_value='Linux'), \
172-
patch('pytest_postgresql.executor.TCPExecutor.stop',
173-
side_effect=AttributeError("module 'os' has no attribute 'killpg'")), \
174-
patch.object(executor, '_windows_terminate_process') as mock_terminate:
175-
171+
with (
172+
patch("subprocess.check_output") as mock_subprocess,
173+
patch("platform.system", return_value="Linux"),
174+
patch(
175+
"pytest_postgresql.executor.TCPExecutor.stop",
176+
side_effect=AttributeError("module 'os' has no attribute 'killpg'"),
177+
),
178+
patch.object(executor, "_windows_terminate_process") as mock_terminate,
179+
):
180+
176181
result = executor.stop()
177-
182+
178183
# Should call pg_ctl stop, fail on super().stop, then use Windows terminate
179184
mock_subprocess.assert_called_once()
180185
mock_terminate.assert_called_once_with(None)
181186
assert result is executor
182187

183188
def test_command_formatting_windows(self):
184189
"""Test that command is properly formatted for Windows."""
185-
with patch('platform.system', return_value='Windows'):
190+
with patch("platform.system", return_value="Windows"):
186191
executor = PostgreSQLExecutor(
187192
executable="C:/Program Files/PostgreSQL/bin/pg_ctl.exe",
188193
host="localhost",
@@ -192,21 +197,21 @@ def test_command_formatting_windows(self):
192197
logfile="C:/temp/log.txt",
193198
startparams="-w -s",
194199
dbname="testdb",
195-
postgres_options="-c shared_preload_libraries=test"
200+
postgres_options="-c shared_preload_libraries=test",
196201
)
197-
202+
198203
# The command should be properly formatted without quotes around stderr
199204
expected_parts = [
200205
"C:/Program Files/PostgreSQL/bin/pg_ctl.exe start",
201206
'-D "C:/temp/data"',
202-
"-o \"-F -p 5555 -c log_destination=stderr",
207+
'-o "-F -p 5555 -c log_destination=stderr',
203208
"-c logging_collector=off",
204209
"-c unix_socket_directories=C:/temp/socket",
205-
"-c shared_preload_libraries=test\"",
210+
'-c shared_preload_libraries=test"',
206211
'-l "C:/temp/log.txt"',
207-
"-w -s"
212+
"-w -s",
208213
]
209-
214+
210215
# Check if all expected parts are in the command
211216
command = executor.command
212217
for part in expected_parts:

0 commit comments

Comments
 (0)