Skip to content

Commit a1f6a56

Browse files
authored
fix: js bin and add tests (#531)
fixes #529
1 parent 4a62cc0 commit a1f6a56

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

.github/workflows/pull_request.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172

173173
test-js-bindings:
174174
name:
175-
Test JS Bindings
175+
Test JS Packages
176176
# use the same image we use for compiling
177177
runs-on: ubuntu-22.04
178178
services:
@@ -201,12 +201,15 @@ jobs:
201201
uses: oven-sh/setup-bun@v2
202202
- name: Install JS dependencies
203203
run: bun install
204-
- name: Build TypeScript code
204+
- name: Build backend-jsonrpc
205205
working-directory: packages/@postgrestools/backend-jsonrpc
206206
run: bun run build
207-
- name: Run JS tests
207+
- name: Run backend-jsonrpc test
208208
working-directory: packages/@postgrestools/backend-jsonrpc
209209
run: bun run test
210+
- name: Run cli test
211+
working-directory: packages/@postgrestools/postgrestools
212+
run: bun run test
210213

211214
codegen:
212215
name: Check Codegen

packages/@postgrestools/postgrestools/bin/postgrestools

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ const PLATFORMS = {
2121
};
2222

2323
function isMusl() {
24-
let stderr;
24+
let stdout;
2525
try {
26-
stderr = execSync("ldd --version", {
26+
stdout = execSync("ldd --version", {
2727
stdio: [
2828
"ignore", // stdin
2929
"pipe", // stdout – glibc systems print here
3030
"pipe", // stderr – musl systems print here
3131
],
3232
});
3333
} catch (err) {
34-
stderr = err.stderr;
34+
stdout = err.stderr;
3535
}
36-
if (stderr.indexOf("musl") > -1) {
36+
if (typeof stdout === 'string' && stdout.indexOf("musl") > -1) {
3737
return true;
3838
}
3939
return false;

packages/@postgrestools/postgrestools/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,8 @@
4040
"@postgrestools/cli-x86_64-linux-gnu": "<placeholder>",
4141
"@postgrestools/cli-aarch64-linux-gnu": "<placeholder>",
4242
"@postgrestools/cli-x86_64-linux-musl": "<placeholder>"
43+
},
44+
"scripts": {
45+
"test": "bun test"
4346
}
4447
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select 1;

0 commit comments

Comments
 (0)