From 9681a5a9795b35325044278aba1d8219f9757ae0 Mon Sep 17 00:00:00 2001 From: bhelgs <33836927+bhelgs@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:52:08 -0400 Subject: [PATCH] add support for \ character in pytest temporary path - Closes #982 --- newsfragments/982.feature.rst | 1 + pytest_postgresql/executor.py | 8 ++++---- pytest_postgresql/factories/process.py | 8 +++++++- tests/test_postgresql.py | 3 +-- 4 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 newsfragments/982.feature.rst diff --git a/newsfragments/982.feature.rst b/newsfragments/982.feature.rst new file mode 100644 index 00000000..68346c3f --- /dev/null +++ b/newsfragments/982.feature.rst @@ -0,0 +1 @@ +add support for \\ character in pytest temporary path \ No newline at end of file diff --git a/pytest_postgresql/executor.py b/pytest_postgresql/executor.py index 25797492..ddcfbbed 100644 --- a/pytest_postgresql/executor.py +++ b/pytest_postgresql/executor.py @@ -49,11 +49,11 @@ class PostgreSQLExecutor(TCPExecutor): """ BASE_PROC_START_COMMAND = ( - "{executable} start -D {datadir} " + '{executable} start -D "{datadir}" ' "-o \"-F -p {port} -c log_destination='stderr' " "-c logging_collector=off " "-c unix_socket_directories='{unixsocketdir}' {postgres_options}\" " - "-l {logfile} {startparams}" + '-l "{logfile}" {startparams}' ) VERSION_RE = re.compile(r".* (?P\d+(?:\.\d+)?)") @@ -214,13 +214,13 @@ def running(self) -> bool: """Check if server is running.""" if not os.path.exists(self.datadir): return False - status_code = subprocess.getstatusoutput(f"{self.executable} status -D {self.datadir}")[0] + status_code = subprocess.getstatusoutput(f'{self.executable} status -D "{self.datadir}"')[0] return status_code == 0 def stop(self: T, sig: Optional[int] = None, exp_sig: Optional[int] = None) -> T: """Issue a stop request to executable.""" subprocess.check_output( - f"{self.executable} stop -D {self.datadir} -m f", + f'{self.executable} stop -D "{self.datadir}" -m f', shell=True, ) try: diff --git a/pytest_postgresql/factories/process.py b/pytest_postgresql/factories/process.py index 1c8ffc5c..d8a1d223 100644 --- a/pytest_postgresql/factories/process.py +++ b/pytest_postgresql/factories/process.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with pytest-postgresql. If not, see . """Fixture factory for postgresql process.""" + import os.path import platform import subprocess @@ -32,6 +33,11 @@ from pytest_postgresql.janitor import DatabaseJanitor +def _tmp_dir(tmp_path_factory: TempPathFactory, subdir: str) -> Path: + """Mock out for testing a specific temporary path.""" + return tmp_path_factory.mktemp(subdir) + + def postgresql_proc( executable: Optional[str] = None, host: Optional[str] = None, @@ -107,7 +113,7 @@ def postgresql_proc_fixture( ) from ex postgresql_ctl = os.path.join(pg_bindir, "pg_ctl") - tmpdir = tmp_path_factory.mktemp(f"pytest-postgresql-{request.fixturename}") + tmpdir = _tmp_dir(tmp_path_factory, f"pytest-postgresql-{request.fixturename}") pg_port = get_port(port) or get_port(config["port"]) assert pg_port is not None diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 3e53cf13..ee929835 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -22,9 +22,8 @@ def test_postgresql_proc(postgresql_proc: PostgreSQLExecutor) -> None: assert postgresql_proc.running() is True -@pytest.mark.xfail(reason="issue tracked by #982") def test_postgresql_proc__bad_tmp(postgresql_proc: PostgreSQLExecutor) -> None: - """Test different postgresql versions.""" + r"""Test a \ character in the pytest temporary path.""" def bad_path(tmp_path_factory: TempPathFactory, subdir: str) -> Path: return tmp_path_factory.mktemp(rf"bad\path/{subdir}")