fix(cli): close direct query resources after command execution
Fix #745
This commit is contained in:
@@ -24,6 +24,7 @@ export interface CliResult {
|
||||
|
||||
interface RunOptions {
|
||||
backend?: Backend;
|
||||
backendFactory?: (env?: NodeJS.ProcessEnv) => Promise<Backend>;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
diagnostics?: () => Promise<DbxDiagnostics>;
|
||||
}
|
||||
@@ -56,6 +57,7 @@ class CliError extends Error {
|
||||
|
||||
export async function runCli(argv: string[], options: RunOptions = {}): Promise<CliResult> {
|
||||
const env = options.env ?? process.env;
|
||||
let ownedBackend: Backend | undefined;
|
||||
|
||||
try {
|
||||
const flags = parseFlags(argv);
|
||||
@@ -69,7 +71,8 @@ export async function runCli(argv: string[], options: RunOptions = {}): Promise<
|
||||
return ok(`${usage()}\n`);
|
||||
}
|
||||
|
||||
const backend = options.backend ?? (await createBackend(env));
|
||||
const backendFactory = options.backendFactory ?? createBackend;
|
||||
const backend = options.backend ?? (ownedBackend = await backendFactory(env));
|
||||
|
||||
if (args[0] === "doctor") {
|
||||
ensureArgCount(args, 1, "dbx doctor");
|
||||
@@ -242,6 +245,8 @@ export async function runCli(argv: string[], options: RunOptions = {}): Promise<
|
||||
: "ERROR";
|
||||
const wantsJson = argv.includes("--json");
|
||||
return fail(code, message, wantsJson);
|
||||
} finally {
|
||||
await ownedBackend?.close?.().catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,6 +87,54 @@ test("runs read query as json", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("closes backend resources created by the CLI", async () => {
|
||||
let closed = false;
|
||||
const result = await runCli(["query", "local", "select count(*) as total from users", "--json"], {
|
||||
backendFactory: async () =>
|
||||
fakeBackend({
|
||||
close: async () => {
|
||||
closed = true;
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
assert.equal(result.exitCode, 0);
|
||||
assert.equal(closed, true);
|
||||
});
|
||||
|
||||
test("closes backend resources created by the CLI after failures", async () => {
|
||||
let closed = false;
|
||||
const result = await runCli(["query", "local", "select count(*) as total from users", "--json"], {
|
||||
backendFactory: async () =>
|
||||
fakeBackend({
|
||||
executeQuery: async () => {
|
||||
throw new Error("boom");
|
||||
},
|
||||
close: async () => {
|
||||
closed = true;
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
assert.equal(result.exitCode, 1);
|
||||
assert.equal(JSON.parse(result.stderr).error.message, "boom");
|
||||
assert.equal(closed, true);
|
||||
});
|
||||
|
||||
test("does not close caller-provided backend resources", async () => {
|
||||
let closed = false;
|
||||
const result = await runCli(["query", "local", "select count(*) as total from users", "--json"], {
|
||||
backend: fakeBackend({
|
||||
close: async () => {
|
||||
closed = true;
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
assert.equal(result.exitCode, 0);
|
||||
assert.equal(closed, false);
|
||||
});
|
||||
|
||||
test("runs query as csv", async () => {
|
||||
const result = await runCli(["query", "local", "select count(*) as total from users", "--format", "csv"], {
|
||||
backend: fakeBackend(),
|
||||
|
||||
Reference in New Issue
Block a user