🌐 AI搜索 & 代理 主页
Skip to content

Commit fa83237

Browse files
Nik SamokhvalovNik Samokhvalov
authored andcommitted
test(cli): harden init integration tests
- Locate initdb/postgres in Debian CI paths - Skip integration tests only when binaries are truly unavailable
1 parent 4fe39a4 commit fa83237

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

cli/test/init.integration.test.cjs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,34 @@ const path = require("node:path");
66
const net = require("node:net");
77
const { 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+
935
function havePostgresBinaries() {
10-
const r = spawnSync("initdb", ["--version"], { stdio: "ignore" });
11-
return r.status === 0;
36+
return !!(findPgBin("initdb") && findPgBin("postgres"));
1237
}
1338

1439
async 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

Comments
 (0)