Skip to content

Commit

Permalink
Better exception messages when we could not find proper executable - c…
Browse files Browse the repository at this point in the history
…loses #598
  • Loading branch information
fizyk committed May 19, 2023
1 parent 9d314a2 commit cad1ed4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions newsfragments/598.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add your info here
9 changes: 9 additions & 0 deletions pytest_postgresql/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""pytest-postgresql's exceptions."""


class ExecutableMissingException(FileNotFoundError):
"""Exception risen when PgConfig was not found."""


class PostgreSQLUnsupported(Exception):
"""Exception raised when unsupported postgresql would be detected."""
14 changes: 9 additions & 5 deletions pytest_postgresql/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from mirakuru.exceptions import ProcessFinishedWithError
from pkg_resources import parse_version

from pytest_postgresql.exceptions import ExecutableMissingException, PostgreSQLUnsupported

_LOCALE = "C.UTF-8"

if platform.system() == "Darwin":
Expand All @@ -39,10 +41,6 @@
T = TypeVar("T", bound="PostgreSQLExecutor")


class PostgreSQLUnsupported(Exception):
"""Exception raised when unsupported postgresql would be detected."""


class PostgreSQLExecutor(TCPExecutor):
"""PostgreSQL executor running on pg_ctl.
Expand Down Expand Up @@ -195,7 +193,13 @@ def wait_for_postgres(self) -> None:
@property
def version(self) -> Any:
"""Detect postgresql version."""
version_string = subprocess.check_output([self.executable, "--version"]).decode("utf-8")
try:
version_string = subprocess.check_output([self.executable, "--version"]).decode("utf-8")
except FileNotFoundError as ex:
raise ExecutableMissingException(
f"Could not found {self.executable}. Is PostgreSQL server installed? "
f"Alternatively pg_config installed might be from different version that postgresql-server."
) from ex
matches = self.VERSION_RE.search(version_string)
assert matches is not None
return parse_version(matches.groupdict()["version"])
Expand Down
12 changes: 9 additions & 3 deletions pytest_postgresql/factories/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from pytest import FixtureRequest, TempPathFactory

from pytest_postgresql.config import get_config
from pytest_postgresql.exceptions import ExecutableMissingException
from pytest_postgresql.executor import PostgreSQLExecutor
from pytest_postgresql.janitor import DatabaseJanitor

Expand Down Expand Up @@ -95,9 +96,14 @@ def postgresql_proc_fixture(
# check if that executable exists, as it's no on system PATH
# only replace if executable isn't passed manually
if not os.path.exists(postgresql_ctl) and executable is None:
pg_bindir = subprocess.check_output(
["pg_config", "--bindir"], universal_newlines=True
).strip()
try:
pg_bindir = subprocess.check_output(
["pg_config", "--bindir"], universal_newlines=True
).strip()
except FileNotFoundError as ex:
raise ExecutableMissingException(
"Could not found pg_config executable. Is it in systenm $PATH?"
) from ex
postgresql_ctl = os.path.join(pg_bindir, "pg_ctl")

tmpdir = tmp_path_factory.mktemp(f"pytest-postgresql-{request.fixturename}")
Expand Down
3 changes: 2 additions & 1 deletion tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from pytest import FixtureRequest

from pytest_postgresql.config import get_config
from pytest_postgresql.executor import PostgreSQLExecutor, PostgreSQLUnsupported
from pytest_postgresql.exceptions import PostgreSQLUnsupported
from pytest_postgresql.executor import PostgreSQLExecutor
from pytest_postgresql.factories import postgresql, postgresql_proc
from pytest_postgresql.retry import retry

Expand Down

0 comments on commit cad1ed4

Please sign in to comment.