Skip to content

Commit

Permalink
Have tests respect PG_CONFIG (#453)
Browse files Browse the repository at this point in the history
When running tests locally I often switch between different PG
installations (@ different versions). In order to do so, I rely on the
`PG_CONFIG` environment variable.

This PR makes it so that tests are first reading PG's `bindir` from
`pg_config` (using explicit path provided with `PG_CONFIG` variable if
it exists.
  • Loading branch information
Y-- authored Dec 2, 2024
1 parent c3e80d8 commit 3cb6110
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions test/pycheck/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,17 @@ def get_pg_major_version():
return int(major_version_string.group(0))


def get_bin_dir():
pg_config_bin = (
os.environ["PG_CONFIG"] if "PG_CONFIG" in os.environ else "pg_config"
)
return capture([pg_config_bin, "--bindir"], silent=True).strip()


PG_MAJOR_VERSION = get_pg_major_version()

PG_BIN_DIR = get_bin_dir()

# this is out of ephemeral port range for many systems hence
# it is a lower change that it will conflict with "in-use" ports
PORT_LOWER_BOUND = 10200
Expand All @@ -131,6 +140,10 @@ def get_pg_major_version():
next_port = PORT_LOWER_BOUND


def pg_bin(b):
return os.path.join(PG_BIN_DIR, b)


class NoResultClass:
def __eq__(self, other):
return self is other
Expand Down Expand Up @@ -393,7 +406,15 @@ def psql(self, query, **kwargs):
self.set_default_connection_options(kwargs)
connect_options = " ".join([f"{k}={v}" for k, v in kwargs.items()])

run(["psql", f"port={self.port} {connect_options}", "-c", query], shell=False)
run(
[
pg_bin("psql"),
f"port={self.port} {connect_options}",
"-c",
query,
],
shell=False,
)

@contextmanager
def transaction(self, **kwargs):
Expand Down Expand Up @@ -541,13 +562,14 @@ def debug(self):
def psql_debug(self, **kwargs):
conninfo = self.make_conninfo(**kwargs)
run(
["psql", conninfo],
[pg_bin("psql"), conninfo],
silent=True,
)

def initdb(self):
initdb = pg_bin("initdb")
run(
f"initdb -A trust --nosync --username postgres --pgdata {self.pgdata}",
f"'{initdb}' -A trust --nosync --username postgres --pgdata {self.pgdata}",
stdout=subprocess.DEVNULL,
)

Expand Down Expand Up @@ -595,11 +617,17 @@ def initdb(self):
pgconf.write("duckdb.force_execution = 'true'\n")

def pgctl(self, command, **kwargs):
run(f"pg_ctl -w --pgdata {self.pgdata} {command}", **kwargs)
pg_ctl = pg_bin("pg_ctl")
run(
f"'{pg_ctl}' -w --pgdata {self.pgdata} {command}",
**kwargs,
)

def apgctl(self, command, **kwargs):
pg_ctl = pg_bin("pg_ctl")
return asyncio.create_subprocess_shell(
f"pg_ctl -w --pgdata {self.pgdata} {command}", **kwargs
f"'{pg_ctl}' -w --pgdata {self.pgdata} {command}",
**kwargs,
)

def start(self):
Expand Down

0 comments on commit 3cb6110

Please sign in to comment.