@@ -6,9 +6,34 @@ const path = require("node:path");
66const net = require ( "node:net" ) ;
77const { spawn, spawnSync } = require ( "node:child_process" ) ;
88
9+ function findOnPath ( cmd ) {
10+ const which = spawnSync ( "sh" , [ "-lc" , `command -v ${ cmd } ` ] , { encoding : "utf8" } ) ;
11+ if ( which . status === 0 ) return String ( which . stdout || "" ) . trim ( ) ;
12+ return null ;
13+ }
14+
15+ function findPgBin ( cmd ) {
16+ const p = findOnPath ( cmd ) ;
17+ if ( p ) return p ;
18+
19+ // Debian/Ubuntu (GitLab CI node:*-bullseye images): binaries usually live here.
20+ // We avoid filesystem globbing in JS and just ask the shell.
21+ const probe = spawnSync (
22+ "sh" ,
23+ [
24+ "-lc" ,
25+ `ls -1 /usr/lib/postgresql/*/bin/${ cmd } 2>/dev/null | head -n 1 || true` ,
26+ ] ,
27+ { encoding : "utf8" }
28+ ) ;
29+ const out = String ( probe . stdout || "" ) . trim ( ) ;
30+ if ( out ) return out ;
31+
32+ return null ;
33+ }
34+
935function havePostgresBinaries ( ) {
10- const r = spawnSync ( "initdb" , [ "--version" ] , { stdio : "ignore" } ) ;
11- return r . status === 0 ;
36+ return ! ! ( findPgBin ( "initdb" ) && findPgBin ( "postgres" ) ) ;
1237}
1338
1439async function getFreePort ( ) {
@@ -44,7 +69,11 @@ async function withTempPostgres(t) {
4469 const socketDir = path . join ( tmpRoot , "sock" ) ;
4570 fs . mkdirSync ( socketDir , { recursive : true } ) ;
4671
47- const init = spawnSync ( "initdb" , [ "-D" , dataDir , "-U" , "postgres" , "-A" , "trust" ] , {
72+ const initdb = findPgBin ( "initdb" ) ;
73+ const postgresBin = findPgBin ( "postgres" ) ;
74+ assert . ok ( initdb && postgresBin , "PostgreSQL binaries not found (need initdb and postgres)" ) ;
75+
76+ const init = spawnSync ( initdb , [ "-D" , dataDir , "-U" , "postgres" , "-A" , "trust" ] , {
4877 encoding : "utf8" ,
4978 } ) ;
5079 assert . equal ( init . status , 0 , init . stderr || init . stdout ) ;
@@ -59,11 +88,9 @@ async function withTempPostgres(t) {
5988
6089 const port = await getFreePort ( ) ;
6190
62- const postgresProc = spawn (
63- "postgres" ,
64- [ "-D" , dataDir , "-k" , socketDir , "-h" , "127.0.0.1" , "-p" , String ( port ) ] ,
65- { stdio : [ "ignore" , "pipe" , "pipe" ] }
66- ) ;
91+ const postgresProc = spawn ( postgresBin , [ "-D" , dataDir , "-k" , socketDir , "-h" , "127.0.0.1" , "-p" , String ( port ) ] , {
92+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
93+ } ) ;
6794
6895 const { Client } = require ( "pg" ) ;
6996
0 commit comments