Skip to content

Commit a80b66c

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 a80b66c

File tree

1 file changed

+67
-46
lines changed

1 file changed

+67
-46
lines changed

nix/checks.nix

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,35 @@
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 = [
159+
"nounset"
160+
"pipefail"
161+
];
162+
runtimeInputs = with pkgs; [
163+
coreutils
164+
bash
165+
perl
166+
pgpkg
167+
pg_prove
168+
pg_regress
169+
procps
170+
start-postgres-server-bin
171+
which
172+
getkey-script
173+
supabase-groonga
174+
python3
175+
netcat
176+
];
177+
178+
text = ''
179+
180+
#shellcheck disable=SC1091
175181
source ${bashlog}
176-
set -uo pipefail
182+
#shellcheck disable=SC1091
183+
source ${pkgs.stdenv}/setup
184+
export PATH="${lib.makeBinPath runtimeInputs}:$PATH"
177185
178186
export BASHLOG_FILE=1
179187
export BASHLOG_FILE_PATH=debug.log
@@ -184,10 +192,11 @@
184192
function log_cmd {
185193
local cmd_name="$1"
186194
shift
187-
log debug "Executing: $cmd_name $@"
195+
log debug "Executing: $cmd_name $*"
188196
local exit_code=0
189-
echo "\$ $cmd_name $@" >> debug.log
190-
"$cmd_name" "$@" 2>&1 >> debug.log || exit_code=$?
197+
echo "\$ $cmd_name $*" >> debug.log
198+
199+
"$cmd_name" "$@" >> debug.log 2>&1 || exit_code=$?
191200
log debug "Exit code: $exit_code"
192201
return $exit_code
193202
}
@@ -207,12 +216,12 @@
207216
208217
function check_postgres_ready {
209218
for i in {1..60}; do
210-
if log_cmd pg_isready -h ${self.supabase.defaults.host} -p ${pgPort} -U supabase_admin -q; then
219+
if log_cmd pg_isready -h localhost -p ${pgPort} -U supabase_admin -q; then
211220
log info "PostgreSQL is ready"
212221
break
213222
fi
214223
sleep 1
215-
if [ $i -eq 60 ]; then
224+
if [ "$i" -eq 60 ]; then
216225
log error "PostgreSQL failed to start"
217226
exit 1
218227
fi
@@ -253,13 +262,14 @@
253262
log_cmd initdb --locale=C --username=supabase_admin -D "$PGTAP_CLUSTER"
254263
substitute ${./tests/postgresql.conf.in} "$PGTAP_CLUSTER"/postgresql.conf \
255264
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${getkey-script}/bin/pgsodium-getkey"
256-
echo "listen_addresses = '*'" >> "$PGTAP_CLUSTER"/postgresql.conf
265+
echo "listen_addresses = '127.0.0.1'" >> "$PGTAP_CLUSTER"/postgresql.conf
257266
echo "port = ${pgPort}" >> "$PGTAP_CLUSTER"/postgresql.conf
258-
echo "host all all 127.0.0.1/32 trust" >> $PGTAP_CLUSTER/pg_hba.conf
267+
echo "host all all 127.0.0.1/32 trust" >> "$PGTAP_CLUSTER/pg_hba.conf"
259268
log info "Checking shared_preload_libraries setting:"
260-
log info $(grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf)
269+
log info "$(grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf)"
261270
# Remove timescaledb if running orioledb-17 check
262271
log info "pgpkg.version is: ${pgpkg.version}"
272+
#shellcheck disable=SC2193
263273
if [[ "${pgpkg.version}" == *"17"* ]]; then
264274
perl -pi -e 's/ timescaledb,//g' "$PGTAP_CLUSTER/postgresql.conf"
265275
fi
@@ -273,10 +283,10 @@
273283
274284
# PostgreSQL startup
275285
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
286+
log_cmd pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER/postgresql.log" -o "-k $PGTAP_CLUSTER -p ${pgPort} -d 5" start
277287
else
278288
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
289+
log_cmd pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER/postgresql.log" -o "-k $PGTAP_CLUSTER/sockets -p ${pgPort} -d 5" start
280290
fi || {
281291
log error "pg_ctl failed to start PostgreSQL"
282292
log error "Contents of postgresql.log:"
@@ -288,10 +298,10 @@
288298
check_postgres_ready
289299
290300
log info "Creating test database"
291-
log_cmd createdb -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin testing
301+
log_cmd createdb -p ${pgPort} -h localhost --username=supabase_admin testing
292302
293303
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
304+
if ! log_cmd psql -p ${pgPort} -h localhost --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql}; then
295305
log error "Error executing SQL file. PostgreSQL log content:"
296306
cat "$PGTAP_CLUSTER"/postgresql.log
297307
pg_ctl -D "$PGTAP_CLUSTER" stop
@@ -300,7 +310,7 @@
300310
301311
# Create a table to store test configuration
302312
log info "Creating test_config table"
303-
log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -c "
313+
log_cmd psql -p ${pgPort} -h localhost --username=supabase_admin -d testing -c "
304314
CREATE TABLE IF NOT EXISTS test_config (key TEXT PRIMARY KEY, value TEXT);
305315
INSERT INTO test_config (key, value) VALUES ('http_mock_port', '$HTTP_MOCK_PORT')
306316
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;
@@ -309,11 +319,11 @@
309319
for t in $(printf "%s\n" ${builtins.concatStringsSep " " sortedTestList}); do
310320
log info "Running pgtap test: $t.sql"
311321
#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"
322+
log_cmd psql -p ${pgPort} -h localhost --username=supabase_admin -d testing -f "${./tests/sql}/$t.sql"
313323
done
314324
rm -rf "$SORTED_DIR"
315325
log_cmd pg_ctl -D "$PGTAP_CLUSTER" stop
316-
rm -rf $PGTAP_CLUSTER
326+
rm -rf "$PGTAP_CLUSTER"
317327
318328
# End of pgtap tests
319329
# from here on out we are running pg_regress tests, we use a different cluster for this
@@ -322,50 +332,61 @@
322332
323333
log info "Starting PostgreSQL server for pg_regress tests"
324334
unset GRN_PLUGINS_DIR
325-
log_cmd ${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize
335+
if ! log_cmd ${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize; then
336+
log error "Failed to start PostgreSQL server for pg_regress tests"
337+
exit 1
338+
fi
326339
327340
check_postgres_ready
328341
329342
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
343+
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
331344
log error "Error executing SQL file"
332345
exit 1
333346
fi
334347
335348
# Create a table to store test configuration for pg_regress tests
336349
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 "
350+
log_cmd psql -p ${pgPort} -h localhost --no-password --username=supabase_admin -d postgres -c "
338351
CREATE TABLE IF NOT EXISTS test_config (key TEXT PRIMARY KEY, value TEXT);
339352
INSERT INTO test_config (key, value) VALUES ('http_mock_port', '$HTTP_MOCK_PORT')
340353
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;
341354
"
342355
343-
mkdir -p $out/regression_output
356+
#shellcheck disable=SC2154
357+
mkdir -p "$out/regression_output"
344358
log info "Running pg_regress tests"
345359
if ! log_cmd pg_regress \
346360
--use-existing \
347361
--dbname=postgres \
348362
--inputdir=${./tests} \
349-
--outputdir=$out/regression_output \
350-
--host=${self.supabase.defaults.host} \
363+
--outputdir="$out/regression_output" \
364+
--host=localhost \
351365
--port=${pgPort} \
352366
--user=supabase_admin \
353367
${builtins.concatStringsSep " " sortedTestList} 2>&1; then
354368
log error "pg_regress tests failed"
355-
cat $out/regression_output/regression.diffs
369+
cat "$out/regression_output/regression.diffs"
356370
exit 1
357371
fi
358372
log info "pg_regress tests completed successfully"
359373
360374
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
375+
log_cmd pg_prove -p ${pgPort} -U supabase_admin -h localhost -d postgres -v ${../migrations/tests}/test.sql
362376
log info "Migrations tests completed successfully"
363377
'';
378+
};
364379
in
365380
{
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";
381+
psql_15 = pkgs.runCommand "run-check-harness-psql-15" { } (
382+
lib.getExe (makeCheckHarness self'.packages."psql_15/bin")
383+
);
384+
psql_17 = pkgs.runCommand "run-check-harness-psql-17" { } (
385+
lib.getExe (makeCheckHarness self'.packages."psql_17/bin")
386+
);
387+
psql_orioledb-17 = pkgs.runCommand "run-check-harness-psql-orioledb-17" { } (
388+
lib.getExe (makeCheckHarness self'.packages."psql_orioledb-17/bin")
389+
);
369390
inherit (self'.packages)
370391
wal-g-2
371392
wal-g-3

0 commit comments

Comments
 (0)