test(db): stand-in pg tools write --file=- to their own stdout

The CI runner gives a child process a socket for stdout, which /dev/stdout cannot open, so every case whose pg_restore streams its script failed there. The COPY terminators in PGTOOLS-020 are spelled as a dump spells them.
This commit is contained in:
Maurice
2026-11-17 14:23:12 +01:00
parent 410d2393e0
commit d3cd484519
2 changed files with 22 additions and 11 deletions
+17 -7
View File
@@ -66,7 +66,12 @@ export function answersTables(names: readonly string[]): string {
/** Writes `content` to the output file the call names with `--file=`. */
export function writesFile(content: string): string {
return `printf '%s' ${shellWord(content)} > "$(fake_file_target "$@")"`;
return `printf '%s' ${shellWord(content)} | fake_write "$@"`;
}
/** Appends what the shell `command` prints to the output the call names with `--file=`. */
export function appendsOutputOf(command: string): string {
return `${command} | fake_append "$@"`;
}
/** Writes the start of a restore script that restricts psql (CVE-2025-8714) and creates TREK's migration table. */
@@ -80,7 +85,7 @@ export function writesRestrictedScript(table = 'public.mikro_orm_migrations'): s
*/
export function listsSchemas(schemas: readonly string[]): string {
const toc = schemas.map((name, i) => `${i + 4}; 2615 ${16000 + i} SCHEMA - ${name} trek\n`).join('');
return `case "$*" in *--list*) printf '%s' ${shellWord(toc)} > "$(fake_file_target "$@")"; exit 0;; esac`;
return `case "$*" in *--list*) printf '%s' ${shellWord(toc)} | fake_write "$@"; exit 0;; esac`;
}
/** Answers a `--data-only` call with the COPY block of `mikro_orm_migrations`, one row per name. */
@@ -106,12 +111,17 @@ export function copiesFileEndingWith(suffix: string, destination: string): strin
}
/**
* The function every stand-in uses to find its `--file=` argument, wherever it
* sits. `--file=-` is stdout, as the real tools read it.
* The functions every stand-in writes its output with: fake_file_target finds
* the `--file=` argument wherever it sits, fake_write (replacing) and
* fake_append copy their stdin there. `--file=-` is the tool's own stdout, as
* the real tools read it, written as such: /dev/stdout cannot be opened when
* stdout is a socket, as it is under the CI runner.
*/
const FILE_TARGET_FUNCTION =
'fake_file_target() { for a in "$@"; do case "$a" in --file=-) printf /dev/stdout;; ' +
'--file=*) printf \'%s\' "${a#--file=}";; esac; done; }';
const FILE_TARGET_FUNCTION = [
'fake_file_target() { for a in "$@"; do case "$a" in --file=*) printf \'%s\' "${a#--file=}";; esac; done; }',
'fake_write() { t="$(fake_file_target "$@")"; if [ "$t" = - ]; then cat; else cat > "$t"; fi; }',
'fake_append() { t="$(fake_file_target "$@")"; if [ "$t" = - ]; then cat; else cat >> "$t"; fi; }',
].join('\n');
/**
* Installs the stand-in `name` in `bin` and returns the path of its call log.
@@ -12,6 +12,7 @@ import { pgDump, pgDumpMigrations, pgRestore } from '../../../../../src/db/engin
import { hasRestrictFix } from '../../../../../src/db/engine/postgres/restore-safety';
import {
answersSchema,
appendsOutputOf,
copiesFileEndingWith,
failsWith,
flagValues,
@@ -149,7 +150,7 @@ describe('pg-tools', () => {
it('PGTOOLS-005b: the restore script is checked line by line, so its COPY data is never read whole', async () => {
const source = path.join(out, 'in.pgdump');
installFakeTool(bin, 'pg_restore', {
snippets: [writesRestrictedScript(), 'seq 1 200000 >> "$(fake_file_target "$@")"'],
snippets: [writesRestrictedScript(), appendsOutputOf('seq 1 200000')],
});
installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
const readWhole = vi.spyOn(fs, 'readFileSync');
@@ -278,7 +279,7 @@ describe('pg-tools', () => {
it('PGTOOLS-014: a script past the limit stops pg_restore, replaces nothing and leaves no work file', async () => {
installFakeTool(bin, 'pg_restore', {
snippets: [writesRestrictedScript(), 'seq 1 200000 >> "$(fake_file_target "$@")"'],
snippets: [writesRestrictedScript(), appendsOutputOf('seq 1 200000')],
});
const psqlLog = installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
await expect(pgRestore(URL_WITH_SECRET, path.join(out, 'in.pgdump'), bin, 64 * 1024)).rejects.toThrow(
@@ -378,9 +379,9 @@ describe('pg-tools', () => {
it("PGTOOLS-020: a plugin's own table named mikro_orm_migrations never passes for TREK's history", async () => {
const forged =
'COPY trek_plugin_notes.mikro_orm_migrations (id, name) FROM stdin;\n1\tMigration29990101000000_forged\n\.\n';
'COPY trek_plugin_notes.mikro_orm_migrations (id, name) FROM stdin;\n1\tMigration29990101000000_forged\n\\.\n';
const real =
'COPY public.mikro_orm_migrations (id, name, executed_at) FROM stdin;\n1\tMigration20200101000000_baseline_schema\tx\n\.\n';
'COPY public.mikro_orm_migrations (id, name, executed_at) FROM stdin;\n1\tMigration20200101000000_baseline_schema\tx\n\\.\n';
installFakeTool(bin, 'pg_restore', { snippets: [printsWhen('--data-only', forged + real)] });
await expect(pgDumpMigrations('/data/in.pgdump', bin)).resolves.toEqual([
'Migration20200101000000_baseline_schema',