Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function run(argv: string[], db: ReturnType<typeof openDb>): string {

if (command === "export") {
if (!arg) return "splitbill export <group-id>: missing group id";
if (!db.query("SELECT 1 FROM groups WHERE id = ?").get(arg)) return `no such group: ${arg}`;
return exportCsv(db, arg).trimEnd();
}

Expand All @@ -59,5 +60,7 @@ export function run(argv: string[], db: ReturnType<typeof openDb>): string {
if (import.meta.main) {
const db = openDb(process.env.SPLITBILL_DB ?? "splitbill.sqlite");
seedDemoData(db);
console.log(run(process.argv.slice(2), db));
const output = run(process.argv.slice(2), db);
console.log(output);
if (output.startsWith("no such group: ")) process.exitCode = 1;
}
14 changes: 14 additions & 0 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ describe("splitbill export", () => {
const output = run(["export", "flat-4b"], setupDb());
expect(output.split("\n")[0]).toBe("date,payer,description,amount");
});

test("export reports an unknown group", () => {
const output = run(["export", "no-such-group"], setupDb());
expect(output).toBe("no such group: no-such-group");
expect(output).not.toContain("date,payer,description,amount");
});

test("export exits non-zero for an unknown group", () => {
const result = Bun.spawnSync(["bun", "src/cli.ts", "export", "no-such-group"], {
env: { ...process.env, SPLITBILL_DB: ":memory:" },
});
expect(result.stdout.toString().trim()).toBe("no such group: no-such-group");
expect(result.exitCode).not.toBe(0);
});
});

describe("splitbill with no command", () => {
Expand Down
Loading