79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
import { EventEmitter } from "node:events";
|
|
import { PassThrough } from "node:stream";
|
|
import { describe, expect, it } from "vitest";
|
|
import { pgRestore } from "./restore.js";
|
|
|
|
describe("pgRestore", () => {
|
|
it("invokes pg_restore with --clean/--if-exists and the dump path last", async () => {
|
|
let captured: readonly string[] = [];
|
|
const spawnFn = ((cmd: string, args: readonly string[]) => {
|
|
void cmd;
|
|
captured = args;
|
|
const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough };
|
|
child.stdout = new PassThrough();
|
|
child.stderr = new PassThrough();
|
|
queueMicrotask(() => {
|
|
child.stdout.end();
|
|
child.stderr.end();
|
|
child.emit("close", 0);
|
|
});
|
|
return child as never;
|
|
}) as unknown as typeof import("node:child_process").spawn;
|
|
|
|
await pgRestore({
|
|
connectionString: "postgres://example/db",
|
|
dumpPath: "/tmp/dump.sql.gz",
|
|
spawnFn,
|
|
});
|
|
|
|
expect(captured).toContain("--clean");
|
|
expect(captured).toContain("--if-exists");
|
|
expect(captured[captured.length - 1]).toBe("/tmp/dump.sql.gz");
|
|
});
|
|
|
|
it("forwards --schema when schemaOnly is set", async () => {
|
|
let captured: readonly string[] = [];
|
|
const spawnFn = ((cmd: string, args: readonly string[]) => {
|
|
void cmd;
|
|
captured = args;
|
|
const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough };
|
|
child.stdout = new PassThrough();
|
|
child.stderr = new PassThrough();
|
|
queueMicrotask(() => {
|
|
child.stdout.end();
|
|
child.stderr.end();
|
|
child.emit("close", 0);
|
|
});
|
|
return child as never;
|
|
}) as unknown as typeof import("node:child_process").spawn;
|
|
|
|
await pgRestore({
|
|
connectionString: "postgres://example/db",
|
|
dumpPath: "/tmp/dump.sql.gz",
|
|
schemaOnly: "verify_abc",
|
|
spawnFn,
|
|
});
|
|
|
|
expect(captured).toContain("--schema");
|
|
expect(captured).toContain("verify_abc");
|
|
});
|
|
|
|
it("throws with stderr on non-zero exit", async () => {
|
|
const spawnFn = ((cmd: string) => {
|
|
void cmd;
|
|
const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough };
|
|
child.stdout = new PassThrough();
|
|
child.stderr = new PassThrough();
|
|
queueMicrotask(() => {
|
|
child.stderr.write("FATAL: missing dump");
|
|
child.stderr.end();
|
|
child.stdout.end();
|
|
child.emit("close", 1);
|
|
});
|
|
return child as never;
|
|
}) as unknown as typeof import("node:child_process").spawn;
|
|
|
|
await expect(pgRestore({ connectionString: "x", dumpPath: "y", spawnFn })).rejects.toThrow(/missing dump|exited/);
|
|
});
|
|
});
|