@@ -6,6 +6,10 @@ const path = require("node:path");
66const net = require ( "node:net" ) ;
77const { spawn, spawnSync } = require ( "node:child_process" ) ;
88
9+ function sqlLiteral ( value ) {
10+ return `'${ String ( value ) . replace ( / ' / g, "''" ) } '` ;
11+ }
12+
913function findOnPath ( cmd ) {
1014 const which = spawnSync ( "sh" , [ "-lc" , `command -v ${ cmd } ` ] , { encoding : "utf8" } ) ;
1115 if ( which . status === 0 ) return String ( which . stdout || "" ) . trim ( ) ;
@@ -92,6 +96,22 @@ async function withTempPostgres(t) {
9296 stdio : [ "ignore" , "pipe" , "pipe" ] ,
9397 } ) ;
9498
99+ // Register cleanup immediately so failures below don't leave a running postgres and hang CI.
100+ t . after ( async ( ) => {
101+ postgresProc . kill ( "SIGTERM" ) ;
102+ try {
103+ await waitFor (
104+ async ( ) => {
105+ if ( postgresProc . exitCode === null ) throw new Error ( "still running" ) ;
106+ } ,
107+ { timeoutMs : 5000 , intervalMs : 100 }
108+ ) ;
109+ } catch {
110+ postgresProc . kill ( "SIGKILL" ) ;
111+ }
112+ fs . rmSync ( tmpRoot , { recursive : true , force : true } ) ;
113+ } ) ;
114+
95115 const { Client } = require ( "pg" ) ;
96116
97117 const connectLocal = async ( database = "postgres" ) => {
@@ -109,26 +129,11 @@ async function withTempPostgres(t) {
109129 const postgresPassword = "postgrespw" ;
110130 {
111131 const c = await connectLocal ( ) ;
112- await c . query ( " alter user postgres password $1" , [ postgresPassword ] ) ;
132+ await c . query ( ` alter user postgres password ${ sqlLiteral ( postgresPassword ) } ;` ) ;
113133 await c . query ( "create database testdb" ) ;
114134 await c . end ( ) ;
115135 }
116136
117- t . after ( async ( ) => {
118- postgresProc . kill ( "SIGTERM" ) ;
119- try {
120- await waitFor (
121- async ( ) => {
122- if ( postgresProc . exitCode === null ) throw new Error ( "still running" ) ;
123- } ,
124- { timeoutMs : 5000 , intervalMs : 100 }
125- ) ;
126- } catch {
127- postgresProc . kill ( "SIGKILL" ) ;
128- }
129- fs . rmSync ( tmpRoot , { recursive : true , force : true } ) ;
130- } ) ;
131-
132137 const adminUri = `postgresql://postgres:${ postgresPassword } @127.0.0.1:${ port } /testdb` ;
133138 return { port, socketDir, adminUri, postgresPassword } ;
134139}
@@ -249,10 +254,11 @@ test("integration: init reports nicely when lacking permissions", { skip: !haveP
249254 {
250255 const c = new Client ( { connectionString : pg . adminUri } ) ;
251256 await c . connect ( ) ;
252- await c . query (
253- "do $$ begin if not exists (select 1 from pg_roles where rolname='limited') then create role limited login password $1; end if; end $$;" ,
254- [ limitedPw ]
255- ) ;
257+ await c . query ( `do $$ begin
258+ if not exists (select 1 from pg_roles where rolname='limited') then
259+ execute 'create role limited login password ${ sqlLiteral ( limitedPw ) } ';
260+ end if;
261+ end $$;` ) ;
256262 await c . query ( "grant connect on database testdb to limited" ) ;
257263 await c . end ( ) ;
258264 }
0 commit comments