41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { assertDisposableTestDatabase } from "./testDatabase";
|
|
|
|
const cleanup: string[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const directory of cleanup.splice(0)) {
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
describe("assertDisposableTestDatabase", () => {
|
|
it("accepts a new uniquely named test database", () => {
|
|
const directory = mkdtempSync(path.join(tmpdir(), "study-db-safety-"));
|
|
cleanup.push(directory);
|
|
const target = path.join(directory, "unique.test.db");
|
|
expect(assertDisposableTestDatabase(`file:${target}`)).toBe(path.resolve(target));
|
|
});
|
|
|
|
it.each(["file:./dev.db", "file:/app/data/study.db"])(
|
|
"rejects protected database path %s",
|
|
(databaseUrl) => {
|
|
expect(() => assertDisposableTestDatabase(databaseUrl)).toThrow(
|
|
"Refusing to use non-disposable database"
|
|
);
|
|
}
|
|
);
|
|
|
|
it("rejects an existing database even when its name looks like a test", () => {
|
|
const directory = mkdtempSync(path.join(tmpdir(), "study-db-safety-"));
|
|
cleanup.push(directory);
|
|
const target = path.join(directory, "existing.test.db");
|
|
writeFileSync(target, "not disposable");
|
|
expect(() => assertDisposableTestDatabase(`file:${target}`)).toThrow(
|
|
"Refusing to use non-disposable database"
|
|
);
|
|
});
|
|
});
|