Skip to content

Commit 4fc1725

Browse files
committed
refactor: convert postgres check harness from runCommand to writeShellApplication
Migrate test harness to use writeShellApplication for better shell script handling, fix shellcheck warnings.
1 parent efd4387 commit 4fc1725

File tree

1 file changed

+58
-46
lines changed

1 file changed

+58
-46
lines changed

nix/checks.nix

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,32 @@
153153
) filteredSqlTests;
154154
sortedTestList = builtins.sort (a: b: a < b) testList;
155155
in
156-
pkgs.runCommand "postgres-${pgpkg.version}-check-harness"
157-
{
158-
nativeBuildInputs = with pkgs; [
159-
coreutils
160-
bash
161-
perl
162-
pgpkg
163-
pg_prove
164-
pg_regress
165-
procps
166-
start-postgres-server-bin
167-
which
168-
getkey-script
169-
supabase-groonga
170-
python3
171-
netcat
172-
];
173-
}
174-
''
156+
pkgs.writeShellApplication rec {
157+
name = "postgres-${pgpkg.version}-check-harness";
158+
bashOptions = [ "nounset" "pipefail" ];
159+
runtimeInputs = with pkgs; [
160+
coreutils
161+
bash
162+
perl
163+
pgpkg
164+
pg_prove
165+
pg_regress
166+
procps
167+
start-postgres-server-bin
168+
which
169+
getkey-script
170+
supabase-groonga
171+
python3
172+
netcat
173+
];
174+
175+
text = ''
176+
177+
#shellcheck disable=SC1091
175178
source ${bashlog}
176-
set -uo pipefail
179+
#shellcheck disable=SC1091
180+
source ${pkgs.stdenv}/setup
181+
export PATH="${lib.makeBinPath runtimeInputs}:$PATH"
177182
178183
export BASHLOG_FILE=1
179184
export BASHLOG_FILE_PATH=debug.log
@@ -184,10 +189,11 @@
184189
function log_cmd {
185190
local cmd_name="$1"
186191
shift
187-
log debug "Executing: $cmd_name $@"
192+
log debug "Executing: $cmd_name $*"
188193
local exit_code=0
189-
echo "\$ $cmd_name $@" >> debug.log
190-
"$cmd_name" "$@" 2>&1 >> debug.log || exit_code=$?
194+
echo "\$ $cmd_name $*" >> debug.log
195+
196+
"$cmd_name" "$@" >> debug.log 2>&1 || exit_code=$?
191197
log debug "Exit code: $exit_code"
192198
return $exit_code
193199
}
@@ -207,12 +213,12 @@
207213
208214
function check_postgres_ready {
209215
for i in {1..60}; do
210-
if log_cmd pg_isready -h ${self.supabase.defaults.host} -p ${pgPort} -U supabase_admin -q; then
216+
if log_cmd pg_isready -h localhost -p ${pgPort} -U supabase_admin -q; then
211217
log info "PostgreSQL is ready"
212218
break
213219
fi
214220
sleep 1
215-
if [ $i -eq 60 ]; then
221+
if [ "$i" -eq 60 ]; then
216222
log error "PostgreSQL failed to start"
217223
exit 1
218224
fi
@@ -253,13 +259,14 @@
253259
log_cmd initdb --locale=C --username=supabase_admin -D "$PGTAP_CLUSTER"
254260
substitute ${./tests/postgresql.conf.in} "$PGTAP_CLUSTER"/postgresql.conf \
255261
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${getkey-script}/bin/pgsodium-getkey"
256-
echo "listen_addresses = '*'" >> "$PGTAP_CLUSTER"/postgresql.conf
262+
echo "listen_addresses = '127.0.0.1'" >> "$PGTAP_CLUSTER"/postgresql.conf
257263
echo "port = ${pgPort}" >> "$PGTAP_CLUSTER"/postgresql.conf
258-
echo "host all all 127.0.0.1/32 trust" >> $PGTAP_CLUSTER/pg_hba.conf
264+
echo "host all all 127.0.0.1/32 trust" >> "$PGTAP_CLUSTER/pg_hba.conf"
259265
log info "Checking shared_preload_libraries setting:"
260-
log info $(grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf)
266+
log info "$(grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf)"
261267
# Remove timescaledb if running orioledb-17 check
262268
log info "pgpkg.version is: ${pgpkg.version}"
269+
#shellcheck disable=SC2193
263270
if [[ "${pgpkg.version}" == *"17"* ]]; then
264271
perl -pi -e 's/ timescaledb,//g' "$PGTAP_CLUSTER/postgresql.conf"
265272
fi
@@ -273,10 +280,10 @@
273280
274281
# PostgreSQL startup
275282
if [[ "$(uname)" == "Darwin" ]]; then
276-
log_cmd pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k "$PGTAP_CLUSTER" -p ${pgPort} -d 5" start
283+
log_cmd pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER/postgresql.log" -o "-k $PGTAP_CLUSTER -p ${pgPort} -d 5" start
277284
else
278285
mkdir -p "$PGTAP_CLUSTER/sockets"
279-
log_cmd pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k $PGTAP_CLUSTER/sockets -p ${pgPort} -d 5" start
286+
log_cmd pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER/postgresql.log" -o "-k $PGTAP_CLUSTER/sockets -p ${pgPort} -d 5" start
280287
fi || {
281288
log error "pg_ctl failed to start PostgreSQL"
282289
log error "Contents of postgresql.log:"
@@ -288,10 +295,10 @@
288295
check_postgres_ready
289296
290297
log info "Creating test database"
291-
log_cmd createdb -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin testing
298+
log_cmd createdb -p ${pgPort} -h localhost --username=supabase_admin testing
292299
293300
log info "Loading prime SQL file"
294-
if ! log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql}; then
301+
if ! log_cmd psql -p ${pgPort} -h localhost --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql}; then
295302
log error "Error executing SQL file. PostgreSQL log content:"
296303
cat "$PGTAP_CLUSTER"/postgresql.log
297304
pg_ctl -D "$PGTAP_CLUSTER" stop
@@ -300,7 +307,7 @@
300307
301308
# Create a table to store test configuration
302309
log info "Creating test_config table"
303-
log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -c "
310+
log_cmd psql -p ${pgPort} -h localhost --username=supabase_admin -d testing -c "
304311
CREATE TABLE IF NOT EXISTS test_config (key TEXT PRIMARY KEY, value TEXT);
305312
INSERT INTO test_config (key, value) VALUES ('http_mock_port', '$HTTP_MOCK_PORT')
306313
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;
@@ -309,11 +316,11 @@
309316
for t in $(printf "%s\n" ${builtins.concatStringsSep " " sortedTestList}); do
310317
log info "Running pgtap test: $t.sql"
311318
#XXX enable ON_ERROR_STOP ?
312-
log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -f "${./tests/sql}/$t.sql"
319+
log_cmd psql -p ${pgPort} -h localhost --username=supabase_admin -d testing -f "${./tests/sql}/$t.sql"
313320
done
314321
rm -rf "$SORTED_DIR"
315322
log_cmd pg_ctl -D "$PGTAP_CLUSTER" stop
316-
rm -rf $PGTAP_CLUSTER
323+
rm -rf "$PGTAP_CLUSTER"
317324
318325
# End of pgtap tests
319326
# from here on out we are running pg_regress tests, we use a different cluster for this
@@ -322,50 +329,55 @@
322329
323330
log info "Starting PostgreSQL server for pg_regress tests"
324331
unset GRN_PLUGINS_DIR
325-
log_cmd ${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize
332+
if ! log_cmd ${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize; then
333+
log error "Failed to start PostgreSQL server for pg_regress tests"
334+
exit 1
335+
fi
326336
327337
check_postgres_ready
328338
329339
log info "Loading prime SQL file"
330-
if ! log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --no-password --username=supabase_admin -d postgres -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql} 2>&1; then
340+
if ! log_cmd psql -p ${pgPort} -h localhost --no-password --username=supabase_admin -d postgres -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql} 2>&1; then
331341
log error "Error executing SQL file"
332342
exit 1
333343
fi
334344
335345
# Create a table to store test configuration for pg_regress tests
336346
log info "Creating test_config table for pg_regress tests"
337-
log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --no-password --username=supabase_admin -d postgres -c "
347+
log_cmd psql -p ${pgPort} -h localhost --no-password --username=supabase_admin -d postgres -c "
338348
CREATE TABLE IF NOT EXISTS test_config (key TEXT PRIMARY KEY, value TEXT);
339349
INSERT INTO test_config (key, value) VALUES ('http_mock_port', '$HTTP_MOCK_PORT')
340350
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;
341351
"
342352
343-
mkdir -p $out/regression_output
353+
#shellcheck disable=SC2154
354+
mkdir -p "$out/regression_output"
344355
log info "Running pg_regress tests"
345356
if ! log_cmd pg_regress \
346357
--use-existing \
347358
--dbname=postgres \
348359
--inputdir=${./tests} \
349-
--outputdir=$out/regression_output \
350-
--host=${self.supabase.defaults.host} \
360+
--outputdir="$out/regression_output" \
361+
--host=localhost \
351362
--port=${pgPort} \
352363
--user=supabase_admin \
353364
${builtins.concatStringsSep " " sortedTestList} 2>&1; then
354365
log error "pg_regress tests failed"
355-
cat $out/regression_output/regression.diffs
366+
cat "$out/regression_output/regression.diffs"
356367
exit 1
357368
fi
358369
log info "pg_regress tests completed successfully"
359370
360371
log info "Running migrations tests"
361-
log_cmd pg_prove -p ${pgPort} -U supabase_admin -h ${self.supabase.defaults.host} -d postgres -v ${../migrations/tests}/test.sql
372+
log_cmd pg_prove -p ${pgPort} -U supabase_admin -h localhost -d postgres -v ${../migrations/tests}/test.sql
362373
log info "Migrations tests completed successfully"
363374
'';
375+
};
364376
in
365377
{
366-
psql_15 = makeCheckHarness self'.packages."psql_15/bin";
367-
psql_17 = makeCheckHarness self'.packages."psql_17/bin";
368-
psql_orioledb-17 = makeCheckHarness self'.packages."psql_orioledb-17/bin";
378+
psql_15 = pkgs.runCommand "run-check-harness-psql-15" {} (lib.getExe (makeCheckHarness self'.packages."psql_15/bin"));
379+
psql_17 = pkgs.runCommand "run-check-harness-psql-17" {} (lib.getExe (makeCheckHarness self'.packages."psql_17/bin"));
380+
psql_orioledb-17 = pkgs.runCommand "run-check-harness-psql-orioledb-17" {} (lib.getExe (makeCheckHarness self'.packages."psql_orioledb-17/bin"));
369381
inherit (self'.packages)
370382
wal-g-2
371383
wal-g-3

0 commit comments

Comments
 (0)