|
| 1 | +import { describe, it, expect } from "bun:test"; |
| 2 | +import { spawn } from "child_process"; |
| 3 | +import { join, dirname } from "path"; |
| 4 | +import { fileURLToPath } from "url"; |
| 5 | + |
| 6 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 7 | +const binPath = join(__dirname, "../bin/postgrestools"); |
| 8 | +const testSqlPath = join(__dirname, "test.sql"); |
| 9 | + |
| 10 | +describe("postgrestools bin", () => { |
| 11 | + |
| 12 | + it("should check a SQL file successfully", async () => { |
| 13 | + const result = await new Promise((resolve) => { |
| 14 | + const proc = spawn("node", [binPath, "check", testSqlPath], { |
| 15 | + env: { ...process.env }, |
| 16 | + }); |
| 17 | + |
| 18 | + let stdout = ""; |
| 19 | + let stderr = ""; |
| 20 | + |
| 21 | + proc.stdout.on("data", (data) => { |
| 22 | + stdout += data.toString(); |
| 23 | + }); |
| 24 | + |
| 25 | + proc.stderr.on("data", (data) => { |
| 26 | + stderr += data.toString(); |
| 27 | + }); |
| 28 | + |
| 29 | + proc.on("close", (code) => { |
| 30 | + resolve({ code, stdout, stderr }); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + expect(result.code).toBe(0); |
| 35 | + expect(result.stderr).toBe(""); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should fail when file doesn't exist", async () => { |
| 39 | + const result = await new Promise((resolve) => { |
| 40 | + const proc = spawn("node", [binPath, "check", "nonexistent.sql"], { |
| 41 | + env: { ...process.env }, |
| 42 | + }); |
| 43 | + |
| 44 | + let stdout = ""; |
| 45 | + let stderr = ""; |
| 46 | + |
| 47 | + proc.stdout.on("data", (data) => { |
| 48 | + stdout += data.toString(); |
| 49 | + }); |
| 50 | + |
| 51 | + proc.stderr.on("data", (data) => { |
| 52 | + stderr += data.toString(); |
| 53 | + }); |
| 54 | + |
| 55 | + proc.on("close", (code) => { |
| 56 | + resolve({ code, stdout, stderr }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + expect(result.code).not.toBe(0); |
| 61 | + }); |
| 62 | +}); |
0 commit comments