-
Notifications
You must be signed in to change notification settings - Fork 54
Cleaner error message when the server is not installed - closes #1031 #1396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ``pg_ctl`` discovery no longer hands back a path that does not exist. A client-only PostgreSQL | ||
| install (Debian/Ubuntu's ``libpq-dev`` without the matching server package) makes | ||
| ``pg_config --bindir`` point at a directory holding no ``pg_ctl``; that path used to be returned | ||
| unchecked, and the failure only surfaced later as ``Could not found ...`` when the executor read the | ||
| server version. The path is now verified up front, and the ``ExecutableMissingException`` names the | ||
| locations that were checked along with the ways to fix it: install the server package, point at a | ||
| ``pg_ctl`` with ``--postgresql-exec``, or use ``postgresql_noproc`` against a server you run yourself. | ||
|
|
||
| A broken ``pg_config`` is reported the same way. Only ``FileNotFoundError`` used to be handled, so a | ||
| non-executable or failing ``pg_config`` escaped as a raw ``PermissionError``/``CalledProcessError``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Corrected the README's description of how ``pg_ctl`` is located — it listed the discovery steps in | ||
| the wrong order — and spelled out that ``postgresql_proc`` needs a local PostgreSQL server, pointing | ||
| at ``postgresql_noproc`` for dockerised or otherwise externally managed servers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Internal module for functions handling pg_ctl discovery.""" | ||
|
|
||
| import logging | ||
| import os | ||
| import platform | ||
| import subprocess | ||
| from typing import Iterable | ||
|
|
||
| from pytest_postgresql.config import PostgreSQLConfig | ||
| from pytest_postgresql.exceptions import ExecutableMissingException | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| PG_CTL_NAMES = ("pg_ctl.exe", "pg_ctl") if platform.system() == "Windows" else ("pg_ctl",) | ||
| """Names to look for when probing the filesystem for pg_ctl. | ||
|
|
||
| os.path.isfile matches the literal name, so finding the binary on Windows means asking | ||
| for pg_ctl.exe. Launching it is more forgiving: Windows appends .exe to an extensionless | ||
| program name, which is why the old code could run a path it never checked. | ||
| """ | ||
|
|
||
| PG_CONFIG_TIMEOUT = 60 | ||
| """Seconds to wait for pg_config, so a hung binary cannot stall fixture setup. | ||
|
|
||
| Matches PostgreSQLExecutor's default subprocess timeout. | ||
| """ | ||
|
|
||
|
|
||
| def _pg_bindir(checked: Iterable[str]) -> str: | ||
| """Ask pg_config where PostgreSQL keeps its binaries. | ||
|
|
||
| pg_config comes from the client development package, which on most distributions | ||
| can be installed without the server, so its answer is a hint rather than an answer. | ||
|
|
||
| :param checked: locations probed so far, to name in the error if pg_config can't be run | ||
| :raises ExecutableMissingException: pg_config is missing, unusable, hanging or failing. | ||
| TimeoutExpired is a SubprocessError, so it needs no branch of its own. | ||
| """ | ||
| try: | ||
| return subprocess.check_output( | ||
| ["pg_config", "--bindir"], universal_newlines=True, timeout=PG_CONFIG_TIMEOUT | ||
| ).strip() | ||
| except (OSError, subprocess.SubprocessError) as ex: | ||
| logger.debug("Could not read the binaries directory from pg_config: %s", ex) | ||
| raise ExecutableMissingException.pg_config_unusable(f"{type(ex).__name__}: {ex}", checked) from ex | ||
|
|
||
|
|
||
| def _is_executable(path: str) -> bool: | ||
| """Whether path is a file this user can actually run. | ||
|
|
||
| isfile is needed alongside the access check because X_OK on a directory only means | ||
| it can be traversed. On Windows os.access ignores X_OK, so this is an existence test. | ||
| """ | ||
| return os.path.isfile(path) and os.access(path, os.X_OK) | ||
|
|
||
|
|
||
| def _pg_exe(executable: str | None, config: PostgreSQLConfig) -> str: | ||
| """If executable is set, use it. Otherwise best effort to find the executable.""" | ||
| # an explicitly passed executable is taken at face value, it's not ours to second-guess | ||
| if executable is not None: | ||
| return executable | ||
| postgresql_ctl = config.exec | ||
| # check if that executable exists, as it's not on systems' PATH | ||
| if _is_executable(postgresql_ctl): | ||
| return postgresql_ctl | ||
| checked = [postgresql_ctl] | ||
| bindir = _pg_bindir(checked) | ||
| for name in PG_CTL_NAMES: | ||
| candidate = os.path.join(bindir, name) | ||
| if candidate in checked: | ||
| continue | ||
| checked.append(candidate) | ||
| if _is_executable(candidate): | ||
| return candidate | ||
| raise ExecutableMissingException.not_in_bindir(bindir, checked) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.